-
-
Save cabal95/e36c06e716d3328b512b to your computer and use it in GitHub Desktop.
#!/bin/bash | |
# | |
BACKUPDEST="$1" | |
DOMAIN="$2" | |
MAXBACKUPS="$3" | |
if [ -z "$BACKUPDEST" -o -z "$DOMAIN" ]; then | |
echo "Usage: ./vm-backup <backup-folder> <domain> [max-backups]" | |
exit 1 | |
fi | |
if [ -z "$MAXBACKUPS" ]; then | |
MAXBACKUPS=6 | |
fi | |
echo "Beginning backup for $DOMAIN" | |
# | |
# Generate the backup path | |
# | |
BACKUPDATE=`date "+%Y-%m-%d.%H%M%S"` | |
BACKUPDOMAIN="$BACKUPDEST/$DOMAIN" | |
BACKUP="$BACKUPDOMAIN/$BACKUPDATE" | |
mkdir -p "$BACKUP" | |
# | |
# Get the list of targets (disks) and the image paths. | |
# | |
TARGETS=`virsh domblklist "$DOMAIN" --details | grep ^file | awk '{print $3}'` | |
IMAGES=`virsh domblklist "$DOMAIN" --details | grep ^file | awk '{print $4}'` | |
# | |
# Create the snapshot. | |
# | |
DISKSPEC="" | |
for t in $TARGETS; do | |
DISKSPEC="$DISKSPEC --diskspec $t,snapshot=external" | |
done | |
virsh snapshot-create-as --domain "$DOMAIN" --name backup --no-metadata \ | |
--atomic --disk-only $DISKSPEC >/dev/null | |
if [ $? -ne 0 ]; then | |
echo "Failed to create snapshot for $DOMAIN" | |
exit 1 | |
fi | |
# | |
# Copy disk images | |
# | |
for t in $IMAGES; do | |
NAME=`basename "$t"` | |
cp "$t" "$BACKUP"/"$NAME" | |
done | |
# | |
# Merge changes back. | |
# | |
BACKUPIMAGES=`virsh domblklist "$DOMAIN" --details | grep ^file | awk '{print $4}'` | |
for t in $TARGETS; do | |
virsh blockcommit "$DOMAIN" "$t" --active --pivot >/dev/null | |
if [ $? -ne 0 ]; then | |
echo "Could not merge changes for disk $t of $DOMAIN. VM may be in invalid state." | |
exit 1 | |
fi | |
done | |
# | |
# Cleanup left over backup images. | |
# | |
for t in $BACKUPIMAGES; do | |
rm -f "$t" | |
done | |
# | |
# Dump the configuration information. | |
# | |
virsh dumpxml "$DOMAIN" >"$BACKUP/$DOMAIN.xml" | |
# | |
# Cleanup older backups. | |
# | |
LIST=`ls -r1 "$BACKUPDOMAIN" | grep -E '^[0-9]{4}-[0-9]{2}-[0-9]{2}\.[0-9]+$'` | |
i=1 | |
for b in $LIST; do | |
if [ $i -gt "$MAXBACKUPS" ]; then | |
echo "Removing old backup "`basename $b` | |
rm -rf "$b" | |
fi | |
i=$[$i+1] | |
done | |
echo "Finished backup" | |
echo "" |
Is there a reason you all do not use blockcopy? I can backup/clone complete VM's without shutting them down. A snapshot relies on the last backup.
So a mix of a weekly Full Backup and snapshots would be a smart thing right? I have to read up on it, but when I make 6 snapshots and one full backup, I need basically just the fullbackup plus the latest snapshot to restore my machine.
Is there a reason you all do not use blockcopy? I can backup/clone complete VM's without shutting them down. A snapshot relies on the last backup. So a mix of a weekly Full Backup and snapshots would be a smart thing right? I have to read up on it, but when I make 6 snapshots and one full backup, I need basically just the fullbackup plus the latest snapshot to restore my machine.
The point of this script (at least the last time I checked it), is to pivot your live OS to a temporary snapshot file, while it is still booted and running. This frees up the ability to copy the OS virtual disk without risk of data corruption while it is running live. Once the backup of the VM disk is finished, the live snapshot is pivoted back onto the VM disk file, and resumes (or rather, maintains) live running of the OS.
Example of how I automate this script in the following loop and it backs up all the VMs on the system. Note: I did have to make sure permissions were correct on the disk image files so the script would actually back them up, but besides that, it works great! Love it! Thanks for this.
I also rsync the /etc directory to the backup drive so I have a copy of the existing configs. I suppose I should copy the /etc directory into a date-stamped directory with the images, I just haven't written that yet.
#!/bin/bash date date > /etc/date.log BACKUPDIR=/media/usbbackup IMGDIR=$BACKUPDIR/image-backups # However many backups you want to keep... NUMBACKUPS=5 rsync -arp --delete /etc $BACKUPDIR # Selects all domains (on or off) to begin the loop (i.e. VMs with a number: running, or VMs with a dash [-]: stopped.) for VM in $( virsh list --all | awk '( $1 ~ /^[0-9]|-+$/ ) { print $2 }' ) do /usr/local/bin/vm-backup.sh $IMGDIR $VM $NUMBACKUPS #echo "Next" done date exit
Example of how I automate this script in the following loop and it backs up all the VMs on the system. Note: I did have to make sure permissions were correct on the disk image files so the script would actually back them up, but besides that, it works great! Love it! Thanks for this.
I also rsync the /etc directory to the backup drive so I have a copy of the existing configs. I suppose I should copy the /etc directory into a date-stamped directory with the images, I just haven't written that yet.
#!/bin/bash
date
date > /etc/date.logBACKUPDIR=/media/usbbackup
IMGDIR=$BACKUPDIR/image-backupsHowever many backups you want to keep...
NUMBACKUPS=5
rsync -arp --delete /etc $BACKUPDIR
Selects all domains (on or off) to begin the loop (i.e. VMs with a number: running, or VMs with a dash [-]: stopped.)
for VM in $( virsh list --all | awk '(
$1 ~ /^[0-9]|-+$ / ) { print $2 }' )
do
/usr/local/bin/vm-backup.sh $IMGDIR $VM $NUMBACKUPS
#echo "Next"
done
date
exit
Instead of using awk, you can use virsh list --all --name
.
@juliyvchirkov thanks for sharing! Just a few notes from my side:
virtnbdbackupWorkers="$(/usr/bin/nproc)"
this doesnt make quite that much sense, virtnbdbackup uses one worker for each disk the virtual machine has attached, to speed up the backup process (process multiple disks in different threads).
So it is always limited to the amount of disks, not to the amounts of cpu's that your host system has. If you use the amount of cpus (which is most probably bigger than the amount of disks the virtual machine has) it will always default to the amount of disks, as the value is bigger ;)). So if you dont want to limit the amount of concurrent disks to process during backup, leave this value to default or limit it to a lower amount of workers if you want to throttle backup process.
virtnbdbackupSocket="/var/tmp/virtnbdbackup.sock"
by default virtnbdbackup uses a process related socket file, that allows to start multiple backups from different virtual
machines at the same time. If you ever want to enhance your script to backup multiple virtual machines at the same time (not sequentially as now) you dont want to set this option to a hardcoded socked.