Linux文件查找、压缩、打包与备份策略


文件查找

which

which:显示命令绝对路径信息和别名信息。主要用来查找可执行文件的位置,根据环境变量PATH所规范的路径搜索可执行文件的文件名。默认情况下,which 命令将显示可执行文件的第一个存储位置,若加上-a选项,则可以列出所有的可以找到的同名执行文件,而非仅显示第一个而已。

which [-a] command
选项:
-a:列出所有可找到的同名执行文件,而不只是列出第一个

[root@localhost dtest]# which ls 
alias ls='ls --color=auto'
	/usr/bin/ls

whereis

whereis 只寻找系统中某些特定目录下的文件。whereis只要针对/bin和/sbin目录下的执行文件,以及/usr/share/man下的man page文件等,相较于find搜索硬盘来说,时间耗费更少,速度更快。

whereis [-lbmsu] 文件/目录名
选项:
-l:列出whereis查询的几个主要目录
-b:只查询二进制文件
-m:只查询说明文件manual路径下的文件
-s:只查询source源文件
-u:查询不在上述三种文件中的其他特殊文件

[root@localhost dtest]# whereis -l    # 列出whereis查询的目录
bin: /usr/bin
bin: /usr/sbin
bin: /usr/lib
bin: /usr/lib64
bin: /etc
bin: /usr/etc
bin: /usr/games
bin: /usr/local/bin
bin: /usr/local/sbin
bin: /usr/local/etc
bin: /usr/local/lib
bin: /usr/local/games
bin: /usr/include
bin: /usr/local
bin: /usr/libexec
bin: /usr/share
man: /usr/share/man/man0p
man: /usr/share/man/man1
man: /usr/share/man/man1p
[root@localhost dtest]# whereis ls    # 查询ls文件名
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@localhost dtest]# whereis -b ls    # 查询ls文件名搜索到的二进制文件
ls: /usr/bin/ls
[root@localhost dtest]# whereis fstab    # 查询fstab文件名
fstab: /etc/fstab /usr/include/fstab.h /usr/share/man/man5/fstab.5.gz

locate/updatedb

locate直接在后面输入文件的部分名称即可得到结果,通过关键词进行搜索。locate寻找数据是通过已建立的数据库/var/lib/mlocate/mlocate.db进行快速定位,因此要使用locate命令需要先建立mlocate.db数据库。

限制:locate的数据库的建立默认每天执行一次,因此locate无法搜索到新建立的文件信息,可以通过updatedb命令手动更新数据库来解决问题。updatedb命令回去读取/etc/updatedb.conf配置文件的设定,然后再去硬盘进行文件名的搜索,最后更新整个/var/lib/mlocate/mlocate.db数据库文件。

locate [-ilcS]
选项:
-i:忽略大小写
-l:输出几行
-c:仅计算找到的文件数量
-S:输出数据库文件信息

[root@localhost ~]# locate -l 4 passwd    # 找出系统中关键字包含passwd的文件名,并只列出前四个
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
[root@localhost ~]# locate -S    # 列出locate查询的数据库文件的信息
Database /var/lib/mlocate/mlocate.db:
	8,673 directories
	118,666 files
	5,667,232 bytes in file names
	2,579,813 bytes used to store database

find

find:从磁盘进行指定目录开始扫描文件,速度较慢。
命令语法:
> find path -option [ -print ] [ -exec -ok command ] {} \;

参数含义
-name filename查找文件名为filename的文件
-size [±]SIZE查找比SIZE大(+)或小(-)的文件
-type TYPE查找文件类型为TYPE的文件
-perm mode查找文件权限刚好等于mode的文件
-perm -mode查找文件权限必须全部包括mode的文件
-perm +mode查找文件权限包含mode任一权限的文件
-mtime [±]nn n天前的一天之内被更动过的文件; +n n天以前被更动过的文件; -n 天之内被更动过的文件
-uid n所有者uid为n的文件
-gid n所属组gid为n的文件
-user name所有者名称为name 的文件
-group name所属组名称为name的文件
-nouser寻找文件的拥有者不存在于/etc/passwd的文件
-nogroup寻找文件的所属组不存在于/etc/group的文件
-exec将匹配的文件执行该参数所给出的shell命令,相应命令形式为command { } ;,注意{ }\;之间的空格。
-ok和-exec的作用相同,不过以一种更为安全的模式来执行该参数所给出的shell命令,在执每一个命令之前,都会给出提示让用户确定是否执行。

