Weekly backups of all OpenVZ container
Thu, 01/08/2009 - 17:56 — sandipHere's is a simple shell script to run a weekly lvm snapshot dump of all OpenVZ containers using the vzdump utility:
#!/bin/bash
# ve_dumps.sh
# Dump all VEs
# Todays' date
DATE=$(date +%d)
# Paths
BAK_PATH=/opt/bak/vz_dumpsr /># Week of month
BAK_DIR=$(cal | awk -v date="${DATE}" '{ for( i=1; i <= NF ; i++ ) if ($i==date) { print FNR-2 } }')
# Function to check and remove previously failed snapshot.
check_vzsnap() {
OUTPUT=`/usr/sbin/lvdisplay | grep vzsnap`
[ -n "$OUTPUT" ] && lvremove -f /dev/vg0/vzsnap
}
# Function to perform backup.
backup() {
# Check and create the required backup directory
[ -d "${BAK_PATH}/${BAK_DIR}&q uot; ] || mkdir -p ${BAK_PATH}/${BAK_DIR}
# do dumps
echo "Starting dump at `date`"
/usr/bin/vzdump --exclude-path '.+/log/.+' --exclude-path '.+/bak/.+' --exclude-path '/tmp/.+' --exclude-path '/var/tmp/.+' --exclude-path '/var/run/.+pid' --snapshot --dumpdir=${BAK_PATH}/${BAK_DI R} --compress --all
echo "Completed dump at `date`"
}
# Main ############################r />
# Remove previously failed snapshot
check_vzsnap
# Run backups
backup
exit 0