PDA

View Full Version : How to backup/restore all HyperVM/OpenVZ VMs Virtual Machines / VPSs



Fli
08-14-2013, 10:38 PM
You have OpenVZ server node?

You wish to backup its virtual machines / VPSs all in one and if possible regularly via cron job?

Here is the solution which worked for me. It is based on "vzdump" and this .sh script will backup all OpenVZ VMs/VPSs to a speciffied directory as files like "vzdump-110.tgz"

More info on manual backup here: http://openvz.org/Backup_of_a_running_container_with_vzdump
More info about migration: Google: openvz vzmigrate OR i there is (https://github.com/slrslr/linux-bash-scripts-for-openvz-vzdump-bulk-restore-migration) migration script
Here is tutorial on how to install vzdump (http://internetlifeforum.com/virtualisation/2495-how-install-openvz-vzdump/) (a must for this cript to work)

More advanced and updated script that can do restore and also couple of fixing post-restore tasks is here: https://github.com/slrslr/linux-bash-scripts-for-openvz-vzdump-bulk-restore-migration

WARNING: do not follow this manual if you do not know what you are doing exactly, some steps may lead in data loss.

1. Login via SSH to your server
2. create new file vmsbackup.sh and paste following code in it

vi vmsbackup.sh

hit "a" key and then copy and paste (right click in putty):

Version #1


#!/bin/bash
# vmsbackup.sh
# Dump all VPSs (VMs)


# Todays' date
DATE=$(date +%d)


# Paths
BAK_PATH=/home/vmsbackup


# 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=`/sbin/lvdisplay | grep vzsnap`
[ -n "$OUTPUT" ] && lvremove -f /dev/vg0/vzsnap
}


# Function to remove all backup files and folders older than X (hours) - 720 = 30 days
tmpwatch -m 720 $BAK_PATH


# Function to perform backup.
backup() {
# Check and create the required backup directory
[ -d "${BAK_PATH}/${BAK_DIR}" ] || mkdir -p ${BAK_PATH}/${BAK_DIR}
# do *****
echo "Starting dump at `date`"
# ionice decreases process priority regarding disk i/o (but only vzdump, not rsync probably)
/bin/nice -n 19 /usr/bin/ionice -c2 -n5 /usr/sbin/vzdump --tmpdir $BAK_PATH --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 --bwlimit 100000
# --bwlimit, do not add low or else dump creating and VPS suspension time can take critically long time
echo "Completed dump at `date`"
}

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

# Remove previously failed snapshot
check_vzsnap

# Run backups
backup

exit 0


Version #2 (send email to admin, allow excluding some VPS out of dump)


#!/bin/bash
# ve_*****.sh
# Dump all VEs

# Todays' date
DATE=$(date +%d)
startt=$(date)

# Paths
BAK_PATH=/backup

# 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=`/sbin/lvdisplay | grep vzsnap`
[ -n "$OUTPUT" ] && lvremove -f /dev/vg0/vzsnap
}

# Function to remove all backup files and folders older than X (hours) - 168 = 7 days, 1080=45days
/bin/nice -n 19 /usr/bin/ionice -c2 -n7 tmpwatch -m 500 $BAK_PATH

for myctid in $(vzlist -o ctid -H | grep -v 860 -F);do
# Function to perform backup.
backup() {
# Check and create the required backup directory
[ -d "${BAK_PATH}/${BAK_DIR}" ] || mkdir -p ${BAK_PATH}/${BAK_DIR}
# do *****
echo "Starting dump at `date`"
/bin/nice -n 19 /usr/bin/ionice -c2 -n7 /usr/sbin/vzdump --tmpdir $BAK_PATH --exclude-path '.+/+.tar.gz' --exclude-path '.+/log/messages.+' --exclude-path '.+/log/exim.+' --exclude-path '.+/log/maillog.+' --exclude-path '.+/log/secure.+' --exclude-path '.+/log/cron.+' --exclude-path '.+/bak/.+' --exclude-path '/tmp/.+' --exclude-path '/var/tmp/.+' --exclude-path '/var/run/.+pid' --snapshot --dumpdir=${BAK_PATH}/${BAK_DIR} --compress $myctid --bwlimit 300000
echo "Completed dump at `date`"

ln -s /root/vzdumprestore $BAK_PATH/vzdumprestore
}
done

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

# Remove previously failed snapshot
check_vzsnap

# Run backups
backup

echo "VPS backups on $(hostname) server should be ready now. vmsbackup runtime:
START: $startt
STOPP: $(date)" | mail -s "Monthly VPS backups" [email protected]
exit 0

then edit line: tmpwatch -m 720 $BAK_PATH
replace number 720 by number of hours. this means that backup files older than this number of hours will be deleted by the script to prevent filling the disk. If you dont want to delete backups at all, just delete that line to make them grow more and more your disk space untill disk is full.

Then ctrl C and write: :wq to save changes and quit editor

3. Then make the file executable so you can run it via cron: chmod +x vmsbackup.sh

4. Then run the backup script by command: sh vmsbackup.sh to test it works. NOTE, that this process will suspend all VMS for some seconds or so, its part of backup process. (NOTE: suspension of a VMs can take even 15 minutes or so if VM contains many millions of files and at same time is large like 50gb and more. Check vzmigrate program which may do migration without suspension. OR do not use --snapshot or --suspend switch in vzdump command in above scripts, use just --compress)

5. Then copy file into weekly cron folder: cp vmsbackup.sh /etc/cron.weekly/

From now on, you should find out backed up VMs in folder: /home/vmsbackup
check this folder size etc if everything works properly. It is not recommended to store backups on local server, much better to stored on the server in another location / another company. You can buy a VPS from ICPH (https://instantcpanelhosting.com/cart.php?gid=4) for backup purposes.

How to restore VM backup files

VMs backup files (example.: vzdump-110.tgz) can be then restored into a working VM virtual machine. The tool which can be used is the same which we used for a backup. It is "vzdump".


vzdump --restore /path/to/vzdump-110.tgz 110

Above mentioned command will restore mentioned backup file as a VM id 110. If vzdump Canot be found, you can install it using command: yum install vzdump OR apt-get install vzdump

°°° Please share your experience to help other readers °°°