dump和restore命令实现全备、增量备份和差异备份
差异备份,在全备的基础上做备份。因为tar命令不能实现差异备份,所以本文讲解如何用dump和restore命令实现。当然dump和restore命令也可以实现全备和增量备份。
一 创建备份目录 分区 挂载
[plain]
[root@serv01 data]# mkdir /backup
[root@serv01 data]# fdisk /dev/sdb
[root@serv01 data]# fdisk /dev/sdc
[root@serv01 data]# mkfs.ext4 /dev/sdb1
[root@serv01 data]# mkfs.ext4 /dev/sdc1
[root@serv01 data]# mount /dev/sdb1 /data
[root@serv01 data]# mount /dev/sdc1/backup/
二 拷贝文件、修改配置文件
[plain]
#拷贝文件
[root@serv01 data]# rm -rf *
[root@serv01 data]# cp /boot/* .
cp: omitting directory `/boot/efi'
cp: omitting directory `/boot/grub'
cp: omitting directory `/boot/lost+found'
#向fstab文件追加内容
[root@serv01 data]# echo "/dev/sdb1/data ext4 defaults 1 2" >> /etc/fstab
[root@serv01 data]# echo "/dev/sdc1/backup ext4 defaults 1 2" >> /etc/fstab
三 dump和restore——实现全备
[plain]
[root@serv01 data]# yum install dump -y
#dump:支持增量备份和差异备份,只能对单独的分区进行备份
#0:全备
#u:把/etc/dumpdates更新
#f:备份后的名字
[root@serv01 data]# cd /backup/
[root@serv01 backup]# ls
lost+found
[root@serv01 backup]# rm -rf lost+found/
#第一次查看,发现dumpdates没有任何内容
[root@serv01 backup]# ls /etc/dumpdates
/etc/dumpdates
#备份
[root@serv01 backup]# dump -0uf data01.dump/data/
DUMP: Date of this level 0 dump: Fri Aug 2 18:57:53 2013
DUMP: Dumping /dev/sdb1 (/data) to data01.dump
DUMP: Label: none
DUMP: Writing 10 Kilobyte records
DUMP: mapping (Pass I) [regular files]
DUMP: mapping (Pass II) [directories]
DUMP: estimated 59 blocks.
DUMP: Volume 1 started with block 1 at: Fri Aug 2 18:57:53 2013
DUMP: dumping (Pass III) [directories]
DUMP: dumping (Pass IV) [regular files]
DUMP: Closing data01.dump
DUMP: Volume 1 completed at: Fri Aug 2 18:57:53 2013
DUMP: Volume 1 50 blocks (0.05MB)
DUMP: 50 blocks (0.05MB) on 1 volume(s)
DUMP: finished in less than a second
DUMP: Date of this level 0 dump: Fri Aug 2 18:57:53 2013
DUMP: Date this dump completed: Fri Aug 2 18:57:53 2013
DUMP: Average transfer rate: 0 kB/s
DUMP: DUMP IS DONE
[root@serv01 backup]# ll
total 52
-rw-r--r—. 1 root root 51200 Aug 2 18:57 data01.dump
#查看dumpdates文件,发现已经更新了
[root@serv01 backup]# cat /etc/dumpdates
/dev/sdb1 0 Fri Aug 2 18:57:53 2013 +0800
#还原,数据没有了,重新备份
[root@serv01 backup]# cd /data
[root@serv01 data]# ls
lost+found
[root@serv01 data]# rm -rf *
[root@serv01 data]# cp /boot/* ./
cp: omitting directory `/boot/efi'
cp: omitting directory `/boot/grub'
cp: omitting directory `/boot/lost+found'
[root@serv01 data]# cd /backup/
[root@serv01 backup]# ls
data01.dump
[root@serv01 backup]# rm -rf *
[root@serv01 backup]# ls
[root@serv01 backup]# > /etc/dumpdates
[root@serv01 backup]# dump -0uf dump01.dump/data/
DUMP: Date of this level 0 dump: Fri Aug 2 19:02:20 2013
DUMP: Dumping /dev/sdb1 (/data) to dump01.dump
DUMP: Label: none
DUMP: Writing 10 Kilobyte records
DUMP: mapping (Pass I) [regular files]
DUMP: mapping (Pass II) [directories]
DUMP: estimated 18674 blocks.
DUMP: Volume 1 started with block 1 at: Fri Aug 2 19:02:20 2013
DUMP: dumping (Pass III) [directories]
DUMP: dumping (Pass IV) [regular files]
DUMP: Closing dump01.dump
DUMP: Volume 1 completed at: Fri Aug 2 19:02:20 2013
DUMP: Volume 1 18670 blocks (18.23MB)
DUMP: 18670 blocks (18.23MB) on 1 volume(s)
DUMP: finished in less than a second
DUMP: Date of this level 0 dump: Fri Aug 2 19:02:20 2013
DUMP: Date this dump completed: Fri Aug 2 19:02:20 2013
DUMP: Average transfer rate: 0 kB/s
DUMP: DUMP IS DONE
#进入data目录,发现有数据
[root@serv01 backup]# cd /data
[root@serv01 data]# ls
config-2.6.32-131.0.15.el6.x86_64 symvers-2.6.32-131.0.15.el6.x86_64.gz vmlinuz-2.6.32-131.0.15.el6.x86_64
initramfs-2.6.32-131.0.15.el6.x86_64.img System.map-2.6.32-131.0.15.el6.x86_64
#全部删除,模拟数据丢失
[root@serv01 data]# rm -rf *
[root@serv01 data]# ls
#恢复数据
[root@serv01 data]# restore -rf/backup/dump01.dump
#恢复成功
[root@serv01 data]# ls
config-2.6.32-131.0.15.el6.x86_64 restoresymtable System.map-2.6.32-131.0.15.el6.x86_64
initramfs-2.6.32-131.0.15.el6.x86_64.img symvers-2.6.32-131.0.15.el6.x86_64.gz vmlinuz-2.6.32-131.0.15.el6.x86_64
[root@serv01 data]# cp /etc/passwd ./
[root@serv01 data]# ls
config-2.6.32-131.0.15.el6.x86_64 passwd symvers-2.6.32-131.0.15.el6.x86_64.gz vmlinuz-2.6.32-131.0.15.el6.x86_64
initramfs-2.6.32-131.0.15.el6.x86_64.img restoresymtable System.map-2.6.32-131.0.15.el6.x86_64
[root@serv01 data]# cd /backup/
四 dump和restore——实现增量备份
[plain]
#增量备份:1-9
#dump:备份磁盘上的块,不止是文件;只能备份单独的分区
[root@serv01 data]# rm -rf *
[root@serv01 data]# cp /boot/* .
cp: omitting directory `/boot/efi'
cp: omitting directory `/boot/grub'
cp: omitting directory `/boot/lost+found'
[root@serv01 data]# cd /backup/
[root@serv01 backup]# ll
total 18672
-rw-r--r--. 1 root root 19118080 Aug 2 2013dump01.dump
[root@serv01 backup]# rm -rf dump01.dump
#第一次备份:全备
[root@serv01 backup]# dump -0uf data01.dump/data/
DUMP: Date of this level 0 dump: Fri Aug 2 11:25:57 2013
DUMP: Dumping /dev/sdb1 (/data) to data01.dump
DUMP: Label: none
DUMP: Writing 10 Kilobyte records
DUMP: mapping (Pass I) [regular files]
DUMP: mapping (Pass II) [directories]
DUMP: estimated 18674 blocks.
DUMP: Volume 1 started with block 1 at: Fri Aug 2 11:25:57 2013
DUMP: dumping (Pass III) [directories]
DUMP: dumping (Pass IV) [regular files]
DUMP: Closing data01.dump
DUMP: Volume 1 completed at: Fri Aug 2 11:25:57 2013
DUMP: Volume 1 18670 blocks (18.23MB)
DUMP: 18670 blocks (18.23MB) on 1 volume(s)
DUMP: finished in less than a second
DUMP: Date of this level 0 dump: Fri Aug 2 11:25:57 2013
DUMP: Date this dump completed: Fri Aug 2 11:25:57 2013
DUMP: Average transfer rate: 0 kB/s
DUMP: DUMP IS DONE
[root@serv01 backup]# ll
total 18672
-rw-r—r—. 1 root root 19118080 Aug 2 11:25 data01.dump
#拷贝文件到data目录
#第二次备份:增量备份
[root@serv01 backup]# cp /etc/inittab/data/
[root@serv01 backup]# dump -1uf data02.dump/data/
DUMP: Date of this level 1 dump: Fri Aug 2 11:26:28 2013
DUMP: Date of last level 0 dump: Fri Aug 2 11:25:57 2013
DUMP: Dumping /dev/sdb1 (/data) to data02.dump
DUMP: Label: none
DUMP: Writing 10 Kilobyte records
DUMP: mapping (Pass I) [regular files]
DUMP: mapping (Pass II) [directories]
DUMP: estimated 55 blocks.
DUMP: Volume 1 started with block 1 at: Fri Aug 2 11:26:28 2013
DUMP: dumping (Pass III) [directories]
DUMP: dumping (Pass IV) [regular files]
DUMP: Closing data02.dump
DUMP: Volume 1 completed at: Fri Aug 2 11:26:28 2013
DUMP: Volume 1 50 blocks (0.05MB)
DUMP: 50 blocks (0.05MB) on 1 volume(s)
DUMP: finished in less than a second
DUMP: Date of this level 1 dump: Fri Aug 2 11:26:28 2013
DUMP: Date this dump completed: Fri Aug 2 11:26:28 2013
DUMP: Average transfer rate: 0 kB/s
DUMP: DUMP IS DONE
[root@serv01 backup]# ll
total 18724
-rw-r--r--. 1 root root 19118080 Aug 2 11:25 data01.dump
-rw-r--r—. 1 root root 51200 Aug 2 11:26 data02.dump
#再次拷贝文件
[root@serv01 backup]# cp /etc/passwd /data/
#第三次备份:增量备份
[root@serv01 backup]# dump -2uf data03.dump/data/
DUMP: Date of this level 2 dump: Fri Aug 2 11:27:07 2013
DUMP: Date of last level 1 dump: Fri Aug 2 11:26:28 2013
DUMP: Dumping /dev/sdb1 (/data) to data03.dump
DUMP: Label: none
DUMP: Writing 10 Kilobyte records
DUMP: mapping (Pass I) [regular files]
DUMP: mapping (Pass II) [directories]
DUMP: estimated 55 blocks.
DUMP: Volume 1 started with block 1 at: Fri Aug 2 11:27:07 2013
DUMP: dumping (Pass III) [directories]
DUMP: dumping (Pass IV) [regular files]
DUMP: Closing data03.dump
DUMP: Volume 1 completed at: Fri Aug 2 11:27:07 2013
DUMP: Volume 1 50 blocks (0.05MB)
DUMP: 50 blocks (0.05MB) on 1 volume(s)
DUMP: finished in less than a second
DUMP: Date of this level 2 dump: Fri Aug 2 11:27:07 2013
DUMP: Date this dump completed: Fri Aug 2 11:27:07 2013
DUMP: Average transfer rate: 0 kB/s
DUMP: DUMP IS DONE
[root@serv01 backup]# cat /etc/dumpdates
/dev/sdb1 0 Fri Aug 2 11:25:57 2013 +0800
/dev/sdb1 1 Fri Aug 2 11:26:28 2013 +0800
/dev/sdb1 2 Fri Aug 2 11:27:07 2013 +0800
#第四次备份:增量备份
[root@serv01 backup]# dump -3uf data04.dump/data/
DUMP: Date of this level 3 dump: Fri Aug 2 11:28:48 2013
DUMP: Date of last level 2 dump: Fri Aug 2 11:27:07 2013
DUMP: Dumping /dev/sdb1 (/data) to data04.dump
DUMP: Label: none
DUMP: Writing 10 Kilobyte records
DUMP: mapping (Pass I) [regular files]
DUMP: mapping (Pass II) [directories]
DUMP: estimated 50 blocks.
DUMP: Volume 1 started with block 1 at: Fri Aug 2 11:28:48 2013
DUMP: dumping (Pass III) [directories]
DUMP: dumping (Pass IV) [regular files]
DUMP: Closing data04.dump
DUMP: Volume 1 completed at: Fri Aug 2 11:28:48 2013
DUMP: Volume 1 40 blocks (0.04MB)
DUMP: 40 blocks (0.04MB) on 1 volume(s)
DUMP: finished in less than a second
DUMP: Date of this level 3 dump: Fri Aug 2 11:28:48 2013
DUMP: Date this dump completed: Fri Aug 2 11:28:48 2013
DUMP: Average transfer rate: 0 kB/s
DUMP: DUMP IS DONE
[root@serv01 backup]# cat /etc/dumpdates
/dev/sdb1 0 Fri Aug 2 11:25:57 2013 +0800
/dev/sdb1 1 Fri Aug 2 11:26:28 2013 +0800
/dev/sdb1 2 Fri Aug 2 11:27:07 2013 +0800
/dev/sdb1 3 Fri Aug 2 11:28:48 2013 +0800
#进入data目录,模拟数据丢失
[root@serv01 backup]# cd /data
[root@serv01 data]# ls
config-2.6.32-131.0.15.el6.x86_64 inittab symvers-2.6.32-131.0.15.el6.x86_64.gz
initramfs-2.6.32-131.0.15.el6.x86_64.img passwd vmlinuz-2.6.32-131.0.15.el6.x86_64
[root@serv01 data]# rm -rf *
[root@serv01 data]# ls
#恢复数据:一级一级地恢复
[root@serv01 data]# restore -rf/backup/data01.dump
[root@serv01 data]# ls
config-2.6.32-131.0.15.el6.x86_64 restoresymtable System.map-2.6.32-131.0.15.el6.x86_64
initramfs-2.6.32-131.0.15.el6.x86_64.img symvers-2.6.32-131.0.15.el6.x86_64.gz vmlinuz-2.6.32-131.0.15.el6.x86_64
[root@serv01 data]# restore -rf/backup/data03.dump
restore: Incremental tape too low
[root@serv01 data]# ls
config-2.6.32-131.0.15.el6.x86_64 restoresymtable System.map-2.6.32-131.0.15.el6.x86_64
initramfs-2.6.32-131.0.15.el6.x86_64.img symvers-2.6.32-131.0.15.el6.x86_64.gz vmlinuz-2.6.32-131.0.15.el6.x86_64
[root@serv01 data]# restore -rf/backup/data02.dump
[root@serv01 data]# restore -rf/backup/data03.dump
[root@serv01 data]# restore -rf/backup/data04.dump
[root@serv01 data]# ls
config-2.6.32-131.0.15.el6.x86_64 inittab restoresymtable vmlinuz-2.6.32-131.0.15.el6.x86_64
initramfs-2.6.32-131.0.15.el6.x86_64.img passwd symvers-2.6.32-131.0.15.el6.x86_64.gz
#恢复时报错:
#1.恢复的数据不对:查看挂载是否正确
#2.restore: Incremental tape too high 同一级别的备份了多次,所以会报这个错误,检查/etc/dumpdates文件和拼写是否错误
五 dump和restore——实现差异备份
[plain]
#演示差异备份
[root@serv01 data]# cd /backup/
[root@serv01 backup]# rm -rf *
[root@serv01 backup]# rm -rf data01.dump
[root@serv01 backup]# > /etc/dumpdates
#首先全备
[root@serv01 backup]# dump -0uf data01.dump/data/
DUMP: Date of this level 0 dump: Fri Aug 2 11:43:51 2013
DUMP: Dumping /dev/sdb1 (/data) to data01.dump
DUMP: Label: none
DUMP: Writing 10 Kilobyte records
DUMP: mapping (Pass I) [regular files]
DUMP: mapping (Pass II) [directories]
DUMP: estimated 16672 blocks.
DUMP: Volume 1 started with block 1 at: Fri Aug 2 11:43:51 2013
DUMP: dumping (Pass III) [directories]
DUMP: dumping (Pass IV) [regular files]
DUMP: Closing data01.dump
DUMP: Volume 1 completed at: Fri Aug 2 11:43:51 2013
DUMP: Volume 1 16680 blocks (16.29MB)
DUMP: 16680 blocks (16.29MB) on 1 volume(s)
DUMP: finished in less than a second
DUMP: Date of this level 0 dump: Fri Aug 2 11:43:51 2013
DUMP: Date this dump completed: Fri Aug 2 11:43:51 2013
DUMP: Average transfer rate: 0 kB/s
DUMP: DUMP IS DONE
[root@serv01 backup]# cat /etc/dumpdates
/dev/sdb1 0 Fri Aug 2 11:43:51 2013 +0800
#拷贝fstab文件
[root@serv01 backup]# cp /etc/fstab /data/
#差异备份
[root@serv01 backup]# dump -1uf data02.dump/data/
DUMP: Date of this level 1 dump: Fri Aug 2 11:44:20 2013
DUMP: Date of last level 0 dump: Fri Aug 2 11:43:51 2013
DUMP: Dumping /dev/sdb1 (/data) to data02.dump
DUMP: Label: none
DUMP: Writing 10 Kilobyte records
DUMP: mapping (Pass I) [regular files]
DUMP: mapping (Pass II) [directories]
DUMP: estimated 55 blocks.
DUMP: Volume 1 started with block 1 at: Fri Aug 2 11:44:20 2013
DUMP: dumping (Pass III) [directories]
DUMP: dumping (Pass IV) [regular files]
DUMP: Closing data02.dump
DUMP: Volume 1 completed at: Fri Aug 2 11:44:20 2013
DUMP: Volume 1 50 blocks (0.05MB)
DUMP: 50 blocks (0.05MB) on 1 volume(s)
DUMP: finished in less than a second
DUMP: Date of this level 1 dump: Fri Aug 2 11:44:20 2013
DUMP: Date this dump completed: Fri Aug 2 11:44:20 2013
DUMP: Average transfer rate: 0 kB/s
DUMP: DUMP IS DONE
#拷贝yum.conf文件
[root@serv01 backup]# cp /etc/yum.conf/data
[root@serv01 backup]# dump -1ufdata021.dump /data/
DUMP: Date of this level 1 dump: Fri Aug 2 11:44:47 2013
DUMP: Date of last level 0 dump: Fri Aug 2 11:43:51 2013
DUMP: Dumping /dev/sdb1 (/data) to data021.dump
DUMP: Label: none
DUMP: Writing 10 Kilobyte records
DUMP: mapping (Pass I) [regular files]
DUMP: mapping (Pass II) [directories]
DUMP: estimated 60 blocks.
DUMP: Volume 1 started with block 1 at: Fri Aug 2 11:44:47 2013
DUMP: dumping (Pass III) [directories]
DUMP: dumping (Pass IV) [regular files]
DUMP: Closing data021.dump
DUMP: Volume 1 completed at: Fri Aug 2 11:44:47 2013
DUMP: Volume 1 60 blocks (0.06MB)
DUMP: 60 blocks (0.06MB) on 1 volume(s)
DUMP: finished in less than a second
DUMP: Date of this level 1 dump: Fri Aug 2 11:44:47 2013
DUMP: Date this dump completed: Fri Aug 2 11:44:47 2013
DUMP: Average transfer rate: 0 kB/s
DUMP: DUMP IS DONE
#拷贝文件,然后差异备份
[root@serv01 backup]# cp /etc/networks/data
[root@serv01 backup]# dump -1ufdata0211.dump /data/
DUMP: Date of this level 1 dump: Fri Aug 2 11:45:34 2013
DUMP: Date of last level 0 dump: Fri Aug 2 11:43:51 2013
DUMP: Dumping /dev/sdb1 (/data) to data0211.dump
DUMP: Label: none
DUMP: Writing 10 Kilobyte records
DUMP: mapping (Pass I) [regular files]
DUMP: mapping (Pass II) [directories]
DUMP: estimated 65 blocks.
DUMP: Volume 1 started with block 1 at: Fri Aug 2 11:45:34 2013
DUMP: dumping (Pass III) [directories]
DUMP: dumping (Pass IV) [regular files]
DUMP: Closing data0211.dump
DUMP: Volume 1 completed at: Fri Aug 2 11:45:34 2013
DUMP: Volume 1 70 blocks (0.07MB)
DUMP: 70 blocks (0.07MB) on 1 volume(s)
DUMP: finished in less than a second
DUMP: Date of this level 1 dump: Fri Aug 2 11:45:34 2013
DUMP: Date this dump completed: Fri Aug 2 11:45:34 2013
DUMP: Average transfer rate: 0 kB/s
DUMP: DUMP IS DONE
#模拟数据删除
[root@serv01 backup]# cd /data
[root@serv01 data]# rm -rf *
#恢复时需要有全备的文件和想恢复的文件(或者最后一次),只需要两个
[root@serv01 data]# restore -rf/backup/data01.dump
[root@serv01 data]# restore -rf/backup/data021.dump
[root@serv01 data]# ls
config-2.6.32-131.0.15.el6.x86_64 inittab symvers-2.6.32-131.0.15.el6.x86_64.gz
fstab passwd vmlinuz-2.6.32-131.0.15.el6.x86_64
initramfs-2.6.32-131.0.15.el6.x86_64.img restoresymtable yum.conf
[root@serv01 data]# rm -rf *
[root@serv01 data]# restore -rf/backup/data01.dump
[root@serv01 data]# restore -rf/backup/data0211.dump
[root@serv01 data]# ls
config-2.6.32-131.0.15.el6.x86_64 inittab restoresymtable yum.conf
fstab networks symvers-2.6.32-131.0.15.el6.x86_64.gz
initramfs-2.6.32-131.0.15.el6.x86_64.img passwd vmlinuz-2.6.32-131.0.15.el6.x86_64
#差异备份:一定是在全备的基础上
#dd没有增量备份的功能
#磁盘拷贝(对拷)
[root@serv01 backup]# cd /data/
[root@serv01 data]# ls
[root@serv01 data]# cp /boot/* .
cp: omitting directory `/boot/efi'
cp: omitting directory `/boot/grub'
cp: omitting directory `/boot/lost+found'
[root@serv01 data]# dd if=/dev/sdb1of=/dev/sdc1
dd: writing to `/dev/sdc1': Input/outputerror
4192897+0 records in
4192896+0 records out
2146762752 bytes (2.1 GB) copied, 71.3082s, 30.1 MB/s
[root@serv01 data]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 1.1G 8.1G 12% /
tmpfs 188M 0 188M 0% /dev/shm
/dev/sda1 194M 25M 160M 14% /boot
/dev/sda5 4.0G 137M 3.7G 4% /opt
/dev/sr0 3.4G 3.4G 0 100% /iso
/dev/sdb1 2.0G 54M 1.9G 3% /data
/dev/sdc1 2.0G 35M 1.9G 2% /backup
[root@serv01 data]# ls /backup/
config-2.6.32-131.0.15.el6.x86_64 symvers-2.6.32-131.0.15.el6.x86_64.gz vmlinuz-2.6.32-131.0.15.el6.x86_64
initramfs-2.6.32-131.0.15.el6.x86_64.img System.map-2.6.32-131.0.15.el6.x86_64