Home » Virtualization

Automatically backing up Xen File-backed DomU

By: Zhiqiang Ma On: Jan 29, 2010 Views: 83 No Comment Print Email
Tags: , , , , ,

A script for backing up file-backed Xen DomU is introduced in this post. This script can be changed to similar platform.

In our cluster, virtual machines are stored under /lhome/xen/. Virtual machine with id vmid is stored in directory vmvmid. The raw image disk file name can also be derived from vmid. Some more details and the configuration file can be found from unified-xen-domu-configuration-file. The set up process of Xen platform can be found from set-up-xen-341-dom0-on-top-of-fedora-11-with-kernel-2-6-29. Some introduction to Xen management command xm can be found from create-and-manage-virtual-machines-on-xen.

This script is called by cron from every one week. We want to backup the virtual machines for every other week. mark_file is used to make the backup command run every other time the script is called. More details please refer to how-to-run-a-cron-job-every-two-weeksmonthsdays. If this script is used manually, please delete the “# check whether bak runned last week” part.

Two copies of backup is made in the script: one in local directory in another hard disk, another one in hard disk of another machine. The raw image file are compress for save disk space and network usage. In our virtual machines, the raw image file size is 20G, while the compressed one is only about 3G to 4G depends on the usage of the virtual machine’s hard disk space. ssh tunnel is used for data transmission.

The backup consist of several steps:

1. For every virtual machine:
    1.1 Shutdown the virtual machine
    1.2 Copy the image file to local directory in another hard disk
    1.3 Start the virtual machine
2. Compress the copied image file in local directory
3. Copy these compressed image file to remote directory

Okay, this is the script:

#!/bin/bash

mark_file=/tmp/xen-bak-marker
log_file=/lhome/xen/bak.log
err_log_file=/lhome/xen/bak_err.log
xen_dir=/lhome/xen
local_bak_dir=/lhome/xen-bak-tmp
bak_dir=10.0.0.13:/lhome/xen-bak
xen_id_list="111 103 104 105 106"

# check whether bak runned last week
if [ -e $mark_file ] ; then
  rm -f $mark_file
else
  touch $mark_file
  exit 0
fi

# set std and stderr to log file
mv $log_file $log_file.old
mv $err_log_file $err_log_file.old
exec 2> $err_log_file
exec > $log_file 

###############################
date
echo "*** Backup starts" 

###############################
date
echo "*** Copy VMs to local disk"

for i in $xen_id_list
do
  date
  echo "Copy vm$i"
  echo "Shutdown vm$i"
  /usr/sbin/xm shutdown vm$i
  sleep 30
  echo "Copy to local_bak_dir: $local_bak_dir"
  cp $xen_dir/vm$i/vmdisk$i $local_bak_dir/vmdisk$i
  date
  echo "Create vm$i with vmmem=1024"
  /usr/sbin/xm create $xen_dir/vm.run vmid=$i vmmem=1024
  scp $log_file $bak_dir/bak.log
done

####################
date
echo "*** Compress local bak vmdisks"

for i in $xen_id_list
do
  date
  echo "Compress $i"
  tar -z -cf $local_bak_dir/vmdisk$i.tar.gz $local_bak_dir/vmdisk$i
  rm -f $local_bak_dir/vmdisk$i
done

####################
date
echo "*** Copy local bak vmdisks to remote machines"

for i in $xen_id_list
do
  date
  echo "Copy to remote: vm$i"
  scp $local_bak_dir/vmdisk$i.tar.gz $bak_dir/vmdisk$i.tar.gz
done

#####################
date
echo "Backup finishes"
scp $log_file $bak_dir/bak.log

Read more:

Digg del.icio.us Stumble Techorati Facebook Newsvine Reddit Twitter
Mixx LinkedIn Google Bookmark Yahoo Bookmark MySpace LiveJournal Blogger RSS feed
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.