If pivoting the VM backing storage back to the original image fails (e.g. VM being down at that time), the script currently still deletes the hotswap image, which means that all changes since the creation of the hotswap image are lost.
		
			
				
	
	
		
			27 lines
		
	
	
		
			819 B
		
	
	
	
		
			Bash
		
	
	
	
	
	
			
		
		
	
	
			27 lines
		
	
	
		
			819 B
		
	
	
	
		
			Bash
		
	
	
	
	
	
#!/bin/bash
 | 
						|
# GENERATED BY CHEF
 | 
						|
# DO NOT EDIT
 | 
						|
 | 
						|
REPOSITORY=$BORG_REPO
 | 
						|
 | 
						|
echo "Starting backup of VM: $1"
 | 
						|
 | 
						|
echo "Dumping domain XML to /root/backups/vm_meta/$1.xml"
 | 
						|
virsh dumpxml --migratable $1 > /root/backups/vm_meta/$1.xml
 | 
						|
 | 
						|
virsh snapshot-create-as --domain $1  \
 | 
						|
        --name hotswap.qcow2  \
 | 
						|
        --no-metadata         \
 | 
						|
        --atomic              \
 | 
						|
        --quiesce             \
 | 
						|
        --disk-only           \
 | 
						|
        --diskspec vda,snapshot=external
 | 
						|
 | 
						|
# TODO report failures
 | 
						|
borg create -v $REPOSITORY::$1_$(date +%F_%H-%M) \
 | 
						|
        /var/lib/libvirt/images/$1.qcow2  \
 | 
						|
        /root/backups/vm_meta/$1.xml
 | 
						|
 | 
						|
echo "Pivoting base image back to original, and removing the snapshot image"
 | 
						|
virsh blockcommit $1 vda --pivot --base=/var/lib/libvirt/images/$1.qcow2 && rm /var/lib/libvirt/images/$1.hotswap.qcow2
 |