KVM/QEMU在线备份(1)

举报
何其不顾四月天 发表于 2020/12/29 00:18:20 2020/12/29
【摘要】 文章目录 Incremental backup简介设备环境qemu 实时备份备份命令备份任务管理bitmap 命令远程备份创建存储结构创建iscsi目标保存退出其他 查看block信息 qemu4.2.0编译qemu-monitor使用 libvirt4.5gmpnettle-3.4libgnutls-3.5.18libvirt4.5错误处理日志模块日志模块 v...

Incremental backup

简介

最近接到一个调研任务,kvm 虚拟机增量盘的实时备份。主要参考资料来源。[QEMU/KVM磁盘在线备份]
主要思路:同步方式(sync)的四种模式。full,top,none,incremental四种方式的。

  • none 实时I/O备份,该方式主要为将实时的I/O变化备份,不足,将所有的I/O备份,浪费大量的内存空间,且只备份了I/O的数据,其他数据没有备份,该方案被放弃。
  • incremental 增量备份。但是无法实现实时备份。
  • 后来又想到另一种方式:在创建的虚拟机的时候,将增量盘挂载到服务器,实现远程备份。这种方法的缺点,对网络要求较高,性能,稳定性较高,一旦网络波动,虚拟机将奔溃。
  • Rsync的远程实时备份,暂时想到的一个思路,以后研究。rsync有多种远程上传方式。做个(标记)。

设备环境

客户端: Ubuntu18.04
服务端: Ubuntu20.04

  
 
  • 1
  • 2

qemu 实时备份

备份命令

  • none : real-time backup
virsh qemu-monitor-command DOMAIN '{ "execute" : "drive-backup" , "arguments" : { "device" : "drive-ide0-0-0" , "sync" : "none" , "target" : "/opt/backup/none.img" } }'

  
 
  • 1
  • bitmap : increment backup
virsh qemu-monitor-command DOMAIN '{ "execute" : "drive-backup" , "arguments" : { "device" : "drive-virtio-disk0" , "sync" : "incremental" , "bitmap" : "bitmap0" , "target" : "/opt/backup/inc.0.qcow2" } }'

  
 
  • 1

备份任务管理

  • 查看备份任务
virsh qemu-monitor-command DOMAIN --pretty '{ "execute": "query-block-jobs" }'

  
 
  • 1
virsh qemu-monitor-command DOMAIN --hmp 'info block-jobs'

  
 
  • 1
  • 暂停备份任务
virsh qemu-monitor-command DOMAIN '{ "execute": "block-job-cancel", "arguments": { "device": "drive-ide0-0-0", "force": true } }'

  
 
  • 1
  • 重置备份任务
virsh qemu-monitor-command DOMAIN '{ "execute" : "block-job-pause", "arguments" : { "device" : "drive-virtio-disk0" } }'

  
 
  • 1
  • 停止备份任务
virsh qemu-monitor-command DOMAIN '{ "execute": "block-job-cancel", "arguments": { "device": "drive-virtio-disk0", "force": true } }'

  
 
  • 1

bitmap 命令

  • 创建bitmap
virsh qemu-monitor-command DOMAIN { "execute": "block-dirty-bitmap-add",  "arguments": {"node": "drive0","name": "bitmap0" }}

  
 
  • 1
  • 删除bitmap
virsh qemu-monitor-command DOMAIN { "execute": "block-dirty-bitmap-remove", "arguments": {"node": "drive0","name": "bitmap0"}}

  
 
  • 1
  • 重置 bitmap node
virsh qemu-monitor-command DOMAIN { "execute": "block-dirty-bitmap-clear",  "arguments": {"node": "drive0","name": "bitmap0"}}

  
 
  • 1

远程备份

  • 命令
iscsi://[<username>[%<password>]@]<host>[:<port>]/<target-iqn-name>/<lun>

  
 
  • 1
iscsi://192.168.1.100:3260/iqn.2019-01.com.iaas/0

  
 
  • 1
  • 样例
