数据备份与数据恢复

数据备份dump

常用参数

参数 说明
-[0-9] 备份层级
-f 指定备份后的文件名
-j 调用bzlib库压缩备份文件,后缀为bz2格式
-T 指定开始备份的日期与时间
-u 备份完毕后,在/etc/dumpdates中记录备份的文件系统、日期与时间
-t 指定文件名
-W 显示需要备份的文件及其最后一次备份的层级、时间、日期
-w 与-W类似,但仅显示需要备份的文件

常用案例

  • 备份分区支持增量备份
  • 备份目录或文件不支持增量备份只能使用0级别备份
  1. [root@localhost ~]# yum install dump
  2. [root@localhost ~]# dump -0uj -f /opt/disk.bak0.bz2 /mnt/disk1/
  3. DUMP: Date of this level 0 dump: Sun May 2 18:58:47 2021
  4. DUMP: Dumping /dev/sdb1 (/mnt/disk1) to /opt/disk.bak0.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 347 blocks.
  11. DUMP: Volume 1 started with block 1 at: Sun May 2 18:58:47 2021
  12. DUMP: dumping (Pass III) [directories]
  13. DUMP: dumping (Pass IV) [regular files]
  14. DUMP: Closing /opt/disk.bak0.bz2
  15. DUMP: Volume 1 completed at: Sun May 2 18:58:47 2021
  16. DUMP: 330 blocks (0.32MB) on 1 volume(s)
  17. DUMP: finished in less than a second
  18. DUMP: Date of this level 0 dump: Sun May 2 18:58:47 2021
  19. DUMP: Date this dump completed: Sun May 2 18:58:47 2021
  20. DUMP: Average transfer rate: 0 kB/s
  21. DUMP: Wrote 330kB uncompressed, 11kB compressed, 30.000:1
  22. DUMP: DUMP IS DONE
  23. [root@localhost ~]# echo "hello world" > /mnt/disk1/hello.txt
  24. [root@localhost ~]# dump -1uj -f /opt/disk.bak1.bz2 /mnt/disk1/
  25. DUMP: DUMP IS DONE
  26. [root@localhost ~]# ll /opt/
  27. total 28
  28. -rw-r--r--. 1 root root 12226 May 2 18:58 disk.bak0.bz2
  29. -rw-r--r--. 1 root root 12820 May 2 18:59 disk.bak1.bz2
  30. [root@localhost ~]# cat /etc/dumpdates
  31. /dev/sdb1 0 Sun May 2 18:58:47 2021 +0800
  32. /dev/sdb1 1 Sun May 2 18:59:47 2021 +0800
  33. [root@localhost ~]# dump -W
  34. Last dump(s) done (Dump '>' file systems):
  35. /dev/sdb1 (/mnt/disk1) Last dump: Level 1, Date Sun May 2 18:59:47 2021

数据恢复restore

常用参数

参数 说明
-C 使用对比模式,将备份的文件与已存在的文件相互对比
-i 使用交互模式,在进行还原操作时,restore指令将依次询问用户
-r 进行还原模式
-t 查看模式,看备份文件有哪些文件
-f 从指定的文件中读取备份数据,进行还原操作

常用案例

  1. #对比备份文件与现有文件的异同
  2. [root@localhost opt]# mv /mnt/disk1/hello.txt /mnt/disk1/hello.md
  3. [root@localhost opt]# restore -C -f disk.bak1.bz2
  4. Dump tape is compressed.
  5. Dump date: Sun May 2 18:59:47 2021
  6. Dumped from: Sun May 2 18:58:47 2021
  7. Level 1 dump of /mnt/disk1 on localhost.localdomain:/dev/sdb1
  8. Label: none
  9. filesys = /mnt/disk1
  10. restore: unable to stat ./hello.txt: No such file or directory
  11. Some files were modified! 1 compare errors
  12. [root@localhost opt]# mv /mnt/disk1/hello.md /mnt/disk1/hello.txt
  13. [root@localhost opt]# restore -C -f disk.bak1.bz2
  14. Dump tape is compressed.
  15. Dump date: Sun May 2 18:59:47 2021
  16. Dumped from: Sun May 2 18:58:47 2021
  17. Level 1 dump of /mnt/disk1 on localhost.localdomain:/dev/sdb1
  18. Label: none
  19. filesys = /mnt/disk1
  20. #查看备份文件的内容有哪些
  21. [root@localhost opt]# restore -t -f disk.bak0.bz2
  22. Dump tape is compressed.
  23. Dump date: Sun May 2 18:58:47 2021
  24. Dumped from: the epoch
  25. Level 0 dump of /mnt/disk1 on localhost.localdomain:/dev/sdb1
  26. Label: none
  27. 2 .
  28. 11 ./lost+found
  29. [root@localhost opt]# restore -t -f disk.bak1.bz2
  30. Dump tape is compressed.
  31. Dump date: Sun May 2 18:59:47 2021
  32. Dumped from: Sun May 2 18:58:47 2021
  33. Level 1 dump of /mnt/disk1 on localhost.localdomain:/dev/sdb1
  34. Label: none
  35. 2 .
  36. 12 ./hello.txt
  37. #恢复备份的文件,注意如果有增量备份需按照顺序进行恢复<时间从早到晚>
  38. [root@localhost ~]# mkdir /opt/restore
  39. [root@localhost ~]# cd /opt/restore/
  40. [root@localhost restore]# restore -r -f /opt/disk.bak0.bz2
  41. Dump tape is compressed.
  42. [root@localhost restore]# ll
  43. total 2064
  44. drwx------. 2 root root 6 Mar 21 21:20 lost+found
  45. -rw-------. 1 root root 2110472 May 2 21:35 restoresymtable
  46. [root@localhost restore]# restore -r -f /opt/disk.bak1.bz2
  47. Dump tape is compressed.
  48. [root@localhost restore]# ll
  49. total 2068
  50. -rw-r--r--. 1 root root 12 May 2 18:59 hello.txt
  51. drwx------. 2 root root 6 Mar 21 21:20 lost+found
  52. -rw-------. 1 root root 2110552 May 2 21:35 restoresymtable