使用tar和rsync创建KVM实例的自动备份,并把它存储到远端服务器上。

执行如下步骤:

(1)创建备份的目录并切换到该目录

root@kvm:~# mkdir backup_kvm1 && cd backup_kvm1
root@kvm:~/backup_kvm1#

(2)找到KVM客户机的镜像文件位置

root@kvm:~/backup_kvm1# virsh dumpxml kvm1 | grep "source file"
 <source file='/var/lib/libvirt/images/kvm1.img'/>

(3)保存当前实例的配置文件到磁盘

root@kvm:~/backup_kvm1# virsh dumpxml kvm1 &gt; kvm1.xml

(4)停止KVM的虚拟机,并复制镜像文件到备份目录

root@kvm:~/backup_kvm1# virsh destroy kvm1
Domain kvm1 destroyed
root@kvm:~/backup_kvm1# cp /var/lib/libvirt/images/kvm1.img .
root@kvm:~/backup_kvm1# ls -lah
total 2.4G
drwxr-xr-x 2 root root 4.0K Apr 20 18:37 .
drwx------ 7 root root 4.0K Apr 20 18:36 ..
-rwxr-xr-x 1 root root 8.0G Apr 20 18:37 kvm1.img
-rw-r--r-- 1 root root 3.0K Apr 20 18:36 kvm1.xml

(5)针对VM的配置和镜像文件进行归档

root@kvm:~/backup_kvm1# tar jcvf kvm1_backup.tar.bz .
./
./kvm1.img
./kvm1.xml
root@kvm:~/backup_kvm1# rm kvm1.img kvm1.xml

(6)传送归档的文件到远端服务器

root@kvm:~/backup_kvm1# rsync -vaz kvm1_backup.tar.bz kvm2:/tmp
sending incremental file list
kvm1_backup.tar.bz
sent 842,977,610 bytes received 35 bytes 26,761,195.08 bytes/sec
total size is 845,671,214 speedup is 1.00

(7)登陆到远端服务器和解压归档文件,并进行还原。

root@kvm2:~# cd /tmp/
root@kvm2:/tmp# tar jxfv kvm1_backup.tar.bz
./
./kvm1.img
./kvm1.xml

(8)复制镜像文件到配置的路径和定义这个实例

root@kvm2:/tmp# cp kvm1.img /var/lib/libvirt/images/
root@kvm2:/tmp# virsh define kvm1.xml
Domain kvm1 defined from kvm1.xml
root@kvm2:/tmp# virsh list --all | grep kvm1
 - kvm1 shut off