基本介绍

实体机无法做快照,如果系统出现异常或者数据损坏,后果严重, 要重做系统,还会造成数据丢失。所以我们可以使用备份和恢复技术
linux 的备份和恢复很简单 , 有两种方式:

  1. 把需要的文件(或者分区)用 TAR 打包就行,下次需要恢复的时候,再解压开覆盖即可
  2. 使用 dump 和 restore 命令

    安装 dump 和 restore

    如果 linux 上没有 dump 和 restore 指令,需要先安装
    1. yum -y install dump
    2. 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:显示需要备份的文件及其最后一次备份的层级、时间与日期。

  1. <a name="fFsMY"></a>
  2. ## dump 应用案例
  3. > 将/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

  1. > 在/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]#

  1. 通过 dump 命令在配合 crontab 可以实现无人值守备份
  2. <a name="IMj2b"></a>
  3. ## dump -W
  4. 显示需要备份的文件及其最后一次备份的层级,时间 ,日期

[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

  1. <a name="y47OE"></a>
  2. ## 查看备份时间文件

[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 整个目录

  1. dump -0j -f /opt/etc.bak.bz2 /etc/
  2. [root@localhost boot]# dump -0j -f /opt/etc.bak.bz2 /etc/
  3. DUMP: Date of this level 0 dump: Wed Feb 9 09:55:48 2022
  4. DUMP: Dumping /dev/mapper/centos-root (/ (dir etc)) to /opt/etc.bak.bz2
  5. DUMP: Label: none
  6. DUMP: Writing 10 Kilobyte records
  7. DUMP: Compressing output at compression level 2 (bzlib)
  8. DUMP: mapping (Pass I) [regular files]
  9. DUMP: mapping (Pass II) [directories]
  10. DUMP: estimated 67180 blocks.
  11. DUMP: Volume 1 started with block 1 at: Wed Feb 9 09:55:49 2022
  12. DUMP: dumping (Pass III) [directories]
  13. DUMP: dumping (Pass IV) [regular files]
  14. DUMP: Closing /opt/etc.bak.bz2
  15. DUMP: Volume 1 completed at: Wed Feb 9 09:55:56 2022
  16. DUMP: Volume 1 took 0:00:07
  17. DUMP: Volume 1 transfer rate: 3463 kB/s
  18. DUMP: Volume 1 76630kB uncompressed, 24245kB compressed, 3.161:1
  19. DUMP: 76630 blocks (74.83MB) on 1 volume(s)
  20. DUMP: finished in 7 seconds, throughput 10947 kBytes/sec
  21. DUMP: Date of this level 0 dump: Wed Feb 9 09:55:48 2022
  22. DUMP: Date this dump completed: Wed Feb 9 09:55:56 2022
  23. DUMP: Average transfer rate: 3463 kB/s
  24. DUMP: Wrote 76630kB uncompressed, 24245kB compressed, 3.161:1
  25. DUMP: DUMP IS DONE
  26. [root@localhost boot]# ll /opt/
  27. 总用量 185152
  28. -rw-r--r--. 1 root root 164732878 2 9 09:46 boot.bak0.bz2
  29. -rw-r--r--. 1 root root 11270 2 9 09:48 boot.bak1.bz2
  30. -rw-r--r--. 1 root root 24827897 2 9 09:55 etc.bak.bz2
  31. drwxr-xr-x. 3 root root 4096 1 19 15:01 idea
  32. drwxr-xr-x. 2 root root 4096 1 19 10:36 jdk
  33. drwxr-xr-x. 2 root root 4096 1 20 14:28 mysql
  34. drwxr-xr-x. 2 root root 4096 10 31 2018 rh
  35. drwxr-xr-x. 3 root root 4096 1 19 14:24 tomcat
  36. [root@localhost boot]#
  37. #下面这条语句会报错,提示 DUMP: Only level 0 dumps are allowed on a subdirectory
  38. dump -1j -f /opt/etc.bak.bz2 /etc/
  39. [root@localhost boot]# dump -1j -f /opt/etc.bak.bz2 /etc/
  40. DUMP: Only level 0 dumps are allowed on a subdirectory
  41. DUMP: The ENTIRE dump is aborted.

如果是重要的备份文件, 比如数据区,建议将文件上传到其它服务器保存,不要将鸡蛋放在同一个篮子.

使用 restore 完成恢复

基本介绍

restore 命令用来恢复已备份的文件,可以从 dump 生成的备份文件中恢复原文件

restore 基本语法

  1. restore [模式选项] [选项]
  2. 说明下面四个模式, 不能混用,在一次命令中, 只能指定一种。
  3. -C :使用对比模式,将备份的文件与已存在的文件相互对比。
  4. -i:使用交互模式,在进行还原操作时,restors 指令将依序询问用户
  5. -r:进行还原模式
  6. -t : 查看模式,看备份文件有哪些文件
  7. 选项
  8. -f <备份设备>:从指定的文件中读取备份数据,进行还原操作

应用案例

restore 命令比较模式,比较备份文件和原文件的区别

  1. # 测试 将/boot中一个文件改名
  2. [root@localhost boot]# mv /boot/hello.java /boot/hello100.java
  3. [root@localhost boot]# ll
  4. 总用量 161604
  5. -rw-r--r--. 1 root root 153596 1 14 22:03 config-3.10.0-1160.53.1.el7.x86_64
  6. -rw-r--r--. 1 root root 151918 11 9 2018 config-3.10.0-957.el7.x86_64
  7. drwx------. 3 root root 4096 11 9 2018 efi
  8. drwxr-xr-x. 2 root root 4096 11 10 16:16 grub
  9. drwx------. 5 root root 4096 2 8 17:09 grub2
  10. -rw-r--r--. 1 root root 0 2 9 10:22 hello100.java
  11. -rw-------. 1 root root 74300933 11 10 16:20 initramfs-0-rescue-14b3cc7d82ff441abbb5736eddaf4973.img
  12. -rw-------. 1 root root 31383246 2 8 17:09 initramfs-3.10.0-1160.53.1.el7.x86_64.img
  13. -rw-------. 1 root root 31586932 11 10 16:21 initramfs-3.10.0-957.el7.x86_64.img
  14. drwx------. 2 root root 16384 11 10 16:13 lost+found
  15. -rw-r--r--. 1 root root 320671 1 14 22:04 symvers-3.10.0-1160.53.1.el7.x86_64.gz
  16. -rw-r--r--. 1 root root 314036 11 9 2018 symvers-3.10.0-957.el7.x86_64.gz
  17. -rw-------. 1 root root 3620596 1 14 22:03 System.map-3.10.0-1160.53.1.el7.x86_64
  18. -rw-------. 1 root root 3543471 11 9 2018 System.map-3.10.0-957.el7.x86_64
  19. -rwxr-xr-x. 1 root root 6639904 11 10 16:20 vmlinuz-0-rescue-14b3cc7d82ff441abbb5736eddaf4973
  20. -rwxr-xr-x. 1 root root 6773352 1 14 22:03 vmlinuz-3.10.0-1160.53.1.el7.x86_64
  21. -rwxr-xr-x. 1 root root 6639904 11 9 2018 vmlinuz-3.10.0-957.el7.x86_64
  22. # 进行比较
  23. [root@localhost boot]# restore -C -f /opt/boot.bak1.bz2
  24. Dump tape is compressed.
  25. Dump date: Wed Feb 9 10:24:03 2022
  26. Dumped from: Wed Feb 9 09:45:29 2022
  27. Level 1 dump of /boot on localhost.localdomain:/dev/sda1
  28. Label: none
  29. filesys = /boot
  30. restore: unable to stat ./hello.java: No such file or directory
  31. Some files were modified! 1 compare errors
  32. # 将文件名改回来
  33. [root@localhost boot]# mv /boot/hello100.java /boot/hello.java
  34. # 再次比较
  35. [root@localhost boot]# restore -C -f /opt/boot.bak1.bz2
  36. Dump tape is compressed.
  37. Dump date: Wed Feb 9 10:24:03 2022
  38. Dumped from: Wed Feb 9 09:45:29 2022
  39. Level 1 dump of /boot on localhost.localdomain:/dev/sda1
  40. Label: none
  41. filesys = /boot
  42. [root@localhost boot]#

restore 命令查看模式,看备份文件有哪些数据/文件

  1. [root@localhost boot]# restore -t -f /opt/boot.bak0.bz2
  2. Dump tape is compressed.
  3. Dump date: Wed Feb 9 09:45:29 2022
  4. Dumped from: the epoch
  5. Level 0 dump of /boot on localhost.localdomain:/dev/sda1
  6. Label: none
  7. 2 .
  8. 11 ./lost+found
  9. 12 ./efi
  10. ...
  11. 349 ./config-3.10.0-1160.53.1.el7.x86_64
  12. 350 ./symvers-3.10.0-1160.53.1.el7.x86_64.gz
  13. 351 ./vmlinuz-3.10.0-1160.53.1.el7.x86_64
  14. [root@localhost boot]#

restore 命令还原模式, 注意细节: 如果你有增量备份,需要把增量备份文件也进行恢复, 有几个增量备份文件, 就要恢复几个,按顺序来恢复即可

  1. # 建立临时目录
  2. [root@localhost boot]# mkdir /opt/boottmp
  3. [root@localhost boot]# cd /opt/boottmp/
  4. # //恢复到第 1 次完全备份状态
  5. [root@localhost boottmp]# restore -r -f /opt/boot.bak0.bz2
  6. Dump tape is compressed.
  7. [root@localhost boottmp]# ll
  8. 总用量 161736
  9. -rw-r--r--. 1 root root 153596 1 14 22:03 config-3.10.0-1160.53.1.el7.x86_64
  10. -rw-r--r--. 1 root root 151918 11 9 2018 config-3.10.0-957.el7.x86_64
  11. drwx------. 3 root root 4096 11 9 2018 efi
  12. drwxr-xr-x. 2 root root 4096 11 10 16:16 grub
  13. drwx------. 5 root root 4096 2 8 17:09 grub2
  14. -rw-------. 1 root root 74300933 11 10 16:20 initramfs-0-rescue-14b3cc7d82ff441abbb5736eddaf4973.img
  15. -rw-------. 1 root root 31383246 2 8 17:09 initramfs-3.10.0-1160.53.1.el7.x86_64.img
  16. -rw-------. 1 root root 31586932 11 10 16:21 initramfs-3.10.0-957.el7.x86_64.img
  17. drwx------. 2 root root 4096 11 10 16:13 lost+found
  18. -rw-------. 1 root root 145792 2 9 10:33 restoresymtable
  19. -rw-r--r--. 1 root root 320671 1 14 22:04 symvers-3.10.0-1160.53.1.el7.x86_64.gz
  20. -rw-r--r--. 1 root root 314036 11 9 2018 symvers-3.10.0-957.el7.x86_64.gz
  21. -rw-------. 1 root root 3620596 1 14 22:03 System.map-3.10.0-1160.53.1.el7.x86_64
  22. -rw-------. 1 root root 3543471 11 9 2018 System.map-3.10.0-957.el7.x86_64
  23. -rwxr-xr-x. 1 root root 6639904 11 10 16:20 vmlinuz-0-rescue-14b3cc7d82ff441abbb5736eddaf4973
  24. -rwxr-xr-x. 1 root root 6773352 1 14 22:03 vmlinuz-3.10.0-1160.53.1.el7.x86_64
  25. -rwxr-xr-x. 1 root root 6639904 11 9 2018 vmlinuz-3.10.0-957.el7.x86_64
  26. # 恢复到第 2 次增量备份状态
  27. [root@localhost boottmp]# restore -r -f /opt/boot.bak1.bz2
  28. Dump tape is compressed.
  29. [root@localhost boottmp]# ll
  30. 总用量 161736
  31. -rw-r--r--. 1 root root 153596 1 14 22:03 config-3.10.0-1160.53.1.el7.x86_64
  32. -rw-r--r--. 1 root root 151918 11 9 2018 config-3.10.0-957.el7.x86_64
  33. drwx------. 3 root root 4096 11 9 2018 efi
  34. drwxr-xr-x. 2 root root 4096 11 10 16:16 grub
  35. drwx------. 5 root root 4096 2 8 17:09 grub2
  36. -rw-r--r--. 1 root root 0 2 9 10:22 hello.java
  37. -rw-------. 1 root root 74300933 11 10 16:20 initramfs-0-rescue-14b3cc7d82ff441abbb5736eddaf4973.img
  38. -rw-------. 1 root root 31383246 2 8 17:09 initramfs-3.10.0-1160.53.1.el7.x86_64.img
  39. -rw-------. 1 root root 31586932 11 10 16:21 initramfs-3.10.0-957.el7.x86_64.img
  40. drwx------. 2 root root 4096 11 10 16:13 lost+found
  41. -rw-------. 1 root root 145872 2 9 10:33 restoresymtable
  42. -rw-r--r--. 1 root root 320671 1 14 22:04 symvers-3.10.0-1160.53.1.el7.x86_64.gz
  43. -rw-r--r--. 1 root root 314036 11 9 2018 symvers-3.10.0-957.el7.x86_64.gz
  44. -rw-------. 1 root root 3620596 1 14 22:03 System.map-3.10.0-1160.53.1.el7.x86_64
  45. -rw-------. 1 root root 3543471 11 9 2018 System.map-3.10.0-957.el7.x86_64
  46. -rwxr-xr-x. 1 root root 6639904 11 10 16:20 vmlinuz-0-rescue-14b3cc7d82ff441abbb5736eddaf4973
  47. -rwxr-xr-x. 1 root root 6773352 1 14 22:03 vmlinuz-3.10.0-1160.53.1.el7.x86_64
  48. -rwxr-xr-x. 1 root root 6639904 11 9 2018 vmlinuz-3.10.0-957.el7.x86_64
  49. [root@localhost boottmp]#

restore 命令恢复备份的文件,或者整个目录的文件

  1. [root@hspedu100 opt]# mkdir etctmp
  2. [root@hspedu100 opt]# cd etctmp/
  3. [root@hspedu100 etctmp]# restore -r -f /opt/etc.bak0.bz2
  4. # 建立临时目录
  5. [root@localhost boottmp]# mkdir /opt/etctmp
  6. [root@localhost boottmp]# cd /opt/etctmp/
  7. # 回复/etc目录
  8. [root@localhost etctmp]# restore -r -f /opt/etc.bak.bz2
  9. Dump tape is compressed.
  10. ./lost+found: (inode 11) not found on tape
  11. ./boot: (inode 131073) not found on tape
  12. ./dev: (inode 786433) not found on tape
  13. ./proc: (inode 262145) not found on tape
  14. ./run: (inode 393217) not found on tape
  15. ./sys: (inode 917505) not found on tape
  16. ./root: (inode 655361) not found on tape
  17. ./var: (inode 262146) not found on tape
  18. ./tmp: (inode 655362) not found on tape
  19. ./data: (inode 146668) not found on tape
  20. ./usr: (inode 393218) not found on tape
  21. ./bin: (inode 17) not found on tape
  22. ./sbin: (inode 16) not found on tape
  23. ./lib: (inode 13) not found on tape
  24. ./lib64: (inode 15) not found on tape
  25. ./home: (inode 131074) not found on tape
  26. ./media: (inode 786434) not found on tape
  27. ./mnt: (inode 917506) not found on tape
  28. ./opt: (inode 786435) not found on tape
  29. ./srv: (inode 14) not found on tape
  30. [root@localhost etctmp]# ll
  31. 总用量 2168
  32. drwxr-xr-x. 143 root root 12288 2 9 09:30 etc
  33. -rw-------. 1 root root 2205520 2 9 10:37 restoresymtable