Linux 备份与刻录工具
备份基础
系统备份重要目录有:/etc/
、/home/
、/root/
、/var
。不需要备份的目录有:/dev
、/proc
、/sys
、/run
、/mnt
、/media
、/tmp
。
常见备份类型有 3 种:
- 完全备份(full):在备份定义的范围内备份所有数据。
- 差分备份(differential):备份所有自上次备份之后发生过变化的数据。在恢复时需要用到完全备份和最近的差分备份。
- 增量备份(incremental):备份上次备份完成之后所有发生变化的数据,而不考虑上次备份是什么类型。
使用 rsync 备份
rsync
可以对两个目录进行镜像,用法非常简单:rsync -av 来源目录 目标目录
例如备份 /root
目录到 /backup
目录:
[root@101c7 ~]$ rsync -av /root /backup/ sending incremental file list root/ root/.bash_history root/.bash_logout root/.bash_profile root/.bashrc root/.cshrc root/.tcshrc root/.viminfo root/anaconda-ks.cfg root/.pki/ root/.pki/nssdb/ sent 4,095 bytes received 184 bytes 8,558.00 bytes/sec total size is 3,458 speedup is 0.81
进行差异备份使用同样的命令:
[root@101c7 ~]$ cp anaconda-ks.cfg 1.cfg [root@101c7 ~]$ rsync -av /root /backup/ [root@101c7 ~]$ rm -rf 1.cfg [root@101c7 ~]$ rsync -av /root /backup/ [root@101c7 ~]$ ll /backup/root/ total 8 -rw-------. 1 root root 240 Sep 21 18:02 1.cfg -rw-------. 1 root root 1260 Sep 8 01:38 anaconda-ks.cfg
从结果可以看到在当前目录新增或修改文件,备份后 /backup
目录下会一同新增和修改。当前目录删除文件,备份后 /backup
目录并不会同步删除文件。
另外,rsync
还支持通过网络备份。例如将本地 /root
备份到 192.168.2.102
主机上面:
[root@server2 ~]$ rsync -av -e ssh /root root@192.168.2.234:/backup sending incremental file list root/ root/.bash_history root/iptables/ root/iptables/iptables.allow root/iptables/iptables.deny root/iptables/iptables.rules sent 33,124 bytes received 363 bytes 22,324.67 bytes/sec total size is 31,780 speedup is 0.95
种方式要求远端服务器也安装了 rsync
软件。
使用 Dump 备份
Dump 是 ext 文件系统的备份工具。
工具简介
Dump 备份工具可以指定备份等级,与上一等级比较只备份差异文件。
备份对象为单一文件系统时,可以使用完整 dump 功能,可以使用挂载点或设备名来进行备份。
备份对象为目录时,仅能使用 level 0 完整备份,不能使用 -u 参数创建时间记录文件。
命令用法:
dump [-Suvj] [-level] [-f 备份文件] 待备份数据
可用参数:
参数 | 说明 |
---|---|
-S | 仅列出后面的待备份数据需要多少容量才能备份完毕。 |
-u | 将这次的 dump 时间记录到 /etc/dumpdateS 文件中。 |
-v | 将备份过程显示出来。 |
-j | 假如对 bzip2 的支持,将数据压缩,默认压缩等级 2。 |
-level | 从 0 到 9 共 10 个等级。 |
-f | 接备份生成的文件名。 |
-W | 列出在 /etc/fstab 里面具有 dump 设置的分区是否有备份过。 |
完整备份
先测试备份目标需要多少容量:
[root@101c7 audit]$ df -h | grep sdb /dev/sdb4 91M 342K 86M 1% /root/sdb4m [root@101c7 audit]$ dump -S /dev/sdb4 39936
单位为 B,所以需要容量为 39KB。
将 /dev/sdb4
备份到 /root/backup/sdb4.dump
中,同时更新记录文件:
[root@101c7 backup]$ dump -0u -f /root/backup/sdb4.dump /dev/sdb4 DUMP: Date of this level 0 dump: Fri Sep 10 14:26:24 2021 DUMP: Dumping /dev/sdb4 (/root/sdb4m) to /root/backup/sdb4.dump DUMP: DUMP IS DONE
查看备份更新记录:
[root@101c7 backup]$ cat /etc/dumpdates /dev/sdb4 0 Fri Sep 10 14:26:24 2021 -0400
差异备份
先查看上次备份的时间:
[root@101c7 backup]$ dump -W Last dump(s) done (Dump '>' file systems): /dev/sdb4 (/root/sdb4m) Last dump: Level 0, Date Fri Sep 10 14:26:24 2021
可以看到 /dev/sdb4
已经进行过一次 level 0 备份。使用 level 1 备份 /dev/sdb4
:
[root@101c7 sdb4m]$ dump -1u -f /root/backup/sdb4.dump1 /dev/sdb4 DUMP: Date of this level 1 dump: Fri Sep 10 14:34:25 2021 DUMP: Date of last level 0 dump: Fri Sep 10 14:26:24 2021 DUMP: Dumping /dev/sdb4 (/root/sdb4m) to /root/backup/sdb4.dump1 DUMP: DUMP IS DONE
备份目录
对目录备份只能使用 level 0 来完成备份。
下面将 /root/sdb4m/lost+found
目录压缩备份到 /root/backup/lost.dump.bz2
:
[root@101c7 sdb4m]$ dump -0j -f /root/backup/lost.dump.bz2 /root/sdb4m/lost+found/ DUMP: Date of this level 0 dump: Fri Sep 10 14:39:46 2021 DUMP: Dumping /dev/sdb4 (/root/sdb4m (dir /lost+found)) to /root/backup/lost.dump.bz2 DUMP: DUMP IS DONE
还原备份
还原备份使用 restore
工具,主要参数有:
参数 | 说明 |
---|---|
-t | 查看 dump 文件的内容。 |
-C | 将 dump 文件内的数据拿来与实际的文件系统作比较,列出不相同的文件。 |
-i | 进入互动模式,可以仅还原部分文件。 |
-r | 将整个文件系统还原。 |
-h | 查看完整备份数据中的 inode 与文件系统 label 等信息。 |
-f | 选择要读取的 dump 文件。 |
-D | 与 -C 搭配,查出后面接的挂载点与 dump 内有不同的文件。 |
查看 dump
文件 sdb4.dmp
的内容:
[root@101c7 backup]$ restore -t -f sdb4.dump Dump date: Fri Sep 10 14:34:25 2021 Dumped from: Fri Sep 10 14:26:24 2021 Level 1 dump of /root/sdb4m on 101c7:/dev/sdb4 Label: P1 2 . 13 ./test10m.img
显示出文件名与原文件的 inode
号码。
查询 sdb4.dmp
与文件系统的差异:
[root@101c7 backup]$ restore -C -f sdb4.dump -D /root/sdb4m/ Dump date: Fri Sep 10 14:34:25 2021 Dumped from: Fri Sep 10 14:26:24 2021 Level 1 dump of /root/sdb4m on 101c7:/dev/sdb4 Label: P1 filesys = /root/sdb4m/ restore: unable to stat ./test10m.img: No such file or directory Some files were modified! 1 compare errors
提示有一个文件被更改。
进入到需要还原的文件系统的挂载点下,将 sdb4.dump
还原到当前文件系统:
[root@101c7 ext333]$ restore -r -f /root/backup/sdb4.dump restore: ./lost+found: File exists
此文件系统中已存在的文件不会被修改,同名文件则会被覆盖。
想还原部分文件可以使用交互模式:
[root@101c7 ext333]$ restore -i -f /root/backup/sdb4.dump restore > ls .: a.cfg lost+found/ restore > help Available commands are: ls [arg] - list directory cd arg - change directory pwd - print current directory add [arg] - add `arg' to list of files to be extracted delete [arg] - delete `arg' from list of files to be extracted extract - extract requested files setmodes - set modes of requested directories quit - immediately exit program what - list dump header information verbose - toggle verbose flag (useful with ``ls'') prompt - toggle the prompt display help or `?' - print this list If no `arg' is supplied, the current directory is used
除了可以使用 ls
,cd
等命令,主要操作命令为:
add
将文件加入等下要解压缩的文件列表中;delete
将文件从解压缩的列表中删除;extract
开始将刚才选择的文件列表解压。
将 a.cfg
加入解压缩列表:
restore > add a.cfg restore > ls .: *a.cfg lost+found/
要被解压缩的文件前面会出现 * 符号。
开始进行解压缩:
restore > extract You have not read any volumes yet. Unless you know which volume your file(s) are on you should start with the last volume and work towards the first. Specify next volume # (none if no more volumes): 1 set owner/mode for '.'? [yn] n restore > quit [root@101c7 ext333]$ ll total 3 -rw-------. 1 root root 1260 Sep 10 15:05 a.cfg
交互过程中分别会询问:
- 需要的 volume 是什么:1
- 是否需要修改权限:n
完成后使用 quit 命令退出,就可以查看到需要的文件已经被解压出来了。
使用 XfsDump 备份
CentOS 7 中常用 xfsdump
和 xfsrestore
工具来备份 XFS
格式文件系统。
工具简介
xfsdump
和 dump
的备份原理相似,但 xfsdump
通过文件系统的 UUID 来区分各个备份文件,因此不能备份具有相同 UUID 的两个文件系统。此外,xfsdump
不支持对特定目录进行备份。
命令用法:xfsdump [-L 标签] [-M 说明] [-l 备份等级] [-f 输出备份文件] 待备份数据
完整备份
对分区 /xfs333
做完整备份并将结果保存在 /root/backup/sdd1.dump
中:
[root@101c7 xfs333]$ xfsdump -l 0 -L sdd1 -M sdd1 -f /root/backup/sdd1.dump /xfs333 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 101c7:/xfs333 xfsdump: dump date: Thu Sep 16 14:59:39 2021 xfsdump: session id: 4545d809-55e2-4de6-ba33-381ed071399c xfsdump: session label: "sdd1_all" xfsdump: stream 0 /root/backup/sdd1.dump OK (success) xfsdump: Dump Status: SUCCESS
备份记录保存在 /var/lib/xfsdump/inventory
中。
增量备份
先查询是否存在备份记录:
[root@101c7 xfs333]$ xfsdump -I file system 0: fs id: c96b6650-756a-4496-bf26-6e7286c55891 session 0: mount point: 101c7:/xfs333 device: 101c7:/dev/sdd1 time: Thu Sep 16 14:59:39 2021 session label: "sdd1_all" session id: 4545d809-55e2-4de6-ba33-381ed071399c level: 0 resumed: NO subtree: NO streams: 1 stream 0: pathname: /root/backup/sdd1.dump start: ino 67 offset 0 end: ino 70 offset 0 interrupted: NO media files: 1 media file 0: mfile index: 0 mfile type: data mfile size: 10512128 mfile start: ino 67 offset 0 mfile end: ino 70 offset 0 media label: "sdd1_all" media id: e2964845-d8da-4b5f-8226-9390468d538f xfsdump: Dump Status: SUCCESS
可以看到存在一个 session 0 的 level 0 备份。接着进行 level 1 的增量备份,备份参数和制作 level 0 时一样:
[root@101c7 xfs333]$ xfsdump -l 1 -L sdd1_1 -M sdd1_1 -f /root/backup/sdd1.dump1 /xfs333 xfsdump: using file dump (drive_simple) strategy xfsdump: version 3.1.7 (dump format 3.0) - type ^C for status and control xfsdump: level 1 incremental dump of 101c7:/xfs333 based on level 0 dump begun Thu Sep 16 14:59:39 2021 xfsdump: stream 0 /root/backup/sdd1.dump1 OK (success) xfsdump: Dump Status: SUCCESS
完成后可以用 xfsdump -I
查询备份记录,多了一个 session 1 的记录。
还原备份
备份还原使用的是 xfsrestore
命令,基本使用方法如下:
xfsrestore [-f 备份文件] [-L S_label] [-s] 待复原目录
常用选项如下:
参数 | 说明 |
---|---|
-I | 查询备份数据,包括 Label 名称与备份时间等。 |
-f | 后面指定备份文件目录。 |
-L | 指定备份等级。 |
-s | 仅恢复特定目录或文件。 |
-i | 进入交互模式。 |
恢复完整备份,需要先查询到 session label 值,用来代表要恢复的备份等级:
[root@101c7 xfs333]$ xfsrestore -f /root/backup/sdd1.dump -L sdd1_all /xfs333 xfsrestore: using file dump (drive_simple) strategy xfsrestore: stream 0 /root/backup/sdd1.dump OK (success) xfsrestore: Restore Status: SUCCESS
指定将备份数据恢复到 /tmp/xfs/
目录下:
[root@101c7 xfs333]$ xfsrestore -f /root/backup/sdd1.dump -L sdd1_all /tmp/xfs/
指定仅还原备份文件中的 255
目录到 /tmp/xfs
目录:
[root@101c7 xfs333]$ xfsrestore -f /root/backup/sdd1.dump -L sdd1_all -s 255 /tmp/xfs/
恢复 level 1 备份到 /tmp/xfs
目录:
[root@101c7 xfs333]$ xfsrestore -f /root/backup/sdd1.dump1 /tmp/xfs/ xfsrestore: stream 0 /root/backup/sdd1.dump1 OK (success) xfsrestore: Restore Status: SUCCESS
使用交互模式来恢复文件:
[root@101c7 xfs333]$ xfsrestore -f /root/backup/sdd1.dump -i /tmp/xfs/ ========================== subtree selection dialog ========================== the following commands are available: pwd ls [ <path> ] cd [ <path> ] add [ <path> ] delete [ <path> ] extract quit help ->
使用方式与 restoredump
一样,选择好文件后(比如 10M
)使用 extract
命令解压:
-> add 10M -> ls 68 255/ * 67 10M -> extract --------------------------------- end dialog --------------------------------- xfsrestore: restoring non-directory files xfsrestore: restore complete: 154 seconds elapsed xfsrestore: Restore Summary: xfsrestore: stream 0 /root/backup/sdd1.dump OK (success) xfsrestore: Restore Status: SUCCESS [root@101c7 xfs333]$ ll /tmp/xfs/ total 10240 -rw-r--r--. 1 root root 10485760 Sep 16 14:55 10M
其他备份方式
遇到一些特殊的需求时,可能需要使用 dd
和 cpio
命令来进行备份。
使用 dd 备份
dd
可以通过直接读取扇区的方式,将整个设备备份成一个文件,使用方法如下:
dd if="输入文件" of="输出文件" bs="block大小" count="block数量"
例如备份磁盘第一个扇区:
[root@101c7 ext333]$ dd if=/dev/sdb of=/tmp/bmr.back bs=512 count=1 1+0 records in 1+0 records out 512 bytes (512 B) copied, 0.000158292 s, 3.2 MB/s
备份 sdb5
整个分区,类似于 Ghost 软件做的镜像:
[root@101c7 ~]$ dd if=/dev/sdb5 of=/root/sdb5.disk 163840+0 records in 163840+0 records out 83886080 bytes (84 MB) copied, 0.476346 s, 176 MB/s
还原备份到 sdb5
只需要将 if
和 of
交换一下:
[root@101c7 ~]$ dd if=/root/sdb5.disk of=/dev/sdb5 163840+0 records in 163840+0 records out 83886080 bytes (84 MB) copied, 0.246963 s, 340 MB/s
使用 cpio 备份
cpio
(copy in/out)命令从其他命令获取输入,因此需要配合类似 find
等命令来获取需要备份的文件名。cpio
可以将数据备份到磁带机上。常见用法:
目标 | 命令 |
---|---|
备份 | cpio -ovcB > 文件或设备 |
还原 | cpio -ivcdu < 文件或设备 |
查看 | cpio -ivct < 文件或设备 |
例如将 /boot
目录备份到 boot.cpio
:
[root@101c7 ~]$ find /boot | cpio -ocvB > boot.cpio /boot/initramfs-3.10.0-862.el7.x86_64kdump.img /boot/initramfs-3.10.0-1160.41.1.el7.x86_64kdump.img 31339 blocks
将 boot.cpio
解压:
[root@101c7 ~]$ cpio -idvc < boot.cpio
镜像制作
在 Linux 下制作镜像可以使用 mkisofs
命令,可用参数如下:
命令 | 说明 |
---|---|
-o | 生成的镜像文件名字。 |
-r | 附带记录文件属性。 |
-v | 显示过程。 |
-m | 要排除的文件。 |
-V | 新建 Volume。 |
-graft-point | 保留文件结构,否则文件将会全部存在顶层目录下。 |
例如将 /root
和 /etc
目录制作成 system.img
:
[root@101c7 ext333]$ mkisofs -r -V 'home' -o /tmp/system.img -graft-point /root=/root /home=/home I: -input-charset not specified, using utf-8 (detected in locale settings) 47.36% done, estimate finish Fri Sep 10 21:42:20 2021 94.80% done, estimate finish Fri Sep 10 21:42:20 2021 Total translation table size: 0 Total rockridge attributes bytes: 4458 Total directory bytes: 26624 Path table size(bytes): 160 Max brk space used 24000 10557 extents written (20 MB)
-graft-point
后面接要备份的数据,等号左边代表镜像文件内的目录,右侧是实际路径。
光盘刻录
在 Linux 下,使用 cdrecord
命令进行 CD/DVD 刻录。
首先扫描系统中的光驱设备:
[root@101c7 ext333]$ cdrecord --devices wodim: Overview of accessible drives (1 found) : ------------------------------------------------------------------------- 0 dev='/dev/sg2' rwrw-- : 'NECVMWar' 'VMware IDE CDR10' -------------------------------------------------------------------------
将 system.img
刻录成数据 CD:
[root@101c7 ext333]$ cdrecord -v dev='/dev/sg2' fs=8m -dummy -data /tmp/system.img wodim: No write mode specified. wodim: Assuming -tao mode. wodim: Future versions of wodim may have different drive dependent defaults. TOC Type: 1 = CD-ROM scsidev: '/dev/sg2' devname: '/dev/sg2' scsibus: -2 target: -2 lun: -2
使用 wodim
命令刻录 system.img
文件如下:
[root@101c7 xfs333]$ wodim -v dev=/dev/sr0 speed=8 -dummy -eject /tmp/system.img wodim: No write mode specified. wodim: Assuming -tao mode. wodim: Future versions of wodim may have different drive dependent defaults. TOC Type: 1 = CD-ROM scsidev: '/dev/sr0'