Rogerio Ferreira

   
You are here: Home Downloads Xen Backup Image
Document Actions

Xen Backup Image

Utilitário para realizar backup de máquinas virtuais inteiras no XEN remotamente, utilizando rdiff-backup. Para máquinas virtuais criadas com o utilitário do xen-tools, xen-create-image.

Click here to get the file

Size 3.6 kB - File type text/x-sh

File contents

#!/bin/bash
# Performing remote backups of VMs created with xen-create-image tool (xen-tools).
# Use rdiff-backup as backend. Tested on Debian Etch R3. Date: 8 August 2008
#
# Copyright (C) 2008 Rogerio Ferreira (http://rogerioferreira.objectis.net)
# Written by Rogerio Ferreira (rogeriotux_at_gmail_dot_com)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

opt_l=""
opt_v=""
opt_u=""
opt_s=""

HELP="
Uso: $(basename "$0") [OPTIONS]

OPTIONS:
  -l, --lv   		LV name.
  -v, --vg  		VG name.
  -u, --user		Remote user name.
  -s, --server	Remote machine name.

  -h, --help    Display help.

Example: # ./xen-backup-image --lv email --vg vm --user backup-vms --server 192.168.0.239

"

while [ -n "$1" ]
do
	case "$1" in
		
		-l | --lv)
			shift
			opt_l="$1"
			
			if [ -z "$opt_l" ]
			then
				echo "No arguments given to -l | --lv"
				exit 1
			fi

			# Check if the LV exists.
			lv_existe=$(xen-list-images | grep "Name:" | awk '{print $2}' | grep "^$opt_l$")

			if [ -z "$lv_existe" ]
			then
				echo "LV $opt_l do not exist."
				exit 1
			fi

		;;

		-v | --vg)
			shift
			opt_v="$1"
			
			if [ -z "$opt_v" ]
			then
				echo "No arguments given to -v | --vg"
				exit 1
			fi
			
			# Check if the VG exists.
			vg_existe=$(lvdisplay | grep "VG Name" | awk '{print $3}' | grep "^$opt_v$")
			
			if [ -z "$vg_existe" ]
			then
				echo "VG $opt_v do not exist."
				exit 1
			fi
		;;
		

                -u | --user)
                        shift
                        opt_u="$1"

                        if [ -z "$opt_u" ]
                        then
                                echo "No arguments given to -u | --user"
                                exit 1
                        fi

                ;;

                -s | --server)
                        shift
                        opt_s="$1"

                        if [ -z "$opt_s" ]
                        then
                                echo "No arguments given to -s | --server"
                                exit 1
                        fi

                ;;


		-h | --help)
			echo "$HELP"
			exit 0
		;;

		*)
			echo "See the -h | --help option for more information."
			exit 1 
		;;
	esac
	
	shift

done

if  ! [ -f "/usr/bin/rdiff-backup" ]
then
	echo RDIFF-BACKUP not installed.
	echo RDIFF-BACKUP being installed now... Wait...

	aptitude update 1> /dev/null
	aptitude install rdiff-backup -y 1> /dev/null

	if ! [ -f "/usr/bin/rdiff-backup" ]
	then
        	echo Unexpected error has occurred, installation aborted.
		exit 1
	fi

	echo "rdiff-backup installed!"
fi

LVs=$(lvs | awk '{print $1}' | grep "^$opt_l-*" | grep -v swap)

echo Performing backup... Wait...

if [ -z "$opt_l" ] || [ -z "$opt_v" ] || [ -z "$opt_u" ] || [ -z "$opt_s" ]
then
	echo "No arguments given!"
	exit 1
fi

for LV in $LVs
do
	mkdir $LV-tmp
	mount -r /dev/$opt_v/$LV $LV-tmp
	rdiff-backup --force $LV-tmp $opt_u@$opt_s::$LV  2> /dev/null
	umount $LV-tmp
	rm -rf $LV-tmp
done

echo Backup finished!