KVM/QEMU在线备份(1)

2023-03-10 13:22:42 浏览数 (2)

文章目录

  • Incremental backup
    • 简介
    • 设备环境
    • qemu 实时备份
      • 备份命令
      • 备份任务管理
      • bitmap 命令
      • 远程备份
        • 创建存储结构
        • 创建iscsi目标
        • 保存退出
        • 其他
      • 查看block信息
    • qemu4.2.0
      • 编译
      • qemu-monitor使用
    • libvirt4.5
      • gmp
      • nettle-3.4
      • libgnutls-3.5.18
      • libvirt4.5
      • 错误处理
      • 日志模块
      • 日志模块
    • virt-manager创建虚拟机

Incremental backup

简介

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

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

设备环境

代码语言:javascript复制
客户端: Ubuntu18.04
服务端: Ubuntu20.04

qemu 实时备份

备份命令

  • none : real-time backup
代码语言:javascript复制
virsh qemu-monitor-command DOMAIN '{ "execute" : "drive-backup" , "arguments" : { "device" : "drive-ide0-0-0" , "sync" : "none" , "target" : "/opt/backup/none.img" } }'
  • bitmap : increment backup
代码语言:javascript复制
virsh qemu-monitor-command DOMAIN '{ "execute" : "drive-backup" , "arguments" : { "device" : "drive-virtio-disk0" , "sync" : "incremental" , "bitmap" : "bitmap0" , "target" : "/opt/backup/inc.0.qcow2" } }'

备份任务管理

  • 查看备份任务
代码语言:javascript复制
virsh qemu-monitor-command DOMAIN --pretty '{ "execute": "query-block-jobs" }'
代码语言:javascript复制
virsh qemu-monitor-command DOMAIN --hmp 'info block-jobs'
  • 暂停备份任务
代码语言:javascript复制
virsh qemu-monitor-command DOMAIN '{ "execute": "block-job-cancel", "arguments": { "device": "drive-ide0-0-0", "force": true } }'
  • 重置备份任务
代码语言:javascript复制
virsh qemu-monitor-command DOMAIN '{ "execute" : "block-job-pause", "arguments" : { "device" : "drive-virtio-disk0" } }'
  • 停止备份任务
代码语言:javascript复制
virsh qemu-monitor-command DOMAIN '{ "execute": "block-job-cancel", "arguments": { "device": "drive-virtio-disk0", "force": true } }'

bitmap 命令

  • 创建bitmap
代码语言:javascript复制
virsh qemu-monitor-command DOMAIN { "execute": "block-dirty-bitmap-add",  "arguments": {"node": "drive0","name": "bitmap0" }}
  • 删除bitmap
代码语言:javascript复制
virsh qemu-monitor-command DOMAIN { "execute": "block-dirty-bitmap-remove", "arguments": {"node": "drive0","name": "bitmap0"}}
  • 重置 bitmap node
代码语言:javascript复制
virsh qemu-monitor-command DOMAIN { "execute": "block-dirty-bitmap-clear",  "arguments": {"node": "drive0","name": "bitmap0"}}

远程备份

  • 命令
代码语言:javascript复制
iscsi://[<username>[%<password>]@]<host>[:<port>]/<target-iqn-name>/<lun>
代码语言:javascript复制
iscsi://192.168.1.100:3260/iqn.2019-01.com.iaas/0
  • 样例
代码语言:javascript复制
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" } }'

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

  • 客户端搭建

安装

代码语言:javascript复制
apt install open-iscsi

设置服务开机启动

代码语言:javascript复制
systemctl  enable  iscsi.service
systemctl  enable  iscsid.service
systemctl  start  iscsi.service
systemctl  start  iscsid.service

iscsi-name

代码语言:javascript复制
cat  /etc/iscsi/initiatorname.iscsi

设置名称:

代码语言:javascript复制
vim /etc/iscsi/initiatorname.iscsi
InitiatorName=name
systemctl  restart  iscsi.service

查找target

代码语言:javascript复制
iscsiadm  -m  discovery  -t  st  -p  172.26.106.103:3260

登陆target

