rysnc https://forum.ff.co.za/ en Backup Virtual Machine https://forum.ff.co.za/documentation/backup-virtual-machine <span>Backup Virtual Machine</span> <span><span lang="" about="/user/jeff" typeof="schema:Person" property="schema:name" datatype="">Jeff</span></span> <span>Sun, 11/11/2018 - 10:21</span> <div class="layout layout--onecol"> <div class="layout__region layout__region--content"> <div class="body-padding field field--name-body field--type-text-with-summary field--label-hidden field--item"><nav aria-label="Book outline for Install QEMU-KVM on Debian 9" role="navigation"> <nav aria-label="Book outline for Install QEMU-KVM on Debian 9" role="navigation"> <p><strong>Table of Contents</strong></p> <ul> <li><a href="/documentation/install-qemu-kvm-debian-9">Install QEMU-KVM on Debian 9</a></p> <ul> <li><a href="/documentation/allow-users-run-virsh-and-manage-vms">Allow users to run virsh and manage VMs</a></li> <li><a href="/documentation/kvm-bridged-networking">KVM Bridged Networking</a></li> <li><a href="/documentation/kvm-virsh-console-access-debian-and-freebsd-vms">KVM Virsh Console Access for Debian and FreeBSD VMs</a></li> </ul> </li> </ul> </nav> <nav aria-label="Book outline for Elliptic Curve Crypto &amp; LetsEncrypt" role="navigation"> <ul></ul> </nav> <nav aria-label="Book outline for Managing qcow2 images" role="navigation"> <ul> <li> <ul> <li><a href="/documentation/managing-kvm-and-qcow2-images" hreflang="en">Managing qcow2 images</a></p> <ul> <li><em><u><strong><a href="/documentation/backup-virtual-machine">Backup virtual machine</a></strong></u></em></li> <li><a href="/documentation/copy-sparse-file-across-network-using-dd-or-rsync" hreflang="en">Copy sparse file across network using dd or rsync</a></li> <li><a href="/documentation/how-mount-qcow2-image-qemu-nbd" hreflang="en">How to mount a qcow2 image with qemu-nbd</a></li> <li><a href="/documentation/reclaiming-space-qcow2-disk-image" hreflang="en">Reclaiming space on qcow2 disk image</a></li> <li><a href="/documentation/resize-qcow2-blockresize-running-vm">Resize qcow2 with blockresize on running VM</a></li> </ul> </li> </ul> </li> </ul> </nav> <ul> <li><a href="/documentation/migrate-vm-minimal-downtime">Migrate a VM with Minimal Downtime</a></li> </ul> <p> </p> </nav> <p><strong>Sources:</strong></p> <ul> <li><a href="https://www.virtkick.com/docs/how-to-perform-a-live-backup-on-your-kvm-virtual-machines.html">www.virtkick.com/docs/how-to-perform-a-live-backup-on-your-kvm-virtual-machines.html</a></li> <li><a href="https://wiki.libvirt.org/page/Live-disk-backup-with-active-blockcommit">wiki.libvirt.org/page/Live-disk-backup-with-active-blockcommit</a></li> </ul> <p> </p> <h2>1. As root, locate the virtual machine you want to back up using virsh:</h2> <p><code>virsh -c qemu:///system list # shows only running</code></p> <p>or</p> <p><code>virsh list --all # shows all</code></p> <p> </p> <h2>2. Dump the configuration for your virtual machine:</h2> <p><code>virsh -c qemu:///system dumpxml MACHINE_NAME &gt; /var/lib/libvirt/images/safe/MACHINE_NAME.datestamp.xml</code></p> <p> </p> <h2>3. You may need to locate the image directory:</h2> <p><code>virsh -c qemu:///system domblklist MACHINE_NAME</code></p> <p> </p> <h2>4. Now prepare the backup of this machine using a snapshot:</h2> <p><code>virsh snapshot-create-as --domain MACHINE_NAME IMAGE_NAME.snapshot --diskspec sda,file=/var/lib/libvirt/images/safe/IMAGE_NAME.snapshot.qcow2 --disk-only --atomic</code></p> <p>Note: If you get an error, then the disk is not converted to virtio-scsi, and should be <strong>vda</strong>, not sda.</p> <p><em>After creating the snapshot, all disk operations from the guest will be directed to it so that you can copy the original disk image to a safe place.</em></p> <p>You can establish this by running, as example:</p> <pre> <code class="language-bash">virsh -c qemu:///system domblklist ispa Target Source ------------------------------------------------ hda - sda /var/lib/libvirt/images/safe/ispa.snapshot.qcow2</code></pre><p> </p> <h2>5. Copy to external server and include both the qcow2 disk image and the machine configuration (the XML file):</h2> <p>su - yourusername   # you must be in libvirt-qemu group</p> <p><code>rsync -hav --progress /var/lib/libvirt/images/IMAGE_NAME.qcow2 192.168.100.2:backups/ # IP address of prionyx on 1GB VLAN</code></p> <p> </p> <h2>6. Merge snapshot:</h2> <p>After the file transfer is completed, you should merge changes written to snapshot, back to original disk image. This operation is called 'active block commit':</p> <p><code>virsh blockcommit MACHINE_NAME sda --active --pivot --shallow --verbose</code></p> <p>Check block devices again. If the block commit completed successfuly your virtual machine should use its original image again.</p> <p><code>virsh -c qemu:///system domblklist MACHINE_NAME</code></p> <p> </p> <h2>Example Backup script for running VMs:</h2> <pre> <code class="language-bash">jeff@sheri:~$ cat /var/lib/libvirt/images/scripts/ispa.backup.sh #!/bin/bash ################################################################################ # # This script takes a snaphot of the running VM, which leaves the main qcow2 # image in a known state. All VM activity is then recorded in the snapshot, # while the main qcow2 image can be rsync'd to a remote backup server. # After the image has been transferred, the snapshot is merged back to the # main image, and then deleted. # # NB. This script must be run by a user who is a member of libvirt-qemu group. # ################################################################################ virsh -c qemu:///system dumpxml ISPA &gt; /var/lib/libvirt/images/xml/ispa.xml scp /var/lib/libvirt/images/xml/ispa.xml 192.168.100.4:/var/lib/libvirt/images/xml/ virsh snapshot-create-as --domain ISPA ISPA.snapshot --diskspec sda,file=/var/lib/libvirt/images/snapshots/ispa.snapshot.qcow2 --disk-only --atomic rsync --rsync-path="ionice -c 3 nice rsync" -havW --progress /var/lib/libvirt/images/ispa.qcow2 192.168.100.4:/var/lib/libvirt/images/sheriff/ virsh blockcommit ISPA sda --active --pivot --shallow --verbose virsh snapshot-delete ISPA --metadata ISPA.snapshot rm -rf /var/lib/libvirt/images/snapshots/ispa.snapshot.qcow2 #rm /var/lib/libvirt/images/xml/ispa.xml </code></pre><p> </p> </div> <div class="field field--name-field-category field--type-entity-reference field--label-above"> <div class="field--label">Category</div> <div class="field--item"><a href="/documentation" hreflang="en">Documentation</a></div> </div> <div class="fftags field field--name-field-tags field--type-entity-reference field--label-inline"> <div class="field--label">Tags</div> <div class="field--items"> <div class="field--item"><a href="/tags/backups" hreflang="en">backups</a></div> <div class="field--item"><a href="/tags/kvm" hreflang="en">KVM</a></div> <div class="field--item"><a href="/tags/rysnc" hreflang="en">rysnc</a></div> <div class="field--item"><a href="/tags/qemu-kvm" hreflang="en">qemu-kvm</a></div> </div> </div> </div> </div> Sun, 11 Nov 2018 08:21:51 +0000 Jeff 126 at https://forum.ff.co.za