例:找出当前目录下.txt结尾的文件并进行统一删除
1. find . -type f -name “*.txt” -delete
2. find . -type f -name “*.txt” -exec rm -rf {} ;
3. find . -type f -name “*.txt” | xargs rm -rf (管道需要xargs将find查询结果变为一整行便于rm处理)
4. rm -rf $(find . -type f -name “*.txt”) 或 rm -rf ` find . -type f -name “*.txt” `

文件压缩

压缩文件可以减少文件容量,在Linux环境中,压缩文件的扩展名大多是:*.tar, *.tar.gz, *.gz, *.Z, *.bz2, *.xz。Linux上常见的压缩指令就是gzip, bzip2及最新的xz,gzip、bzip2以及xz由GNU计划所开发,这些指令通常只能针对一个文件进行压缩和解压缩,因此打包tar就非常重要。

  • *.Z :compress 程序压缩的文件;
  • *.zip :zip 程序压缩的文件;
  • *.gz :gzip 程序压缩的文件;
  • *.bz2 :bzip2 程序压缩的文件;
  • *.xz :xz 程序压缩的文件;
  • *.tar :tar 程序打包的数据,并没有压缩过;
  • *.tar.gz :tar 程序打包的文件,其中并且经过 gzip 的压缩
  • *.tar.bz2 :tar 程序打包的文件,其中并且经过 bzip2 的压缩
  • *.tar.xz :tar 程序打包的文件,其中并且经过 xz 的压缩

gzip

gzip 是为了取代 compress 并提供更好的压缩比而成立,使用 gzip 进行压缩时,在预设的状态下原本的文件会被压缩成为 .gz 的文件名,原文件不复存在。

