Last active
October 25, 2016 14:47
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.