Xen Domain backup script

Installing, Configuring, Troubleshooting server daemons such as Web and Mail
Post Reply
swallowtail
Posts: 112
Joined: 2009/04/18 04:48:27

Xen Domain backup script

Post by swallowtail » 2009/10/01 00:16:12

In the spirit of sharing... :-)

I have written a small script to backup Xen Domains. It takes server names from a text file, checks to see if they are running, and if so shuts them down then does a virt-clone copy of the VM and its config file to "backupdir", progressing through the servers in the list, cloning and restarting them until they are all done.

It's pretty basic, and I'm not a scripting expert, so if anyone comes up with ways to improve it let me know, but I thought it may be useful to someone else...

I have phy drive assignments to my virtuals, and you can't virt-clone with those (I backup the data on those separately) so the script is written to require a copy of the Xen domain's config file in the "xenbackupcfgpath" directory with a .backup extension (i.e. if the server is server01 then it looks for server01.backup). I just copy the standard ones in there with phy drives removed.

I've been using it for a while, but the usual caveats apply... :-D use at your own risk etc... I run it manually at the moment, but there's no reason why it couldn't be automated...

[font=Courier]
#!/bin/bash
# Script written to automatically backup Xen DomU virtual servers.
# Prerequisites:
# Needs a config file in the xenbackupcfgpath directory with
# the same name as the server config file and a .backup
# extension. This .backup file should not have any phy drive
# mappings.

# Simon Wilson, 1 Oct 2009 - simon at simonandkate dot net

debug="echo"
xencfgpath="/etc/xen"
xenbackupcfgpath="/etc/xen/xenbackups"
backupdir="/mnt/usb"

SEQ=/usr/bin/seq

: ${1?"Usage: $0 /path/to/serverlistfile"}

if [ $1 = "--help" ]
then
$debug "Help Text here"
exit 0
fi

$debug "Server List File = \"$1\""

serverlist=( $( cat $1 ) )

$debug "Number of virtual servers to be backed up is $(( ${#serverlist[@]} ))"

for i in $($SEQ 0 $((${#serverlist[@]} - 1)))
do
$debug
$debug "********"
$debug ${serverlist[$i]}
$debug "********"
$debug
if [ -f $xencfgpath/${serverlist[$i]} ];
then
$debug "Server configuration file $xencfgpath/${serverlist[$i]} exists... Continuing..."
if [ -f $xenbackupcfgpath/${serverlist[$i]}.backup ];
then
$debug "Server backup-ready configuration file $xenbackupcfgpath/${serverlist[$i]}.backup exists... Continuing..."

$debug "Backing up Xen Config file $xencfgpath/${serverlist[$i]} to $backupdir/${serverlist[$i]}.$(date +%Y%m%d)..."
cp "$xencfgpath/${serverlist[$i]}" "$backupdir/${serverlist[$i]}.$(date +%Y%m%d)"

$debug "Renaming Xen Config file $xencfgpath/${serverlist[$i]} to $xenbackupcfgpath/${serverlist[$i]}.good..."
mv -f "$xencfgpath/${serverlist[$i]}" "$xenbackupcfgpath/${serverlist[$i]}.good"

$debug "Copying temporary Xen Config file $xenbackupcfgpath/${serverlist[$i]}.backup to $xencfgpath/${serverlist[$i]}..."
cp "$xenbackupcfgpath/${serverlist[$i]}.backup" "$xencfgpath/${serverlist[$i]}"

if /usr/sbin/xm list | grep -q ${serverlist[$i]}
then
$debug "${serverlist[$i]} is running. Sending shutdown command..."
/usr/sbin/xm shutdown ${serverlist[$i]}
while /usr/sbin/xm list | grep ${serverlist[$i]} > null
do
$debug "Waiting for ${serverlist[$i]} to shutdown..."
sleep 10
done
$debug "OK, proceeding..."
fi
$debug "Executing: /usr/bin/virt-clone --nonsparse --original ${serverlist[$i]} --name ${serverlist[$i]}.$(date +%Y%m$
/usr/bin/virt-clone --nonsparse --original ${serverlist[$i]} --name ${serverlist[$i]}.$(date +%Y%m%d) --file $backupd$

$debug "Replacing original Xen Config file $xenbackupcfgpath/${serverlist[$i]}.good to $xencfgpath/${serverlist[$i]}..$
mv -f "$xenbackupcfgpath/${serverlist[$i]}.good" "$xencfgpath/${serverlist[$i]}"

$debug "Removing temporary files..."
rm -f $xencfgpath/${serverlist[$i]}.$(date +%Y%m%d)

/usr/sbin/xm create ${serverlist[$i]}
else
$debug "Error: $xenbackupcfgpath/${serverlist[$i]}.backup does not exist. Make sure that the server has a .backup fil$
$debug "with any "phy" drive mappings removed. "
fi
else
$debug "Error: $xencfgpath/${serverlist[$i]} does not exist. Invalid entry ${serverlist[$i]} in server list file $1."
fi
$debug "Moving on to next entry in server list..."
done
$debug "No more servers to backup. Exiting now."
exit 0[/font]

kenanr
Posts: 1
Joined: 2016/03/06 14:07:39

Re: Xen Domain backup script

Post by kenanr » 2016/03/06 14:15:45

Hi

Also had problem with backup of xen domains so made own scripts and put them on github. I wanted have online backup, disk dump and filesystem type of backup and also easy way of cloning machine. Use it at your own risk.
https://github.com/kenanrizvic/xen-backup-scripts/

Kenan

Post Reply