Voici le script que j’utilise pour dumper quotidiennement l’ensemble de mes CT OpenVZ. Outre les options de dump habituelles, il  est possible de préciser le nombre de dumps à conserver localement sur le HN et d’activer une copie des dumps par rsync sur un serveur distant, en vue de constituer un historique plus conséquent. Il est possible de limiter le débit I/O de génération des dumps (I/O disque) tout comme pour la copie (I/O réseau) afin d’éviter une perturbation conséquente de votre serveur.

Bref, c’est simple et efficace, donc à placer en cron très rapidement !

#!/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="backup.my.domain"   # remote server host
RSYNC_USER="root"               # remote server user
RSYNC_DIR="/home/backup/mcbain" # remote server directory
RSYNC_DELETE="no"               # 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 !!!
#

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."
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

Voici un exemple de sortie :

# ./vzbackup.sh
Starting backup of CT 101...
INFO: starting new backup job: vzdump --stdexcludes --snapshot --compress --maxfiles 4 --dumpdir=/home/backup/vz/101 101
INFO: Starting Backup of VM 101 (openvz)
INFO: CTID 101 exist mounted running
INFO: status = CTID 101 exist mounted running
INFO: backup mode: snapshot
INFO: ionice priority: 7
INFO: creating lvm snapshot of /dev/mapper/openvz-vz ('/dev/openvz/vzsnap-mcbain-0')
INFO:   Logical volume "vzsnap-mcbain-0" created
INFO: creating archive '/home/backup/vz/101/vzdump-openvz-101-2011_02_16-17_42_01.tgz'
INFO: Total bytes written: 2044569600 (2.0GiB, 8.9MiB/s)
INFO: archive file size: 1016MB
INFO:   Logical volume "vzsnap-mcbain-0" successfully removed
INFO: Finished Backup of VM 101 (00:05:24)
INFO: Backup job finished successfuly
Backup done.
Synchronizing to remote server...
sending incremental file list
101/
101/vzdump-openvz-101-2011_02_16-17_42_01.log
101/vzdump-openvz-101-2011_02_16-17_42_01.tgz
Synchronization done.