Back up your OpenVZ container with vzdump and LVM2
OpenVZ is a great piece of software to do container (or VPS) hosting on a Linux box. It doesn’t come with any fancy management tools and it’s pretty raw, but it’s very solid and stable and does the job very well.
For this howto, you will need vzdump. You can download it from the contrib directory on the openvz.org server.
Backing up a container can be a test, because the best method to do it is very sloppy explained in the manual. There are 3 options to do a backup :
Method 1:
- Stop the container (e.g. vzctl stop 101)
- Backup the container (e.g. vzdump 101)
- Start the container (e.g. vzctl start 101)
Method 2:
- Suspend the container for a while (e.g. vzdump –suspend 101)
Method 3:
- Make a live snapshot of the container (e.g. vzdump –dumpdir /home –snapshot 101)
Method 1 is ofcourse slow because backing up a container can be a very lengthy process when it’s a full-blown LAMP server. For instance. A full backup of the webserver this site is running on takes about 2 hours. You don’t want to do that. Trust me.
Method 2 is a lot nicer. It makes a backup of the running container. After it’s done, it pauses the container and starts the backup again with rsync. This way, you only need to backup differences between the live container and the paused container. Suspending usually takes only a few minutes.
But, we want method 3. And trust me.. you want it too
. Making a live snapshot, which you backup to a seperate location doesn’t give you any downtime which is good. It uses LVM2 to make a snapshot of the /vz filesystem (which also has to be in LVM2 ofcourse). For buffering changes, it needs about 512MB of snapshot space. I usually create a much bigger snapshot logical volume because i don’t like to use minimal requirements.
I this example, i use a seperate disk especially for OpenVZ storage. The volume group is called ‘SAN’ (it’s mounted on iSCSI) and the VG is 100GB big.
/vz is mounted on a logical volume called ‘openvz’ (/dev/SAN/openvz). This logical volume is 90GB big, so this leaves me 10GB of unallocated space to be used for snapshotting and other things in the future. Don’t forget to keep some space unallocated when building your data “disk”. If you do so, you will never be able to use LVM2 snapshots.
The rest is actually a piece of cake. vzdump creates a snapshot of /dev/SAN/openvz in /dev/SAN/vzsnap. This snapshot is then backed up to a seperate directory to a tarball.
You can put the following in /etc/crontab to do a daily backup, starting at 2:00, for a single container :
00 2 * * * root /usr/bin/vzdump --dumpdir /home/backups --snapshot --compress --mail some@mailaddress.com 101
Or for all containers :
00 2 * * * root /usr/bin/vzdump --dumpdir /home/backups --snapshot --compress --mail some@mailaddress.com --all
Et voila! There’s your superb backup for OpenVZ.