Friday, November 16, 2007

Automate Virtuozzo backups on Windows

There is currently no GUI option to automate Virtuozzo backups, although not in current version 3.5.1 SP1.
This will be possible in Virtuozzo 4.0, which is currently in beta.

I'm using 2 bat files to script the backups and remove the oldest backup.
The first script creates a full backup of all virtual private servers using the vzbackup command:

for /F "skip=3 usebackq tokens=*" %%i in (`cmd /c "vzlist -ao veid"`) do (cmd /c "vzbackup "%%i && echo VE ID %%i has been backed up!)

To be able execute the second script you need to download RmEmpty, an executable which allows you to remove empty directories, get it here

First, we'll have to search the destination folder on the backup node for files older then x days, in this example I use 4 days.
forfiles /P e:\vz\backups /S -D -4 /c "cmd /c del /q @file"

The forfiles is a default windows binary, some information about the given variables:
/P indicates the path to start searching
/S instructs forfiles to recurse into subdirectories
/D selects files with a last modified data less than or equal to (-)
/C indicates the command to execute for each file
The /q is to delete the files quietly, otherwise it will ask for a confirmation before deletion.

After deleting the files we'll search for empty directories, and remove them:
C:\system\rmemp12\RMEMPTY.EXE e:\vz\backups\

Be aware that if e:\vz\backups\ itself is also empty, it will remove that one too! So always keep at least 1 file in that directory, I use a simple readme.txt so it never gets deleted.