virsh qemu-monitor-command DOMAIN '{ "execute" : "drive-backup" , "arguments" : { "device" : "drive-ide0-0-0" , "sync" : "incremental", "target" : "iscsi://192.168.1.100:3260/iqn.2019-01.com.iaas/0" } }'

  
 
  • 1

参考资料:
iscsi服务端搭建
iscsi客户端搭建

  • 客户端搭建

安装

apt install open-iscsi

  
 
  • 1

设置服务开机启动

systemctl  enable  iscsi.service
systemctl  enable  iscsid.service
systemctl  start  iscsi.service
systemctl  start  iscsid.service

  
 
  • 1
  • 2
  • 3
  • 4

iscsi-name

cat  /etc/iscsi/initiatorname.iscsi

  
 
  • 1

设置名称:

vim /etc/iscsi/initiatorname.iscsi
InitiatorName=name
systemctl  restart  iscsi.service

  
 
  • 1
  • 2
  • 3

查找target

iscsiadm  -m  discovery  -t  st  -p  172.26.106.103:3260

  
 
  • 1

登陆target

iscsiadm -m node --targetname iqn.2020-06.wuqiang.iscis -p 172.26.106.103:3260 --login

  
 
  • 1

设置全局单向认证

discovery.sendtargets.auth.authmethod = CHAP
discovery.sendtargets.auth.username = troila
discovery.sendtargets.auth.password = 123456

  
 
  • 1
  • 2
  • 3

重启服务

/etc/init.d/iscsid force-reload #强制重载
/etc/init.d/iscsid stop			#停止服务
/etc/init.d/iscsid start		#启动服务
service iscsi restart			#重启服务
service iscsid restart			#重启服务
service open-iscsi status #查看状态

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 服务端搭建

安装

apt install targetcli-fb

  
 
  • 1

进入配置页面

targetcli

  
 
  • 1

相关命令行

  - bookmarks action [bookmark] - cd [path] - clearconfig [confirm] - exit - get [group] [parameter...] - help [topic] - ls [path] [depth] - pwd - refresh - restoreconfig [savefile] [clear_existing] [target] [storage_object] - saveconfig [savefile] - sessions [action] [sid] - set [group] [parameter=value...] - status - version

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

创建存储结构

创建物理磁盘存储

cd backstores/block
create lun1 /dev/loop10

  
 
  • 1
  • 2

创建文件存储

cd /backstores/fileio/
create lun1 /opt/fileio.img

  
 
  • 1
  • 2

创建内存存储

cd ../ramdisk 
create rd0 10MB

  
 
  • 1
  • 2

查看创建好的存储资源

/backstores> ls
o- backstores ........................................................................................................... [...]
  o- block .............................................................................................. [Storage Objects: 1]
  | o- lun1 ...................................................................... [/dev/loop10 (50.0GiB) write-thru activated]
  |   o- alua ................................................................................................. [ALUA Groups: 1]
  | o- default_tg_pt_gp .................................................................... [ALUA state: Active/optimized]
  o- fileio .............................................................................................. [Storage Objects: 1]
  | o- lun1 ................................................................... [fileio.img (20.0GiB) write-back deactivated]
  |   o- alua ................................................................................................. [ALUA Groups: 1]
  | o- default_tg_pt_gp .................................................................... [ALUA state: Active/optimized]
  o- pscsi ................................................................................................ [Storage Objects: 0]
  o- ramdisk .............................................................................................. [Storage Objects: 0]

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

创建iscsi目标

cd /iscsi/
create #自动创建用户
create iqn.2020-06.iscsi:sn.20200623 #创建指定用户

  
 
  • 1
  • 2
  • 3

关联后端存储

cd iqn.2020-06.iscsi:sn.20200623/tpg1/luns/
create /backstores/block/lun1  #为后端存储路径

  
 
  • 1
  • 2

配置ACL(访问控制列表)

cd ../acls
create iqn.2020-06.wuqiang.iscis #客户端用户名

  
 
  • 1
  • 2

配置CHAP认证-全局单向认证

