Xen DOM-U Backup using LVM-Snapshots and SSH

To backup xen domu’s while running, you can use the following shell script. You have to use lvm on your server (to create live-snapshots) and you have to use key authentification for ssh (to avoid password authentication). What is special about this script is, that it runs on a remote machine. It executes commands through ssh and receives the complete lv-dump through ssh. So you can create a central backup server, which will backup remotely on a scheduled basis and you can win a redundancy at different physical locations very easily, if your bandwidth and lv-size agrees. If your bandwidth is rather slow, you can pipe the output of dd into gzip on the remote machine - or on the backup server if your connection is fast, but your backup space is rather limited. After backing up, you can mount it the usual way:

mount -o loop backup.img /mnt

Here is the script:

#!/bin/sh

if [ $# -ne 7 ]
then
  echo “usage: xen-lvm-backup [HOST] [DOM_U] [BACKUP_LV] [BACKUP_LV_SIZE] [VG] [LV] [DEST]”
  exit 1
fi

HOST=”$1″
DOM_U=”$2″
BACKUP_LV=”$3″
BACKUP_LV_SIZE=”$4″
VG=”$5″
LV=”$6″
DEST=”$7″

if /usr/bin/ssh -o PasswordAuthentication=no -l root $HOST /bin/true; then # able to login?
  /usr/bin/ssh -o PasswordAuthentication=no -l root $HOST “/usr/sbin/xm pause $DOM_U”
  /usr/bin/ssh -o PasswordAuthentication=no -l root $HOST “/sbin/lvcreate -L$BACKUP_LV_SIZE -s -n $BACKUP_LV /dev/$VG/$LV”
  /usr/bin/ssh -o PasswordAuthentication=no -l root $HOST “/usr/sbin/xm unpause $DOM_U”
  /bin/echo “” > $DEST
  /usr/bin/ssh -o PasswordAuthentication=no -l root $HOST “/bin/dd if=/dev/$VG/$BACKUP_LV” > $DEST
  /usr/bin/ssh -o PasswordAuthentication=no -l root $HOST “/sbin/lvremove -f /dev/$VG/$BACKUP_LV”
fi

Use it without any warranty.

Leave a Reply

CAPTCHA Image Audio Version
Reload Image