Script to backup virtual machines
Prerequisites:
- VM must be located on LVM
- virsh
This is my script (all vm will be backup-ed):
#!/bin/sh
##############################
VOLGROUP=sugriwa
LOGVOLSRC=episode1
##SOURCE=/home/export/vm
TARGET=/home/backup/backupvm
##
##default
LOGVOLSNAP=snapvm1
SNAPSIZE=15G
##############################
if [ "x$1" != "xY" ] ; then
echo $0 Y
exit
fi
mkdir "$TARGET/xml"
ALLVM=`virsh list --all | tail -n+3 | head -n-1 | awk '{print $2}'`
for a in $ALLVM; do
virsh suspend $a
virsh dumpxml $a > "$TARGET/xml/$a.xml"
done
echo creating snapshot
lvcreate -L$SNAPSIZE -s -n $LOGVOLSNAP $VOLGROUP/$LOGVOLSRC
for a in $ALLVM; do
virsh resume $a
done
mkdir /mnt/$LOGVOLSNAP
mount /dev/$VOLGROUP/$LOGVOLSNAP /mnt/$LOGVOLSNAP
echo copying...
rsync -ahvx --inplace --progress /mnt/$LOGVOLSNAP/ $TARGET/
umount /mnt/$LOGVOLSNAP
rmdir /mnt/$LOGVOLSNAP
echo removing snapshot
lvremove -f $VOLGROUP/$LOGVOLSNAP
- VM must be located on LVM
- virsh
This is my script (all vm will be backup-ed):
#!/bin/sh
##############################
VOLGROUP=sugriwa
LOGVOLSRC=episode1
##SOURCE=/home/export/vm
TARGET=/home/backup/backupvm
##
##default
LOGVOLSNAP=snapvm1
SNAPSIZE=15G
##############################
if [ "x$1" != "xY" ] ; then
echo $0 Y
exit
fi
mkdir "$TARGET/xml"
ALLVM=`virsh list --all | tail -n+3 | head -n-1 | awk '{print $2}'`
for a in $ALLVM; do
virsh suspend $a
virsh dumpxml $a > "$TARGET/xml/$a.xml"
done
echo creating snapshot
lvcreate -L$SNAPSIZE -s -n $LOGVOLSNAP $VOLGROUP/$LOGVOLSRC
for a in $ALLVM; do
virsh resume $a
done
mkdir /mnt/$LOGVOLSNAP
mount /dev/$VOLGROUP/$LOGVOLSNAP /mnt/$LOGVOLSNAP
echo copying...
rsync -ahvx --inplace --progress /mnt/$LOGVOLSNAP/ $TARGET/
umount /mnt/$LOGVOLSNAP
rmdir /mnt/$LOGVOLSNAP
echo removing snapshot
lvremove -f $VOLGROUP/$LOGVOLSNAP
Comments