Skip to content

Instantly share code, notes, and snippets.

@harupong
Last active May 30, 2024 09:20
Show Gist options
  • Save harupong/51d6fde05e2385bfc7fee263c79cee07 to your computer and use it in GitHub Desktop.
Save harupong/51d6fde05e2385bfc7fee263c79cee07 to your computer and use it in GitHub Desktop.
Proxmox backup hook script
#!/usr/bin/perl -w
# Hook script for vzdump to backup to a GCS bucket
# Based on the blog post of Andrew Palardy
# https://www.apalrd.net/posts/2022/pve_backup/
use strict;
# Define the name of your bucket here
my $bucket = "proxmox-monthly-backup";
# Define the name of your rclone remote
my $remote = "gcs";
#Uncomment this to see the hook script with arguments (not required)
print "HOOK: " . join (' ', @ARGV) . "\n";
#Get the phase from the first argument
my $phase = shift;
#For job-based phases
#Note that job-init was added in PVE 7.2 or 7.3 AFAIK
if ( $phase eq 'job-init' ||
$phase eq 'job-start' ||
$phase eq 'job-end' ||
$phase eq 'job-abort') {
#Env variables available for job based arguments
my $dumpdir = $ENV{DUMPDIR};
my $storeid = $ENV{STOREID};
#Uncomment this to print the environment variables for debugging
print "HOOK-ENV: dumpdir=$dumpdir;storeid=$storeid\n";
#Call rclone sync at job end
if ($phase eq 'job-end') {
#system ("/root/s3cleanup.sh $bucket \"$retain days\"");
my $result = system ("rclone sync --config /root/.config/rclone/rclone.conf $dumpdir $remote:$bucket");
#Die of error returned
if($result != 0) {
die "sync backup failed";
}
}
#For backup-based phases
} 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') {
#Data available to backup-based phases
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};
my $tarfile = $ENV{TARGET};
my $logfile = $ENV{LOGFILE};
#Uncomment this to print environment variables
print "HOOK-ENV: vmtype=$vmtype;dumpdir=$dumpdir;storeid=$storeid;hostname=$hostname;tarfile=$tarfile;logfile=$logfile\n";
# During backup-end, do something
if ($phase eq 'backup-end') {
# do something
}
# During log-end, do something
if ($phase eq 'log-end') {
# do something
}
#Otherwise, phase is unknown
} 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