Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@andrearug
Created April 27, 2018 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrearug/b7953c9f9d509f997179fad9174dc5d6 to your computer and use it in GitHub Desktop.
Save andrearug/b7953c9f9d509f997179fad9174dc5d6 to your computer and use it in GitHub Desktop.
Perl script that uploads backup files to cloud storage using rclone, removes backups older then 1 week.
#!/usr/bin/perl -w
# example hook script for vzdump (--script option)
use strict;
print "HOOK: " . join (' ', @ARGV) . "\n";
my $phase = shift;
if ($phase eq 'job-start' ||
$phase eq 'job-end' ||
$phase eq 'job-abort') {
my $dumpdir = $ENV{DUMPDIR};
my $storeid = $ENV{STOREID};
print "HOOK-ENV: dumpdir=$dumpdir;storeid=$storeid\n";
if ($phase eq 'job-end') {
system ("rclone delete --config /root/.config/rclone/rclone.conf --min-age 1w -v -v googlecrypt:dump/") == 0 ||
die "Deleting old backups failed";
}
} elsif ($phase eq 'backup-start' ||
$phase eq 'backup-end' ||
$phase eq 'backup-abort' ||
$phase eq 'log-end' ||
$phase eq 'pre-stop' ||
$phase eq 'pre-restart' ||
$phase eq 'post-restart') {
my $mode = shift; # stop/suspend/snapshot
my $vmid = shift;
my $vmtype = $ENV{VMTYPE}; # lxc/qemu
my $dumpdir = $ENV{DUMPDIR};
my $storeid = $ENV{STOREID};
my $hostname = $ENV{HOSTNAME};
# tarfile is only available in phase 'backup-end'
my $tarfile = $ENV{TARFILE};
# logfile is only available in phase 'log-end'
my $logfile = $ENV{LOGFILE};
print "HOOK-ENV: vmtype=$vmtype;dumpdir=$dumpdir;storeid=$storeid;hostname=$hostname;tarfile=$tarfile;logfile=$logfile\n";
# example: copy resulting backup file to another host using scp
if ($phase eq 'backup-end') {
system ("rclone move -v --config /root/.config/rclone/rclone.conf $tarfile googlecrypt:/dump") == 0 ||
die "move tar file to backup-host failed";
}
# example: copy resulting log file to another host using scp
if ($phase eq 'log-end') {
system ("rclone copy --config /root/.config/rclone/rclone.conf $logfile googlecrypt:/dump") == 0 ||
die "copy log file to backup-host failed";
}
} else {
die "got unknown phase '$phase'";
}
exit (0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment