Pour backup des machine virtuelle, nous utiliserons vzdump.
Voir: http://webserverpage.com/?p=248
Pour l’installation de vzdump, voici la procédure:
Configuration du repos Dag: /etc/yum.repos.d/dag.repo
[dag] name=Dag baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag enabled=1 gpgcheck=0
yum install cstream
yum install perl-LockFile-Simple
wget http://download.openvz.org/contrib/utils/vzdump/vzdump-1.2-4.noarch.rpm
rpm -ivh vzdump-1.2-4.noarch.rpm
On édite le fichier .bashrc pour y ajouter:
export PERL5LIB=/usr/share/perl5/
Script pour automatiser les backups des machines virtuelles
J’ai trouvé ce script qui fait pas mal tout:
vzbackup.sh
#!/bin/bash
#
# vzbackup.sh
#
# Boris HUISGEN <bhuisgen@hbis.fr>
#PATH=$PATH:/usr/sbin
# backup settings
VZDUMP_DIR="/home/backup/vz" # backup directory
VZDUMP_MODE="snapshot" # dump mode (stop/suspend/snapshot)
VZDUMP_COMPRESS="yes" # compress dump files (yes/no)
VZDUMP_MAXFILES="3" # maximum backups per CT to keep in local
VZDUMP_BWLIMIT="0" # limit I/O bandwith (unit kbytes/s, 0 to disable)
VZDUMP_EXTRA_OPTIONS="" # extra options (man vzdump)
# remote copy settings
RSYNC_ENABLE="yes" # copy backup directory to remote server (yes/no)
RSYNC_HOST="" # remote server host
RSYNC_USER="" # remote server user
RSYNC_DIR="/home/backup/vz" # remote server directory
RSYNC_DELETE="yes" # delete old backups on remote server (set to no to keep a remote historic)
RSYNC_BWLIMIT="0" # limit I/O bandwith (unit kbytes/s, 0 to disable)
RSYNC_EXTRA_OPTIONS="" # extra options (man rsync)
#
# DO NOT MODIFY AFTER THIS LINE !!!
#
export PERL5LIB=/usr/share/perl5/
VZLIST="$(which vzlist)"
VZDUMP="$(which vzdump)"
VZDUMP_OPTIONS="--stdexcludes"
RSYNC="$(which rsync)"
RSYNC_OPTIONS="-avz"
function log {
echo "[" `date "+%Y-%m-%d %H:%M:%S"` "] $1"
}
# create backup directory
if [ ! -d $VZDUMP_DIR ]
then
mkdir -p $VZDUMP_DIR;
fi
# set vzdump options
VZDUMP_OPTIONS="$VZDUMP_OPTIONS --$VZDUMP_MODE"
if [ $VZDUMP_COMPRESS = "yes" ]
then
VZDUMP_OPTIONS="$VZDUMP_OPTIONS --compress"
fi
VZDUMP_OPTIONS="$VZDUMP_OPTIONS --maxfiles $VZDUMP_MAXFILES"
if [ $VZDUMP_BWLIMIT -gt "0" ]
then
VZDUMP_OPTIONS="$VZDUMP_OPTIONS --bwlimit $VZDUMP_BWLIMIT"
fi
if [ -z $VZDUMP_EXTRA_OPTIONS ]
then
VZDUMP_OPTIONS="$VZDUMP_OPTIONS $VZDUMP_EXTRA_OPTIONS"
fi
# set rsync options
if [ $RSYNC_DELETE = "yes" ]
then
RSYNC_OPTIONS="$RSYNC_OPTIONS --delete"
fi
if [ $RSYNC_BWLIMIT -gt "0" ]
then
RSYNC_OPTIONS="$RSYNC_OPTIONS --bwlimit=$RSYNC_BWLIMIT"fi
fi
if [ -z $RSYNC_EXTRA_OPTIONS ]
then
RSYNC_OPTIONS="$RSYNC_OPTIONS $RSYNC_EXTRA_OPTIONS"
fi
# dump all CT
for CT in `$VZLIST -Ho veid`; do
log "Starting backup of CT $CT..."
# create CT directory
VZDUMP_SUBDIR="$VZDUMP_DIR/$CT"
if [ ! -d $VZDUMP_SUBDIR ]
then
mkdir $VZDUMP_SUBDIR
fi
# start vzdump
$VZDUMP $VZDUMP_OPTIONS --dumpdir=$VZDUMP_SUBDIR $CT
log "Backup done."
#
# Suppression des fichiers vcdiff trop anciens.
#
# On recupere le nombre de fichier vcdiff contenus dans le repertoire.
NBR_VCDIFF=$(/bin/ls $VZDUMP_SUBDIR | /bin/grep \.vcdiff$ | /usr/bin/wc -l);
# On s'assure qu'il y ait plus de fichiers vcdiff dans le repertoire que le nombre a supprimer.
if [ "$NBR_VCDIFF" -gt "$VZDUMP_FULLBACKUP" ]; then
# On determine le nombre de fichier a supprimer.
NBR_VCDIFF_TO_DELETE=$(/usr/bin/expr $NBR_VCDIFF - $VZDUMP_FULLBACKUP)
# Pour chaque fichier a supprimer.
for VCDIFF_TO_DEL in `/bin/ls ${VZDUMP_SUBDIR} | /bin/grep \.vcdiff$ | /usr/bin/head -${NBR_VCDIFF_TO_DELETE}`; do
# On trouve le fichier de log correspondant.
LOG_TO_DEL=${VCDIFF_TO_DEL/.vcdiff/.log}
# On supprime le fichier vcdiff et log.
/bin/rm -f $VZDUMP_SUBDIR/$VCDIFF_TO_DEL $VZDUMP_SUBDIR/$LOG_TO_DEL
done
log "Fichiers vcdiff/log perimes supprimes."
else
log "Aucun fichier vcdiff/log a supprimer."
fi
# /Suppression des fichiers vcdiff trop anciens.
done
# copy backup directory to remote server
if [ $RSYNC_ENABLE = "yes" ]
then
log "Synchronizing to remote server..."
# start rsync
$RSYNC $RSYNC_OPTIONS $VZDUMP_DIR/ $RSYNC_USER@$RSYNC_HOST:$RSYNC_DIR/
log "Synchronization done."
fi
# end of script
Restaurer une machine virtuelle sur une nouvelle machine physique
vzrestore fichier-vzdump.tgz {new-container-id}
Changer son adresse si necessaire:
vzctl set {new-container-id} --hostname {new-hostname} --save
vzctl set {new-container-id} --ipdel {old-ip} --save
vzctl set {new-container-id} --ipadd {new-ip} --save
On demarre la machine virtuelle:
vzctl start {new-container-id}