基本介绍
实体机无法做快照,如果系统出现异常或者数据损坏,后果严重, 要重做系统,还会造成数据丢失。所以我们可以使用备份和恢复技术
linux 的备份和恢复很简单 , 有两种方式:
- 把需要的文件(或者分区)用 TAR 打包就行,下次需要恢复的时候,再解压开覆盖即可
- 使用 dump 和 restore 命令
安装 dump 和 restore
如果 linux 上没有 dump 和 restore 指令,需要先安装yum -y install dump
yum -y install restore
使用 dump 完成备份
基本介绍
dump 支持分卷和增量备份(所谓增量备份是指备份上次备份后 修改/增加过的文件,也称差异备份)。dump 语法说明
``` dump [ -cu] [-123456789] [ -f <备份后文件名>] [-T <日期>] [ 目录或文件系统] dump []-wW
参数说明
-0123456789:备份的层级; -b<区块大小>:指定区块的大小,单位为KB; -B<区块数目>:指定备份卷册的区块数目; -c:修改备份磁带预设的密度与容量; -d<密度>:设置磁带的密度。单位为BPI; -f<设备名称>:指定备份设备; -h<层级>:当备份层级等于或大于指定的层级时,将不备份用户标示为“nodump”的文件; -n:当备份工作需要管理员介入时,向所有“operator”群组中的使用者发出通知; -s<磁带长度>:备份磁带的长度,单位为英尺; -T<日期>:指定备份的时间与日期; -u:备份完毕后,在/etc/dumpdates中记录备份的文件系统、层级、日期与时间等; -w:与-W类似,但仅显示需要备份的文件; -W:显示需要备份的文件及其最后一次备份的层级、时间与日期。
<a name="fFsMY"></a>
## dump 应用案例
> 将/boot 分区所有内容备份到/opt/boot.bak0.bz2 文件中,备份层级为“0”
dump -0uj -f /opt/boot.bak0.bz2 /boot
[root@localhost boot]# dump -0uj -f /opt/boot.bak0.bz2 /boot DUMP: Date of this level 0 dump: Wed Feb 9 09:45:29 2022 DUMP: Dumping /dev/sda1 (/boot) to /opt/boot.bak0.bz2 DUMP: Label: none DUMP: Writing 10 Kilobyte records DUMP: Compressing output at compression level 2 (bzlib) DUMP: mapping (Pass I) [regular files] DUMP: mapping (Pass II) [directories] DUMP: estimated 178278 blocks. DUMP: Volume 1 started with block 1 at: Wed Feb 9 09:45:29 2022 DUMP: dumping (Pass III) [directories] DUMP: dumping (Pass IV) [regular files] DUMP: Closing /opt/boot.bak0.bz2 DUMP: Volume 1 completed at: Wed Feb 9 09:46:00 2022 DUMP: Volume 1 took 0:00:31 DUMP: Volume 1 transfer rate: 5189 kB/s DUMP: Volume 1 178900kB uncompressed, 160871kB compressed, 1.113:1 DUMP: 178900 blocks (174.71MB) on 1 volume(s) DUMP: finished in 31 seconds, throughput 5770 kBytes/sec DUMP: Date of this level 0 dump: Wed Feb 9 09:45:29 2022 DUMP: Date this dump completed: Wed Feb 9 09:46:00 2022 DUMP: Average transfer rate: 5189 kB/s DUMP: Wrote 178900kB uncompressed, 160871kB compressed, 1.113:1 DUMP: DUMP IS DONE [root@localhost boot]# ll /opt/ 总用量 160892 -rw-r—r—. 1 root root 164732878 2月 9 09:46 boot.bak0.bz2 drwxr-xr-x. 3 root root 4096 1月 19 15:01 idea drwxr-xr-x. 2 root root 4096 1月 19 10:36 jdk drwxr-xr-x. 2 root root 4096 1月 20 14:28 mysql drwxr-xr-x. 2 root root 4096 10月 31 2018 rh drwxr-xr-x. 3 root root
> 在/boot 目录下增加新文件,备份层级为“1”(只备份上次使用层次“0”备份后发生过改变的数据), 注意比较看看这次生成的备份文件 boot1.bak 有多大
dump -1uj -f /opt/boot.bak1.bz2 /boot
[root@localhost boot]# dump -1uj -f /opt/boot.bak1.bz2 /boot DUMP: Date of this level 1 dump: Wed Feb 9 09:48:30 2022 DUMP: Date of last level 0 dump: Wed Feb 9 09:45:29 2022 DUMP: Dumping /dev/sda1 (/boot) to /opt/boot.bak1.bz2 DUMP: Label: none DUMP: Writing 10 Kilobyte records DUMP: Compressing output at compression level 2 (bzlib) DUMP: mapping (Pass I) [regular files] DUMP: mapping (Pass II) [directories] DUMP: estimated 31 blocks. DUMP: Volume 1 started with block 1 at: Wed Feb 9 09:48:30 2022 DUMP: dumping (Pass III) [directories] DUMP: dumping (Pass IV) [regular files] DUMP: Closing /opt/boot.bak1.bz2 DUMP: Volume 1 completed at: Wed Feb 9 09:48:30 2022 DUMP: 30 blocks (0.03MB) on 1 volume(s) DUMP: finished in less than a second DUMP: Date of this level 1 dump: Wed Feb 9 09:48:30 2022 DUMP: Date this dump completed: Wed Feb 9 09:48:30 2022 DUMP: Average transfer rate: 0 kB/s DUMP: Wrote 30kB uncompressed, 11kB compressed, 2.728:1 DUMP: DUMP IS DONE [root@localhost boot]# ll /opt/ 总用量 160904 -rw-r—r—. 1 root root 164732878 2月 9 09:46 boot.bak0.bz2 -rw-r—r—. 1 root root 11270 2月 9 09:48 boot.bak1.bz2 drwxr-xr-x. 3 root root 4096 1月 19 15:01 idea drwxr-xr-x. 2 root root 4096 1月 19 10:36 jdk drwxr-xr-x. 2 root root 4096 1月 20 14:28 mysql drwxr-xr-x. 2 root root 4096 10月 31 2018 rh drwxr-xr-x. 3 root root 4096 1月 19 14:24 tomcat [root@localhost boot]#
通过 dump 命令在配合 crontab 可以实现无人值守备份
<a name="IMj2b"></a>
## dump -W
显示需要备份的文件及其最后一次备份的层级,时间 ,日期
[root@localhost boot]# dump -W Last dump(s) done (Dump ‘>’ file systems):
/dev/mapper/centos-root ( /) Last dump: never /dev/sda1 ( /boot) Last dump: Level 1, Date Wed Feb 9 09:48:30 2022
<a name="y47OE"></a>
## 查看备份时间文件
[root@localhost boot]# cat /etc/dumpdates /dev/sda1 0 Wed Feb 9 09:45:29 2022 +0800 /dev/sda1 1 Wed Feb 9 09:48:30 2022 +0800 ```
dump 备份文件或者目录
前面我们在备份分区时,是可以支持增量备份的,如果备份文件或者目录,不再支持增量备份, 即只能使用 0 级别备份 使用 dump 备份 /etc 整个目录
dump -0j -f /opt/etc.bak.bz2 /etc/
[root@localhost boot]# dump -0j -f /opt/etc.bak.bz2 /etc/
DUMP: Date of this level 0 dump: Wed Feb 9 09:55:48 2022
DUMP: Dumping /dev/mapper/centos-root (/ (dir etc)) to /opt/etc.bak.bz2
DUMP: Label: none
DUMP: Writing 10 Kilobyte records
DUMP: Compressing output at compression level 2 (bzlib)
DUMP: mapping (Pass I) [regular files]
DUMP: mapping (Pass II) [directories]
DUMP: estimated 67180 blocks.
DUMP: Volume 1 started with block 1 at: Wed Feb 9 09:55:49 2022
DUMP: dumping (Pass III) [directories]
DUMP: dumping (Pass IV) [regular files]
DUMP: Closing /opt/etc.bak.bz2
DUMP: Volume 1 completed at: Wed Feb 9 09:55:56 2022
DUMP: Volume 1 took 0:00:07
DUMP: Volume 1 transfer rate: 3463 kB/s
DUMP: Volume 1 76630kB uncompressed, 24245kB compressed, 3.161:1
DUMP: 76630 blocks (74.83MB) on 1 volume(s)
DUMP: finished in 7 seconds, throughput 10947 kBytes/sec
DUMP: Date of this level 0 dump: Wed Feb 9 09:55:48 2022
DUMP: Date this dump completed: Wed Feb 9 09:55:56 2022
DUMP: Average transfer rate: 3463 kB/s
DUMP: Wrote 76630kB uncompressed, 24245kB compressed, 3.161:1
DUMP: DUMP IS DONE
[root@localhost boot]# ll /opt/
总用量 185152
-rw-r--r--. 1 root root 164732878 2月 9 09:46 boot.bak0.bz2
-rw-r--r--. 1 root root 11270 2月 9 09:48 boot.bak1.bz2
-rw-r--r--. 1 root root 24827897 2月 9 09:55 etc.bak.bz2
drwxr-xr-x. 3 root root 4096 1月 19 15:01 idea
drwxr-xr-x. 2 root root 4096 1月 19 10:36 jdk
drwxr-xr-x. 2 root root 4096 1月 20 14:28 mysql
drwxr-xr-x. 2 root root 4096 10月 31 2018 rh
drwxr-xr-x. 3 root root 4096 1月 19 14:24 tomcat
[root@localhost boot]#
#下面这条语句会报错,提示 DUMP: Only level 0 dumps are allowed on a subdirectory
dump -1j -f /opt/etc.bak.bz2 /etc/
[root@localhost boot]# dump -1j -f /opt/etc.bak.bz2 /etc/
DUMP: Only level 0 dumps are allowed on a subdirectory
DUMP: The ENTIRE dump is aborted.
如果是重要的备份文件, 比如数据区,建议将文件上传到其它服务器保存,不要将鸡蛋放在同一个篮子.
使用 restore 完成恢复
基本介绍
restore 命令用来恢复已备份的文件,可以从 dump 生成的备份文件中恢复原文件
restore 基本语法
restore [模式选项] [选项]
说明下面四个模式, 不能混用,在一次命令中, 只能指定一种。
-C :使用对比模式,将备份的文件与已存在的文件相互对比。
-i:使用交互模式,在进行还原操作时,restors 指令将依序询问用户
-r:进行还原模式
-t : 查看模式,看备份文件有哪些文件
选项
-f <备份设备>:从指定的文件中读取备份数据,进行还原操作
应用案例
restore 命令比较模式,比较备份文件和原文件的区别
# 测试 将/boot中一个文件改名
[root@localhost boot]# mv /boot/hello.java /boot/hello100.java
[root@localhost boot]# ll
总用量 161604
-rw-r--r--. 1 root root 153596 1月 14 22:03 config-3.10.0-1160.53.1.el7.x86_64
-rw-r--r--. 1 root root 151918 11月 9 2018 config-3.10.0-957.el7.x86_64
drwx------. 3 root root 4096 11月 9 2018 efi
drwxr-xr-x. 2 root root 4096 11月 10 16:16 grub
drwx------. 5 root root 4096 2月 8 17:09 grub2
-rw-r--r--. 1 root root 0 2月 9 10:22 hello100.java
-rw-------. 1 root root 74300933 11月 10 16:20 initramfs-0-rescue-14b3cc7d82ff441abbb5736eddaf4973.img
-rw-------. 1 root root 31383246 2月 8 17:09 initramfs-3.10.0-1160.53.1.el7.x86_64.img
-rw-------. 1 root root 31586932 11月 10 16:21 initramfs-3.10.0-957.el7.x86_64.img
drwx------. 2 root root 16384 11月 10 16:13 lost+found
-rw-r--r--. 1 root root 320671 1月 14 22:04 symvers-3.10.0-1160.53.1.el7.x86_64.gz
-rw-r--r--. 1 root root 314036 11月 9 2018 symvers-3.10.0-957.el7.x86_64.gz
-rw-------. 1 root root 3620596 1月 14 22:03 System.map-3.10.0-1160.53.1.el7.x86_64
-rw-------. 1 root root 3543471 11月 9 2018 System.map-3.10.0-957.el7.x86_64
-rwxr-xr-x. 1 root root 6639904 11月 10 16:20 vmlinuz-0-rescue-14b3cc7d82ff441abbb5736eddaf4973
-rwxr-xr-x. 1 root root 6773352 1月 14 22:03 vmlinuz-3.10.0-1160.53.1.el7.x86_64
-rwxr-xr-x. 1 root root 6639904 11月 9 2018 vmlinuz-3.10.0-957.el7.x86_64
# 进行比较
[root@localhost boot]# restore -C -f /opt/boot.bak1.bz2
Dump tape is compressed.
Dump date: Wed Feb 9 10:24:03 2022
Dumped from: Wed Feb 9 09:45:29 2022
Level 1 dump of /boot on localhost.localdomain:/dev/sda1
Label: none
filesys = /boot
restore: unable to stat ./hello.java: No such file or directory
Some files were modified! 1 compare errors
# 将文件名改回来
[root@localhost boot]# mv /boot/hello100.java /boot/hello.java
# 再次比较
[root@localhost boot]# restore -C -f /opt/boot.bak1.bz2
Dump tape is compressed.
Dump date: Wed Feb 9 10:24:03 2022
Dumped from: Wed Feb 9 09:45:29 2022
Level 1 dump of /boot on localhost.localdomain:/dev/sda1
Label: none
filesys = /boot
[root@localhost boot]#
restore 命令查看模式,看备份文件有哪些数据/文件
[root@localhost boot]# restore -t -f /opt/boot.bak0.bz2
Dump tape is compressed.
Dump date: Wed Feb 9 09:45:29 2022
Dumped from: the epoch
Level 0 dump of /boot on localhost.localdomain:/dev/sda1
Label: none
2 .
11 ./lost+found
12 ./efi
...
349 ./config-3.10.0-1160.53.1.el7.x86_64
350 ./symvers-3.10.0-1160.53.1.el7.x86_64.gz
351 ./vmlinuz-3.10.0-1160.53.1.el7.x86_64
[root@localhost boot]#
restore 命令还原模式, 注意细节: 如果你有增量备份,需要把增量备份文件也进行恢复, 有几个增量备份文件, 就要恢复几个,按顺序来恢复即可
# 建立临时目录
[root@localhost boot]# mkdir /opt/boottmp
[root@localhost boot]# cd /opt/boottmp/
# //恢复到第 1 次完全备份状态
[root@localhost boottmp]# restore -r -f /opt/boot.bak0.bz2
Dump tape is compressed.
[root@localhost boottmp]# ll
总用量 161736
-rw-r--r--. 1 root root 153596 1月 14 22:03 config-3.10.0-1160.53.1.el7.x86_64
-rw-r--r--. 1 root root 151918 11月 9 2018 config-3.10.0-957.el7.x86_64
drwx------. 3 root root 4096 11月 9 2018 efi
drwxr-xr-x. 2 root root 4096 11月 10 16:16 grub
drwx------. 5 root root 4096 2月 8 17:09 grub2
-rw-------. 1 root root 74300933 11月 10 16:20 initramfs-0-rescue-14b3cc7d82ff441abbb5736eddaf4973.img
-rw-------. 1 root root 31383246 2月 8 17:09 initramfs-3.10.0-1160.53.1.el7.x86_64.img
-rw-------. 1 root root 31586932 11月 10 16:21 initramfs-3.10.0-957.el7.x86_64.img
drwx------. 2 root root 4096 11月 10 16:13 lost+found
-rw-------. 1 root root 145792 2月 9 10:33 restoresymtable
-rw-r--r--. 1 root root 320671 1月 14 22:04 symvers-3.10.0-1160.53.1.el7.x86_64.gz
-rw-r--r--. 1 root root 314036 11月 9 2018 symvers-3.10.0-957.el7.x86_64.gz
-rw-------. 1 root root 3620596 1月 14 22:03 System.map-3.10.0-1160.53.1.el7.x86_64
-rw-------. 1 root root 3543471 11月 9 2018 System.map-3.10.0-957.el7.x86_64
-rwxr-xr-x. 1 root root 6639904 11月 10 16:20 vmlinuz-0-rescue-14b3cc7d82ff441abbb5736eddaf4973
-rwxr-xr-x. 1 root root 6773352 1月 14 22:03 vmlinuz-3.10.0-1160.53.1.el7.x86_64
-rwxr-xr-x. 1 root root 6639904 11月 9 2018 vmlinuz-3.10.0-957.el7.x86_64
# 恢复到第 2 次增量备份状态
[root@localhost boottmp]# restore -r -f /opt/boot.bak1.bz2
Dump tape is compressed.
[root@localhost boottmp]# ll
总用量 161736
-rw-r--r--. 1 root root 153596 1月 14 22:03 config-3.10.0-1160.53.1.el7.x86_64
-rw-r--r--. 1 root root 151918 11月 9 2018 config-3.10.0-957.el7.x86_64
drwx------. 3 root root 4096 11月 9 2018 efi
drwxr-xr-x. 2 root root 4096 11月 10 16:16 grub
drwx------. 5 root root 4096 2月 8 17:09 grub2
-rw-r--r--. 1 root root 0 2月 9 10:22 hello.java
-rw-------. 1 root root 74300933 11月 10 16:20 initramfs-0-rescue-14b3cc7d82ff441abbb5736eddaf4973.img
-rw-------. 1 root root 31383246 2月 8 17:09 initramfs-3.10.0-1160.53.1.el7.x86_64.img
-rw-------. 1 root root 31586932 11月 10 16:21 initramfs-3.10.0-957.el7.x86_64.img
drwx------. 2 root root 4096 11月 10 16:13 lost+found
-rw-------. 1 root root 145872 2月 9 10:33 restoresymtable
-rw-r--r--. 1 root root 320671 1月 14 22:04 symvers-3.10.0-1160.53.1.el7.x86_64.gz
-rw-r--r--. 1 root root 314036 11月 9 2018 symvers-3.10.0-957.el7.x86_64.gz
-rw-------. 1 root root 3620596 1月 14 22:03 System.map-3.10.0-1160.53.1.el7.x86_64
-rw-------. 1 root root 3543471 11月 9 2018 System.map-3.10.0-957.el7.x86_64
-rwxr-xr-x. 1 root root 6639904 11月 10 16:20 vmlinuz-0-rescue-14b3cc7d82ff441abbb5736eddaf4973
-rwxr-xr-x. 1 root root 6773352 1月 14 22:03 vmlinuz-3.10.0-1160.53.1.el7.x86_64
-rwxr-xr-x. 1 root root 6639904 11月 9 2018 vmlinuz-3.10.0-957.el7.x86_64
[root@localhost boottmp]#
restore 命令恢复备份的文件,或者整个目录的文件
[root@hspedu100 opt]# mkdir etctmp
[root@hspedu100 opt]# cd etctmp/
[root@hspedu100 etctmp]# restore -r -f /opt/etc.bak0.bz2
# 建立临时目录
[root@localhost boottmp]# mkdir /opt/etctmp
[root@localhost boottmp]# cd /opt/etctmp/
# 回复/etc目录
[root@localhost etctmp]# restore -r -f /opt/etc.bak.bz2
Dump tape is compressed.
./lost+found: (inode 11) not found on tape
./boot: (inode 131073) not found on tape
./dev: (inode 786433) not found on tape
./proc: (inode 262145) not found on tape
./run: (inode 393217) not found on tape
./sys: (inode 917505) not found on tape
./root: (inode 655361) not found on tape
./var: (inode 262146) not found on tape
./tmp: (inode 655362) not found on tape
./data: (inode 146668) not found on tape
./usr: (inode 393218) not found on tape
./bin: (inode 17) not found on tape
./sbin: (inode 16) not found on tape
./lib: (inode 13) not found on tape
./lib64: (inode 15) not found on tape
./home: (inode 131074) not found on tape
./media: (inode 786434) not found on tape
./mnt: (inode 917506) not found on tape
./opt: (inode 786435) not found on tape
./srv: (inode 14) not found on tape
[root@localhost etctmp]# ll
总用量 2168
drwxr-xr-x. 143 root root 12288 2月 9 09:30 etc
-rw-------. 1 root root 2205520 2月 9 10:37 restoresymtable