cd /iscsi/
get discovery_auth  #查看配置信息
set discovery_auth enable=1
set discovery_auth userid=troila
set discovery_auth password=123456
get discovery_auth enable userid password #查看是否与设置的相同

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

保存退出

exit

  
 
  • 1

其他

创建硬盘

dd if=/dev/vda5 of=/opt/block.img bs=1G count=50 #创建
sudo losetup /dev/loop10 /opt/block.img #挂载为设备 /dev/loop10
fdisk /dev/loop10 #创建逻辑分区
mkfs -t ext4 /dev/loop10 #格式化

  
 
  • 1
  • 2
  • 3
  • 4

创建文件

dd if=/dev/vda5 of=/opt/file.img bs=1G count=20 #创建
mkfs -t ext4 /opt/file.img #格式化

  
 
  • 1
  • 2

查看block信息

virsh qemu-monitor-command DOMAIN --hmp info block

  
 
  • 1
drive-ide0-0-0 (#block971): /opt/qcow2/wuqiang-151-extra.qcow2 (backup-top) Attached to: ide0-0-0 Cache mode: writeback Backing file: /opt/qcow2/wuqiang-151-extra.qcow2 (chain depth: 2)

  
 
  • 1
  • 2
  • 3
  • 4

drive-ide0-0-0 为 块信息名称

qemu4.2.0

编译

卸载之前安装的qemu

apt remove qemu*

  
 
  • 1

源码路径:

https://download.qemu.org/

  
 
  • 1

ubuntu编译

wget https://download.qemu.org/qemu-4.2.0.tar.xz
tar xvf qemu-4.2.0.tar.xz
cd qemu-4.2.0
./configure --prefix=/usr --target-list=x86_64-softmmu --enable-guest-agent --enable-libusb --enable-spice --enable-libiscsi 
make && make install

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

检测:

virsh
version

  
 
  • 1
  • 2

输出为如下所示:Running hypervisor: QEMU 4.2.0 则,安装成功

Compiled against library: libvirt 4.0.0
Using library: libvirt 4.0.0
Using API: QEMU 4.0.0
Running hypervisor: QEMU 4.2.0

  
 
  • 1
  • 2
  • 3
  • 4

注意:

./configure 配置完成之后,输出配置列表注意查看:agent,libiscsi,libusb,kvm,spice是否显示yes。

  
 
  • 1

qemu-monitor使用

进入monitor

qemu-system-x86_64 -enable-kvm -m 512 -smp 2 -hda vm-img-path -boot c -vnc :1 -monitor stdio

  
 
  • 1

dirve_backup介绍

help drive_backup

  
 
  • 1
drive_backup [-n] [-f] [-c] device target [format] -- initiates a point-in-time copy for a device. The device's contents are copied to the new image file, excluding data that is written after the command is started. The -n flag requests QEMU to reuse the image found in new-image-file, instead of recreating it from scratch. The -f flag requests QEMU to copy the whole disk, so that the result does not need a backing file. The -c flag requests QEMU to compress backup data (if the target format supports it).

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

样例:

drive_backup -f ide0-hd0 /opt/qcow2/151_backup.qcow2 qcow2

  
 
  • 1

查看备份任务:

info block-jobs

  
 
  • 1

查看device:

info block

  
 
  • 1

任务取消:

block_job_cancel [-f] device -- stop an active background block operation (use -f if you want to abort the operation immediately instead of keep running until data is in sync)

  
 
  • 1
  • 2
  • 3
block_job_cancel ide0-hd0

  
 
  • 1

libvirt4.5

gmp

wget https://gmplib.org/download/gmp/gmp-6.0.0.tar.xz
tar xvf gmp-6.0.0.tar.xz
cd gmp-6.0.0/
./configure && make && make install

  
 
  • 1
  • 2
  • 3
  • 4

nettle-3.4

wget ftp.gnu.org/gnu/nettle/nettle-3.4.tar.gz
tar xvf nettle-3.4.tar.gz
cd nettle-3.4 && mkdir build && cd build/
../configure --prefix=/usr --disable-openssl --enable-shared --enable-mini-gmp
make && make install

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

libgnutls-3.5.18

wget https://ftp.gnutls.org/gcrypt/gnutls/v3.5/gnutls-3.5.18.tar.xz
tar xvf gnutls-3.5.18.tar.xz
cd gnutls-3.5.18
./configure --enable-shared --with-included-libtasn1 --with-included-unistring --without-p11-kit  && make && make install

  
 
  • 1
  • 2
  • 3
  • 4

libvirt4.5

wdget https://libvirt.org/sources/libvirt-4.5.0.tar.xz
tar xvf libvirt-4.5.0.tar.xz
cd libvirt-4.5.0
./autogen.sh
./configure --prefix=/usr --without-apparmor-profiles --with--storage-iscsi --with-bash-completion --with-readline --with-remote
make && make install

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

依赖库下载

apt install libgmp-dev
apt install nettle-dev
apt install libnl-3-dev
apt install libnl-route-3-dev
apt install libxml2-dev
apt install libyajl-dev
apt install xsltproc
apt install libdevmapper-dev
apt install libpciaccess-dev
apt install uuid-dev
apt install libreadline-dev
apt install bash-completion

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

错误处理

1.错误一

virsh: /usr/lib/x86_64-linux-gnu/libvirt.so.0: version `LIBVIRT_4.4.0' not found (required by virsh)
virsh: /usr/lib/x86_64-linux-gnu/libvirt.so.0: version `LIBVIRT_4.5.0' not found (required by virsh)
virsh: /usr/lib/x86_64-linux-gnu/libvirt.so.0: version `LIBVIRT_PRIVATE_4.5.0' not found (required by virsh)

  
 
  • 1
  • 2
  • 3

解决方案

cd /usr/lib/x86_64-linux-gnu/
rm -rf libvirt.so.0*

  
 
  • 1
  • 2

2.错误二

virsh: /usr/lib/libvirt.so.0: version `LIBVIRT_PRIVATE_4.0.0' not found (required by /usr/lib/x86_64-linux-gnu/libvirt-lxc.so.0)
virsh: /usr/lib/libvirt.so.0: version `LIBVIRT_PRIVATE_4.0.0' not found (required by /usr/lib/x86_64-linux-gnu/libvirt-qemu.so.0)

  
 
  • 1
  • 2

解决方案:

rm -rf libvirt-lxc.so.0*
rm -rf libvirt-qemu.so.0*

  
 
  • 1
  • 2

日志模块

vim /etc/libvirt/libvirtd.conf
log_level = 3 #设置日志级别
log_outputs="3:file:/var/log/libvirt/libvirtd.log" #日志输出文件
/etc/init.d/libvirtd restart #重启服务

  
 
  • 1
  • 2
  • 3
  • 4

相关错误:

.so.0: version `LIBVIRT_PRIVATE_4.0.0' not found (required by /usr/lib/x86_64-linux-gnu/libvirt-lxc.so.0)
virsh: /usr/lib/libvirt.so.0: version `LIBVIRT_PRIVATE_4.0.0' not found (required by /usr/lib/x86_64-linux-gnu/libvirt-qemu.so.0)

  
 
  • 1
  • 2

解决方案:

rm -rf libvirt-lxc.so.0*
rm -rf libvirt-qemu.so.0*

  
 
  • 1
  • 2

日志模块

vim /etc/libvirt/libvirtd.conf
log_level = 3 #设置日志级别
log_outputs="3:file:/var/log/libvirt/libvirtd.log" #日志输出文件
/etc/init.d/libvirtd restart #重启服务

  
 
  • 1
  • 2
  • 3
  • 4

virt-manager创建虚拟机

相关错误:USB redirection is not supported by this version of QEMU
解决方案:移除USB redirection1,USB redirection2

  
 
  • 1
  • 2

文章来源: blog.csdn.net,作者:何其不顾四月天,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/u011218356/article/details/107006513

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。