gzip [-cdtv#] 文件名
选项:
-c:将压缩的数据输出到屏幕上
-d:解压缩
-t:检验压缩文件的一致性
-v:显示原文件/压缩文件的压缩比信息
-#:#为数字代表压缩等级,-1 最快,但是压缩比最差;-9 最慢,但是压缩比最好。预设是 -6

bzip2

bzip2 则是为了取代 gzip 并提供更佳的压缩比而成立,用法与gzip几乎相同。

bzip2 [-cdkzv#] 文件名
选项:
-c :将压缩的过程产生的数据输出到屏幕上
-d :解压缩
-k :不删除原文件
-z :压缩的参数 (默认值,可以不加)
-v :显示出原文件/压缩文件案的压缩比等信息
-# :#为数字代表压缩等级,-1 最快,但是压缩比最差;-9 最慢,但是压缩比最好。预设是 -6

xz

xz为了更高的压缩比而来,但是虽然它有了更高的压缩比,却耗费的时间比gzip久很多。

xz [-dtlkc#] 文件名
选项:
-d :解压缩
-t :测试压缩文件的完整性
-l :列出压缩文件的相关信息
-k :保留原文件
-c :将压缩的过程产生的数据输出到屏幕上
-# :#为数字代表压缩等级,-1 最快,但是压缩比最差;-9 最慢,但是压缩比最好。预设是 -6

文件打包

tar命令

tar
tar命令:将多个文件或目录打包成一个大文件,并能将文件和目录从打包的文件中还原。

常用参数

  • -c, --create,create a new archive,创建压缩包文件
  • -t, --list,list the contents of an archive,列出打包文件中的内容(重点在查看文件名)
  • -x, --extract, --get,extract files from an archive,解压缩
  • -z, --gzip,filter the archive through gzip,通过gzip的格式进行压缩/解压缩,此时文件名最好为*.tar.gz
  • -j, --bzip2,filter the archive through bzip2,通过bzip2的格式进行压缩/解压缩,此时文件名最好为*.tar.bz2
  • -J, --xz,filter the archive through xz,通过xz的格式进行压缩/解压缩,此时文件名最好为*.tar.xz
  • -v, --verbose,verbosely list files processed,详细列出已处理的文件,显示压缩的过程
  • -f, --file=ARCHIVE,use archive file or device ARCHIVE,其后接要被处理的压缩包文件名
  • -C, --directory=DIR,change to directory DIR,该选项用在解压缩(在特定目录中解压缩)
  • -p, --preserve-permissions,extract information about file permissions (default for superuser),保留有关文件权限的信息(超级用户的默认设置),常用于备份重要的配置文件
  • -P, --absolute-names,don’t strip leading `/'s from file names,保留绝对路径
  • -X, --exclude-from=FILE,exclude patterns listed in FILE,在压缩过程中不要将FILE打包。

常用命令

  • 压缩:tar -cvz/j/J -f filename.tar.gz/bz2/xz
  • 查询:tar -tvz/j/J -f filename.tar.gz/bz2/xz
  • 解压缩:tar -xvz/j/J -f filename.tar.gz/bz2/xz

压缩实践

[root@localhost ~]# tar -czvf ./etc.tar.gz /etc    -- 压缩/etc目录
[root@localhost ~]# ll -h 
total 6.8M
-rw-r--r-- 1 root root 6.8M May  6 03:09 etc.tar.gz

[root@localhost ~]# tar -tzvf etc.tar.gz | more    --查看压缩包中的文件名发现每个文件都没有根目录
drwxr-xr-x root/root         0 2021-05-06 00:53 etc/
-rw-r--r-- root/root       473 2021-04-27 00:15 etc/fstab
-rw------- root/root         0 2021-04-27 00:15 etc/crypttab
lrwxrwxrwx root/root         0 2021-04-27 00:15 etc/mtab -> /proc/self/mounts
......

[root@localhost ~]# tar -xzvf etc.tar.gz   --解压缩目录(默认在本目录下进行解压缩)
[root@localhost ~]# ll
total 6908
drwxr-xr-x 78 root root    8192 May  6 00:53 etc
-rw-r--r--  1 root root 7061459 May  6 03:09 etc.tar.gz

查看的时候发现每个文件都没有了根目录,这是因为为了安全起见,tar所记录的文件名就是解压缩后的实际文件名。如果拿掉了根目录,假设将备份数据在 /tmp 解开,那么解压缩的文件就会变成/tmp/etc/xxx。 但如果没有拿掉根目录,解压缩后的文件名就会是绝对路径, 即解压缩后的数据一定会被放置 到 /etc/xxx 去,如此一来,原本的 /etc/ 底下的数据, 就会被备份数据所覆盖。(注:如果确定备份根目录到tar文件中,可以使用-P参数)

或者可以使用相对路径进行压缩:

[root@localhost /]# tar -czvf ~/etc.tar.gz ./etc

[root@localhost /]# tar -tzvf ~/etc.tar.gz | more     --此时查看文件的路径就是相对路径
drwxr-xr-x root/root         0 2021-05-06 00:53 ./etc/
-rw-r--r-- root/root       473 2021-04-27 00:15 ./etc/fstab
-rw------- root/root         0 2021-04-27 00:15 ./etc/crypttab
lrwxrwxrwx root/root         0 2021-04-27 00:15 ./etc/mtab -> /proc/self/mounts
-rw-r--r-- root/root        84 2021-05-05 23:50 ./etc/resolv.conf

备份策略

xfsdump/xfsrestore

xfsdump

xfsdump可以对完整的文件系统进行备份,除了进行完整备份,还可以进行累积备份。第一次备份一定是完整备份,完整备份在xfsdump中被定义为level 0,各个level记录文件放置于/var/lib/xfsdump/inventory中。
xfsdumpxfsdump的限制

  • xfsdump只能备份已挂载的xfs文件系统。
  • xfsdump必须使用root权限才能操作。
  • xfsdump备份下来的数据只能由xfsrestore解析。
  • xfsdump通过文件系统的UUID来分辨各个备份文件,因此不能备份两个具有相同UUID的文件系统。

常用参数
常用参数

  • -L session_label,Specifies a label for the dump session. It can be any arbitrary string up to 255 characters long,记录每次备份的session标签,并可以填写针对此文件系统的简易说明。
  • -M label [ -M label … ],Specifies a label for the first media object (for example, tape cartridge) written on the cor‐responding destination during the session. It can be any arbitrary string up to 255 characters long. Multiple media object labels can be specified, one for each destination,记录存储媒体的标签,并可以填写针对此存储媒体的简易说明。
  • -l level ,Specifies a dump level of 0 to 9. The dump level determines the base dump to which this dump is relative. The base dump is the most recent dump at a lesser level. A level 0 dump is absolute - all files are dumped. A dump level where 1 <= level <= 9 is referred to as an incremental dump. Only files that have been changed since the base dump are dumped. Subtree dumps(see the -s option below) cannot be used as the base for incremental dumps. 指定等级,有0-9十个等级,预设为0,即完整备份。
  • -f dest [ -f dest … ],Specifies a dump destination.其后接产生的文件。
  • -I Displays the xfsdump inventory (no dump is performed). xfsdump records each dump session in an online inventory in /var/lib/xfsdump/inventory.

xfsdump预设仅支持文件系统的备份,并不支持特定目录的备份。

实践:使用xfsdump备份完整的文件系统
xfsdump将/boot整个文件系统备份下来并将备份的相关信息(文件系统/时间/session ID等)写入/var/lib/xfsdump/inventory中,给下次备份作为参考依据。

[root@bogon ~]# xfsdump -l 0 -L boot_all -M boot_all -f /tmp/boot.dump /boot

xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.7 (dump format 3.0) - type ^C for status and control
xfsdump: level 0 dump of bogon:/boot  # 开始备份本机的/boot系统
xfsdump: dump date: Wed May 6 20:30:44 2020  # 备份时间
xfsdump: session id: f5faccf7-538c-4f2a-8ca6-d987fb7d833a  # dump ID  
xfsdump: session label: "boot_all"   # session 标签
xfsdump: ino map phase 1: constructing initial dump list  # 开始备份程序
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 151900864 bytes
xfsdump: creating dump session media file 0 (media 0, file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 151575176 bytes
xfsdump: dump size (non-dir files) : 151335512 bytes
xfsdump: dump complete: 15 seconds elapsed
xfsdump: Dump Summary:
xfsdump: stream 0 /tmp/boot.dump OK (success)
xfsdump: Dump Status: SUCCESS

[root@bogon ~]# ll /tmp/boot.dump

-rw-r--r--. 1 root root 151575176 May 6 20:30 /tmp/boot.dump

[root@bogon ~]# ll /var/lib/xfsdump/inventory/
total 16
-rw-r--r--. 1 root root 5760 May 6 20:31 e1e215d9-1606-472c-b78c-6a3c4ce8e8b1.StObj
-rw-r--r--. 1 root root 312 May 6 20:31 f37a802e-3cde-49ea-8384-ed251e97f106.InvIndex
-rw-r--r--. 1 root root 576 May 6 20:30 fstab

实践:使用xfsdump进行累计备份

[root@bogon ~]# xfsdump -I # 查看xfsdump的文件系统
file system 0:
    fs id:        f37a802e-3cde-49ea-8384-ed251e97f106
    session 0:
        mount point:    bogon:/boot
        device:        bogon:/dev/sda1
        time:        Wed May  6 20:30:44 2020
        session label:    "boot_all"
        session id:    f5faccf7-538c-4f2a-8ca6-d987fb7d833a
        level:        0
        resumed:    NO
        subtree:    NO
        streams:    1
        stream 0:
            pathname:    /tmp/boot.dump
            start:        ino 69 offset 0
            end:        ino 1572967 offset 0
            interrupted:    NO
            media files:    1
            media file 0:
                mfile index:    0
                mfile type:    data
                mfile size:    151575176
                mfile start:    ino 69 offset 0
                mfile end:    ino 1572967 offset 0
                media label:    "boot_all"
                media id:    0ee84558-e516-489d-883b-aa02d42122cb
 xfsdump: Dump Status: SUCCESS
 
[root@bogon ~]# dd if=/dev/zero of=/boot/test.txt bs=1M count=10  # 创建测试数据
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.20551 s, 51.0 MB/s

[root@bogon ~]# xfsdump -l 1 -L boot_2 -M boot_2 -f /tmp/boot.dump1 /boot  # 建立差异备份

[root@bogon ~]# ll /tmp/boot*    # 查看文件大小,dump1的文件大小就是刚刚创建的测试数据大小
-rw-r--r--. 1 root root 151575176 May  6 20:30 /tmp/boot.dump
-rw-r--r--. 1 root root  10510952 May  6 20:31 /tmp/boot.dump1

[root@bogon ~]# xfsdump -I
file system 0:
    fs id:        f37a802e-3cde-49ea-8384-ed251e97f106
    session 0:
        mount point:    bogon:/boot
        device:        bogon:/dev/sda1
        time:        Wed May  6 20:30:44 2020
        session label:    "boot_all"
        session id:    f5faccf7-538c-4f2a-8ca6-d987fb7d833a
        level:        0
        resumed:    NO
        subtree:    NO
        streams:    1
        stream 0:
            pathname:    /tmp/boot.dump
            start:        ino 69 offset 0
            end:        ino 1572967 offset 0
            interrupted:    NO
            media files:    1
            media file 0:
                mfile index:    0
                mfile type:    data
                mfile size:    151575176
                mfile start:    ino 69 offset 0
                mfile end:    ino 1572967 offset 0
                media label:    "boot_all"
                media id:    0ee84558-e516-489d-883b-aa02d42122cb
    session 1:
        mount point:    bogon:/boot
        device:        bogon:/dev/sda1
        time:        Wed May  6 20:31:30 2020
        session label:    "boot_2"
        session id:    d2d50506-1d65-429c-9267-909585f26594
        level:        1
        resumed:    NO
        subtree:    NO
        streams:    1
        stream 0:
            pathname:    /tmp/boot.dump1
            start:        ino 82 offset 0
            end:        ino 83 offset 0
            interrupted:    NO
            media files:    1
            media file 0:
                mfile index:    0
                mfile type:    data
                mfile size:    10510952
                mfile start:    ino 82 offset 0
                mfile end:    ino 83 offset 0
                media label:    "boot_2"
                media id:    b740ecdf-e3e2-49ca-a99d-fb6b765f64ab
xfsdump: Dump Status: SUCCESS

xfsrestore

xfsrestore restores filesystems from dumps produced by xfsdump. 使用xfsrestore复原备份文件。

常用参数
xfsrestore常用参数

  • -I Causes the xfsdump inventory to be displayed (no restore is performed). 显示xfsdump清单,可查询备份数据,包括label与备份时间。

  • -f source [ -f source … ] Specifies a source of the dump to be restored.后接备份文件。

  • -L session_label,Specifies the label of the dump session to be restored.

  • -s subtree,Specifies a subtree to restore. 后接特定的目录,只复原某一个文件或目录。

实践:复原备份

[root@bogon ~]# xfsrestore -I  # 使用xfsrestore -I来查阅xfsdump的内容,与xfsdump -I输出相同
file system 0:
    fs id:        f37a802e-3cde-49ea-8384-ed251e97f106
    session 0:
        mount point:    bogon:/boot
        device:        bogon:/dev/sda1
        time:        Wed May  6 20:30:44 2020
        session label:    "boot_all"
        session id:    f5faccf7-538c-4f2a-8ca6-d987fb7d833a
        level:        0
        resumed:    NO
        subtree:    NO
        streams:    1
        stream 0:
            pathname:    /tmp/boot.dump
            start:        ino 69 offset 0
            end:        ino 1572967 offset 0
            interrupted:    NO
            media files:    1
            media file 0:
                mfile index:    0
                mfile type:    data
                mfile size:    151575176
                mfile start:    ino 69 offset 0
                mfile end:    ino 1572967 offset 0
                media label:    "boot_all"
                media id:    0ee84558-e516-489d-883b-aa02d42122cb
    session 1:
        mount point:    bogon:/boot
        device:        bogon:/dev/sda1
        time:        Wed May  6 20:31:30 2020
        session label:    "boot_2"
        session id:    d2d50506-1d65-429c-9267-909585f26594
        level:        1
        resumed:    NO
        subtree:    NO
        streams:    1
        stream 0:
            pathname:    /tmp/boot.dump1
            start:        ino 82 offset 0
            end:        ino 83 offset 0
            interrupted:    NO
            media files:    1
            media file 0:
                mfile index:    0
                mfile type:    data
                mfile size:    10510952
                mfile start:    ino 82 offset 0
                mfile end:    ino 83 offset 0
                media label:    "boot_2"
                media id:    b740ecdf-e3e2-49ca-a99d-fb6b765f64ab
xfsrestore: Restore Status: SUCCESS

简单复原level 0文件系统

[root@bogon ~]# xfsrestore -f /tmp/boot.dump -L boot_all /boot   # 将数据覆盖到/boot
[root@bogon ~]# xfsrestore -f /tmp/boot.dump -L boot_all /tmp/boot  # 另外复原一份数据在/tmp/boot
[root@bogon ~]# du -sm /boot /tmp/boot
155    /boot
145    /tmp/boot

为何两者大小不一样?
[root@bogon ~]# diff -r /boot /tmp/boot
Only in /boot: test.txt

直接复原的结果是:同名的文件被覆盖,新的文件被保留,所以在/boot中创建的测试数据不会因为复原而消失

dd/cpio

dd

dd(dd可以读取磁盘装置的内容,然后将整个装置备份成一个文件)
dd if=“input_file” of=“output_file” bs=“block_size” count=“number”

  • input_file和out_file也可以是装置
  • bs为规划的一个block的大小,默认512bytes
  • count为bs的数量

默认dd一个一个扇区读写,所以没用到的扇区也会被写入备份文件,所以文件会与磁盘相同大小,但是xfsdump只备份文件系统中使用到的部分。

实践:备份passwd文件

[root@bogon ~]# dd if=/etc/passwd of=/tmp/passwd.bak
4+1 records in
4+1 records out
2354 bytes (2.4 kB) copied, 0.000565697 s, 4.2 MB/s
[root@bogon ~]# ll /etc/passwd /tmp/passwd.bak 
-rw-r--r--. 1 root root 2354 May  4 14:36 /etc/passwd
-rw-r--r--. 1 root root 2354 May  6 20:59 /tmp/passwd.bak

实践:备份/boot文件系统

[root@bogon ~]# dd if=/dev/sda1 of=/tmp/boot.img
2097152+0 records in
2097152+0 records out
1073741824 bytes (1.1 GB) copied, 22.4468 s, 47.8 MB/s
[root@bogon ~]# ll /tmp/boot.img 
-rw-r--r--. 1 root root 1073741824 May  6 21:02 /tmp/boot.img

cpio

cpio可以备份任何东西,包括装置设备文件。不过cpio不会主动寻找文件备份,因此一般配合find类等可以找到文件名的指令来告知cpio应该备份的数据在哪里。

常用指令

  • cpio -ovcB > [file|device] 备份
  • cpio -ivcdu < [file|device] 还原
  • cpio -ivct < [file|device] 查看

常用参数
备份会使用到的选项与参数:

  • -o :将数据 copy 输出到文件或装置上
  • -B :让预设的 Blocks 增加至 5120 bytes ,预设是 512 bytes

还原会使用到的选项与参数:

  • -i :将数据自文件或装置 copy 出系统当中
  • -d :自动建立目录,使用 cpio 所备份的数据内容不一定会在同一层目录中,因此必须要让 cpio 在还原时可以建立新目录
  • -u :自动的将较新的文件覆盖较旧的文件
  • -t :需配合 -i 选项,可用在查看以 cpio 建立的文件或装置的内容 一些可共享的选项与参数
  • -v :让储存的过程中文件名可以在屏幕上显示
  • -c :一种较新的 portable format 方式储存

实践

[root@bogon /]# find boot | cpio -ocvB > /tmp/boot.cpio    # 压缩

[root@bogon /]# ll /tmp/boot.cpio    # 查看
-rw-r--r--. 1 root root 161633280 May  6 21:11 /tmp/boot.cpio
[root@bogon /]# file /tmp/boot.cpio 
/tmp/boot.cpio: ASCII cpio archive (SVR4 with no CRC)

[root@bogon ~]# cpio -idvc < /tmp/boot.cpio     # 解压缩

鸟哥的备份策略

鸟哥的备份策略

鸟哥的备份策略分为两大部分,一个是每日备份经常性变动的重要数据,一个是每周备份不常变动的信息,使用两个scripts分别实现:

  • 主机硬件:使用一个独立的文件系统存储备份数据,该文件系统挂载到/backup
  • 每日备份:MySQL数据库
  • 每周备份:/home、/etc、/var、/boot、/usr/local等目录
  • 自动处理:利用/etc/crontab提供自动备份服务
  • 异地备援:每月定期将数据分别刻录到光盘并使用网络传输到另一部主机上

每周备份scripts [设定备份目录(判断是否存在不存在就创建)→ 使用 cp 和 tar 将数据备份到设定的备份目录 ]

[root@study ~]# vi /backup/backupwk.sh 
#!/bin/bash 
# basedir:用来储存此脚本所预计备份的数据之目录(请独立文件系统) 
basedir=/backup/weekly  
PATH=/bin:/usr/bin:/sbin:/usr/sbin; export PATH 
export LANG=C 

# 设定要备份的服务的配置文件,以及备份的目录 
named=$basedir/named 
postfixd=$basedir/postfix 
vsftpd=$basedir/vsftp 
sshd=$basedir/ssh 
sambad=$basedir/samba 
wwwd=$basedir/www 
others=$basedir/others 
userinfod=$basedir/userinfo 
# 判断目录是否存在,若不存在则予以建立。 
for dirs in $named $postfixd $vsftpd $sshd $sambad $wwwd $others $userinfod 
do 
 [ ! -d "$dirs" ] && mkdir -p $dirs 
done 
 
# 1. 将系统主要的服务配置文件分别备份下来,同时备份 /etc 全部 
cp -a /var/named/chroot/{etc,var} $named 
cp -a /etc/postfix /etc/dovecot.conf $postfixd 
cp -a /etc/vsftpd/*   $vsftpd 
cp -a /etc/ssh/*   $sshd 
cp -a /etc/samba/*   $sambad 
cp -a /etc/{my.cnf,php.ini,httpd} $wwwd 
cd /var/lib 
tar -jpc -f $wwwd/mysql.tar.bz2  mysql 
cd /var/www 
tar -jpc -f $wwwd/html.tar.bz2  html cgi-bin 
cd / 
tar -jpc -f $others/etc.tar.bz2 etc 
cd /usr/ 
tar -jpc -f $others/local.tar.bz2 local 
 
# 2. 关于使用者参数方面 
cp -a /etc/{passwd,shadow,group} $userinfod 
cd /var/spool 
tar -jpc -f $userinfod/mail.tar.bz2 mail 
cd / 
tar -jpc -f $userinfod/home.tar.bz2 home 
cd /var/spool 
tar -jpc -f $userinfod/cron.tar.bz2 cron at 
 
[root@study ~]# chmod 700 /backup/backupwk.sh 

每日备份scripts [设定备份目录(不存在就创建) 使用tar将数据打包备份到指定目录]

[root@study ~]# vi /backup/backupday.sh 
#!/bin/bash 
basedir=/backup/daily/  # 用来储存此脚本所预计备份的数据之目录(请独立文件系统) 
PATH=/bin:/usr/bin:/sbin:/usr/sbin; export PATH 
export LANG=C 
basefile1=$basedir/mysql.$(date +%Y-%m-%d).tar.bz2 
basefile2=$basedir/cgi-bin.$(date +%Y-%m-%d).tar.bz2 
[ ! -d "$basedir" ] && mkdir $basedir 

#1. MysQL (数据库目录在 /var/lib/mysql) 
cd /var/lib 
tar -jpc -f $basefile1 mysql 
#2. WWW 的 CGI 程序 (如果有使用 CGI 程序的话) 
cd /var/www 
tar -jpc -f $basefile2 cgi-bin 
 
[root@study ~]# chmod 700 /backup/backupday.sh 

自动备份

[root@study ~]# vi /etc/crontab 
30 3 * * 0 root /backup/backupwk.sh 
30 2 * * * root /backup/backupday.sh 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值