Skip to content

Instantly share code, notes, and snippets.

@corrupt
Last active October 25, 2016 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corrupt/4ddd77dce498e2b229b2cdc7ae01837b to your computer and use it in GitHub Desktop.
Save corrupt/4ddd77dce498e2b229b2cdc7ae01837b to your computer and use it in GitHub Desktop.
A simple and crude Backup script for KVM qcow2 images from a remote logical volume.
#/bin/bash
VG="/dev/vg0"
SNAPSHOTLV="imagesnap"
SOURCELV="vm-images"
MOUNTPOINT="/mnt/snapshot"
DESTINATION="/mnt/Backup"
ssh root@server /bin/bash <<EOF
lvcreate -l100%FREE -s -n $SNAPSHOTLV $VG/$SOURCELV
mkdir -p $MOUNTPOINT
mount $VG/$SNAPSHOTLV $MOUNTPOINT
EOF
IMAGES=$(ssh root@server ls $MOUNTPOINT/*.qcow2)
for img in $IMAGES; do
if [ -e $DESTINATION/$img ]; then
rsync -avh --progress --inplace root@server:$MOUNTPOINT/$img $DESTINATION
else
rsync -Pavh --sparse root@server:$MOUNTPOINT/$img $DESTINATION
fi
done
ssh root@server /bin/bash <<EOF
umount $MOUNTPOINT
lvremove $VG/$SNAPSHOTLV -f
rmdir $MOUNTPOINT
EOF
@corrupt
Copy link
Author

corrupt commented Oct 21, 2016

This assumes a host entry server in .ssh/config and a readily set-up ssh public key authentication as root on the remote site.
Remote files will be compared with local destination and updated in place, if they already exist. Otherwise a whole backup will me made in sparse mode, efficiently handling zero blocks.
Takes care of LVM snapshots and cleanup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment