Changes

Jump to: navigation, search

Backup a running container over the network with ezvzdump

2,008 bytes added, 22:29, 24 July 2009
Added support for GZip compression and backup retention/rotation. Also added support for emailing of statistics.
so you must still use `vzdump --restore` to restore them.
Dependencies: sendEmail package for emailing of statistics
 
== Changes ==
 
2009/7/24 - JBiel - Added support for GZip compression and backup
retention/rotation. Also added support for emailing of statistics.
== Download ==
# This variable contains a space delimited list of VEID's to be backed up.
# You can use VEIDS="$(cd /vz/private/ && echo *)" to backup all your VE's.
VEIDS="1000 2000 3000101 102 103 104"
VZ_CONF="/etc/vz/conf/" # the path to your openvz $VEID.conf files
VZ_PRIVATE="/vz/private/" # the path to the running VE's
LOCAL_DIR="/vz/ezvzdump/" # the local rsync cache / destination directory
# The remote host and path that this script will rsync the VE's to.
REMOTE_HOST="somehost.example.com"
REMOTE_DIR="/backup/"
# Default rsync flags (please note the potentially unsafe delete flags).
# You can also remove the v flag to get less verbose logging.
RSYNC_DEFAULT="rsync -ravH --delete-after --delete-excluded"  # Exclude these directories from backup (space delimited).# I left /var/log in the backup because when doing a full restore# it's necessary that this directory structure is present.RSYNC_EXCLUDE="/usr/portage /var/log"
# Path to vzctl executable
VZCTL="vzctl"
# Nice debugging messages...
function e { echo -e $(date "+%F %T"): $1
}
function die {
[ ! -d "${LOCAL_DIR}" ] && die "\$LOCAL_DIR directory doesn't exist. ($LOCAL_DIR)"
e "`hostname` - VZ backup for containers $VEIDS started." > /tmp/vzbackuptimes
# Loop through each VEID
for VEID in $VEIDS; do
VEHOSTNAME=`vzlist -o hostname $VEID -H`
echo ""
e "Beginning backup of VEID: $VEID";
RSYNC+=" --exclude=${VEID}${path}"
done;
e "Commencing initial ${RSYNC} ${VZ_PRIVATE}${VEID} ${LOCAL_DIR}"
e "Suspending VEID: $VEID"
before="$(date +%s)"
${VZCTL} chkpnt $VEID --suspend
e "Commencing second pass rsync ..."
${RSYNC} ${VZ_PRIVATE}${VEID} ${LOCAL_DIR}
e "Resuming VEID: $VEID"
${VZCTL} chkpnt $VEID --resume
after="$(date +%s)"
elapsed_seconds="$(expr $after - $before)"
e "Done."
e "Container ${VEID} ($VEHOSTNAME) was down $elapsed_seconds seconds during backup process." >> /tmp/vzbackuptimes
else
fi
# Copy VE config files over into the VE storage/cache area
if [ ! -d "${LOCAL_DIR}${VEID}/etc/vzdump" ]; then
e "Creating directory for openvz config files: mkdir ${LOCAL_DIR}${VEID}/etc/vzdump"
mkdir ${LOCAL_DIR}${VEID}/etc/vzdump
${RSYNC} ${LOCAL_DIR}${VEID}/ ${REMOTE_HOST}:${REMOTE_DIR}${VEID}/
# Move old Rotate older tar.gz backups if they exist. You can comment out these lines if you wish to have only one copy. e "Checking for existing files ${REMOTE_HOST}:${REMOTE_DIR}${VEID}.X.tar and rotating them" ssh ${REMOTE_HOST} "[ -f ${REMOTE_DIR}${VEID}.6.tar.gz ] && mv -f ${REMOTE_DIR}${VEID}.6.tar.gz ${REMOTE_DIR}${VEID}.7.tar.gz" ssh ${REMOTE_HOST} "[ -f ${REMOTE_DIR}${VEID}.5.tar.gz ] && mv -f ${REMOTE_DIR}${VEID}.5.tar.gz ${REMOTE_DIR}${VEID}.6.tar.gz" ssh ${REMOTE_HOST} "[ -f ${REMOTE_DIR}${VEID}.4.tar.gz ] && mv -f ${REMOTE_DIR}${VEID}.4.tar.gz ${REMOTE_DIR}${VEID}.5.tar.gz" ssh ${REMOTE_HOST} "[ -f ${REMOTE_DIR}${VEID}.3.tar.gz ] && mv -f ${REMOTE_DIR}${VEID}.3.tar archive to .gz ${REMOTE_DIR}${VEID}.4.tar.backupgz" e ssh ${REMOTE_HOST} "[ -f ${REMOTE_DIR}${VEID}.2.tar.gz ] && mv -f ${REMOTE_DIR}${VEID}.2.tar.gz ${REMOTE_DIR}${VEID}.3.tar.gz"Checking for existing file ssh ${REMOTE_HOST}:"[ -f ${REMOTE_DIR}${VEID}.1.tar.gz ] && mv -f ${REMOTE_DIR}${VEID}.1.tar.gz ${REMOTE_DIR}${VEID}.2.tar (and moving it to .backup if exists)gz" ssh ${REMOTE_HOST} "[ -f ${REMOTE_DIR}${VEID}.0.tar .gz ] && mv -f ${REMOTE_DIR}${VEID}.0.tar .gz ${REMOTE_DIR}${VEID}.1.tar.backupgz"
# Create a remote tar archive - note you can remove the ampersand from the end if you
# don't want multiple tar processes running on the remote host simultaneously.
e "Making a g-zip compresssed tar archive on remote host (this process will run in the background on the remote host)." ssh ${REMOTE_HOST} "tar cf czf ${REMOTE_DIR}${VEID}.0.tar .gz -C ${REMOTE_DIR}${VEID} ./ 2>/dev/null " &
fi
e "Done."
 
done;
e "`hostname` - VZ backup for containers $VEIDS complete!" >> /tmp/vzbackuptimes# Email a log of the backup process to some email address. Can be modified slightly to use native "mail" command# if sendmail is installed and configured locally.cat /tmp/vzbackuptimes | sendEmail -f root@`hostname` -t someuser@example.com -u "`hostname` VZ backup statistics." -s mail.example.com #(put your open relay here)echoecho /tmp/vzbackuptimesrm /tmp/vzbackuptimes
</pre>
3
edits

Navigation menu