Weekly backups of all OpenVZ container

Here'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_dumps
# 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}" ] || 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_DIR} --compress --all
  echo "Completed dump at `date`"
}

# Main ############################

# Remove previously failed snapshot
check_vzsnap

# Run backups
backup

exit 0

Drop the script in /etc/cron.weekly/ or add to crontab:

# weekly dump of VEs
21 1 * * sun /usr/local/bin/ve_dumps.sh

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

iam getting erros

[root@vps2 ~]# sh ve_dumps.sh
awk: cmd. line:1: { for( i=1; i <= NF ; i++ ) if
awk: cmd. line:1: ^ unexpected newline or end of string
awk: cmd. line:1: ($i==date) { print FNR-2 } }
awk: cmd. line:1: ^ syntax error
No volume groups found
Starting dump at Wed Feb 4 07:03:23 CST 2009
ve_dumps.sh: line 26: /usr/bin/vzdump: No such file or directory
ve_dumps.sh: line 27: --exclude-path: command not found
ve_dumps.sh: line 28: /var/run/.+pid: No such file or directory
ve_dumps.sh: line 29: --all: command not found
Completed dump at Wed Feb 4 07:03:23 CST 2009

vzdump missing

Vzdump is a third party utility that can be used to create backups of OpenVZ VEs. It

is not required for the operation of OpenVZ, but it is a very useful utility.

rpm -ivh http://www.proxmox.com/cms_proxmox/cms/upload/vzdump/vzdump-1.0-2.noarch.rpm

To dump a VE:

vzdump --dumpdir /opt/bak/vz_dumps --snapshot <VEID>

To restore/create VE:

vzdump <VEID> --restore /opt/bak/vz_dumps/vzdump-<VEID>.tar

To dump and compress all VEs:

vzdump --dumpdir=/opt/bak/vz_dumps/  --snapshot  --compress --all

Note: the above script uses lvm snapshots, so the path to the dumpdir should be on a separate volume.

Comment