代码语言:javascript复制
iscsiadm -m node --targetname iqn.2020-06.wuqiang.iscis -p 172.26.106.103:3260 --login

设置全局单向认证

代码语言:javascript复制
discovery.sendtargets.auth.authmethod = CHAP
discovery.sendtargets.auth.username = troila
discovery.sendtargets.auth.password = 123456

重启服务

代码语言:javascript复制
/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 #查看状态
  • 服务端搭建

安装

代码语言:javascript复制
apt install targetcli-fb

进入配置页面

代码语言:javascript复制
targetcli

相关命令行

代码语言:javascript复制
  - 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
创建存储结构

创建物理磁盘存储

代码语言:javascript复制
cd backstores/block
create lun1 /dev/loop10

创建文件存储

代码语言:javascript复制
cd /backstores/fileio/
create lun1 /opt/fileio.img

创建内存存储

代码语言:javascript复制
cd ../ramdisk 
create rd0 10MB

查看创建好的存储资源

代码语言:javascript复制
/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]
创建iscsi目标
代码语言:javascript复制
cd /iscsi/
create #自动创建用户
create iqn.2020-06.iscsi:sn.20200623 #创建指定用户

关联后端存储

代码语言:javascript复制
cd iqn.2020-06.iscsi:sn.20200623/tpg1/luns/
create /backstores/block/lun1  #为后端存储路径

配置ACL(访问控制列表)

代码语言:javascript复制
cd ../acls
create iqn.2020-06.wuqiang.iscis #客户端用户名

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

代码语言:javascript复制
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 #查看是否与设置的相同
保存退出
代码语言:javascript复制
exit
其他

创建硬盘

代码语言:javascript复制
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 #格式化

创建文件

代码语言:javascript复制
dd if=/dev/vda5 of=/opt/file.img bs=1G count=20 #创建
mkfs -t ext4 /opt/file.img #格式化

查看block信息

代码语言:javascript复制
virsh qemu-monitor-command DOMAIN --hmp info block
代码语言:javascript复制
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)

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

qemu4.2.0

编译

卸载之前安装的qemu

代码语言:javascript复制
apt remove qemu*

源码路径:

代码语言:javascript复制
https://download.qemu.org/

ubuntu编译

代码语言:javascript复制
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

检测:

代码语言:javascript复制
virsh
version

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

代码语言:javascript复制
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

注意:

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

qemu-monitor使用

进入monitor

代码语言:javascript复制
qemu-system-x86_64 -enable-kvm -m 512 -smp 2 -hda vm-img-path -boot c -vnc :1 -monitor stdio

dirve_backup介绍

代码语言:javascript复制
help drive_backup
代码语言:javascript复制
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).

样例:

代码语言:javascript复制
drive_backup -f ide0-hd0 /opt/qcow2/151_backup.qcow2 qcow2

查看备份任务:

代码语言:javascript复制
info block-jobs

查看device:

代码语言:javascript复制
info block

任务取消:

代码语言:javascript复制
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)
代码语言:javascript复制
block_job_cancel ide0-hd0

libvirt4.5

gmp

代码语言:javascript复制
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

nettle-3.4

代码语言:javascript复制
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

libgnutls-3.5.18

代码语言:javascript复制
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

libvirt4.5

代码语言:javascript复制
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

依赖库下载

代码语言:javascript复制
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.错误一

代码语言:javascript复制
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)

解决方案

代码语言:javascript复制
cd /usr/lib/x86_64-linux-gnu/
rm -rf libvirt.so.0*

2.错误二

代码语言:javascript复制
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)

解决方案:

代码语言:javascript复制
rm -rf libvirt-lxc.so.0*
rm -rf libvirt-qemu.so.0*

日志模块

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

相关错误:

代码语言:javascript复制
.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)

解决方案:

代码语言:javascript复制
rm -rf libvirt-lxc.so.0*
rm -rf libvirt-qemu.so.0*

日志模块

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

virt-manager创建虚拟机

代码语言:javascript复制
相关错误:USB redirection is not supported by this version of QEMU
解决方案:移除USB redirection1,USB redirection2

0 人点赞