Backup Proxmox Containers to FTP

Sometimes there is no other option, as using FTP for backup. A quick search through Net gives several solutions, but sadly they are mostly either buggy or ugly. So here I came up with my own solution.

Depends on ftp-upload and Perl >5.9.

#!/usr/bin/perl -w
# hook script for vzdump (--script option)
# quax@moccu.com 26.04.2011

use strict;
print "HOOK: " . join (' ', @ARGV) . "\n";

# config
my $host = "host";
my $user = "user";
my $pass = "secret";
my $dir = "/";

my $phase = shift;
my $mode = shift;            # stop/suspend/snapshot
my $vmid = shift;
my $vmtype = $ENV{VMTYPE};        # openvz/qemu
my $dumpdir = $ENV{DUMPDIR};
my $hostname = $ENV{HOSTNAME};
my $tarfile = $ENV{TARFILE};
my $logfile = $ENV{LOGFILE}; 

my %dispatch = (
    "job-start"     => \&nop,
    "job-end"       => \&nop,
    "job-abort"     => \&nop,
    "backup-start"  => \&nop,
    "backup-end"    => \&backup_end,
    "backup-abort"  => \&nop,
    "log-end"       => \&log_end,
    "pre-stop"      => \&nop,
    "pre-restart"   => \&nop,
);

sub upload {
    my $file = shift;

    # try it twice
    system("echo $pass | ftp-upload -h $host -u $user -s -d $dir $file") == 0 ||
    system("echo $pass | ftp-upload -h $host -u $user -s -d $dir $file") == 0 ||
    die "upload to backup-host failed";

    print "HOOK: upload " . $file . " to ftp://" . $host . $dir . " done\n";
}

sub nop {
    # nothing
}

sub backup_end {
    upload($tarfile);
}

sub log_end {
    upload($logfile);
}

exists $dispatch{$phase} ? $dispatch{$phase}() : die "got unknown phase '$phase'";

exit (0);

put it into /usr/local/bin/backup-hook.pl and execute

# echo "script: /usr/local/bin/backup-hook.pl" >> /etc/vzdump.conf

to add it to your config file.