Skip to content

How to backup all OpenVZ containers

Last updated on March 13, 2019

I made a simple working script to backup all the containers at once and send it on Linux backup server via RSYNC, here’s how to do it.

What we got:

  1. OpenVZ server with a lot of containers
  2. Linux backup server

Create file script wherever you want

touch backup.sh

Insert the code below

#!/bin/bash

/bin/mkdir /home/backup/`date "+%Y%m%d"`
vzdump --dumpdir /home/backup/`date "+%Y%m%d"` --compress --all

sshpass -p "password" rsync -avzhe ssh /home/backup/`date "+%Y%m%d"` root@ipadress:/root/openvz-backup
/bin/rm -rf find /home/backup/*

Now lets look at this script in details

/bin/mkdir /home/backup/`date “+%Y%m%d”`   – creates new directory with recent day and time

vzdump –dumpdir /home/backup/`date “+%Y%m%d”` –compress –all  – backing up all the containers

sshpass -p “password” rsync -avzhe ssh /home/backup/`date “+%Y%m%d”` root@ipadress:/root/openvz-backup  – Here we connect to another Linux machine with ssh protocol, using password. Next with rsync we send compressed containers to backup server with already existing folder – root@ipadress:/root/openvz-backup

And after all that delete all newly created containers backup to clear up storage – /bin/rm -rf find /home/backup/*

Make it executable and run

chmod +x backup.sh
sh backup.sh

Then lets make it run daily for example. To do this, you should move it to path:

/etc/cron.daily/

So the full path will be /etc/cron.daily/backup.sh

*Optionally I added another script on the Linux backup server to control the storage

Create script clean.sh and paste the code

#!/bin/bash

find /root/openvz-backup/* -type d -ctime +7 -exec rm -rf {} \;

You can adjust the number of days here

-ctime +7

Dont forget to make it executable

chmod +x clean.sh

Add this script to cron

crontab -e

and past this to the end of file

0 1 * * * sh /root/clean.sh

In this case script will run every day at 01:00 AM

Published inLinuxScriptShell

Be First to Comment

Leave a Reply