Add a template hook for default Proxmox install

This commit is contained in:
Jeremy Gardais 2019-04-26 21:25:48 +02:00
parent 0eea33d04e
commit fe6bd07b01
1 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,83 @@
#!/usr/bin/perl -w
# hook script to copy a dump as a new LXC template (with --script option)
# Template directory
my $TEMPLATE_DIR = "/var/lib/vz/template/cache";
# Template file name
my $TEMPLATE_FILE_LINK = "debian.stretch.template.tar.gz";
# Number of template to keep available
my $RETENTION_TIME = "2";
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";
# do what you want
} 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}; # openvz/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;vmid=$vmid;dumpdir=$dumpdir;storeid=$storeid;hostname=$hostname;tarfile=$tarfile;logfile=$logfile\n";
# copy resulting backup file as a template
if ($phase eq 'backup-end') {
# Copy the dump as a LXC template
system ("cp -- $tarfile $TEMPLATE_DIR") == 0 ||
die "copy tar file as a template failed";
# Unlink (eg hostname=debian.stretch.template.tar.gz)
system ("unlink $TEMPLATE_DIR/$TEMPLATE_FILE_LINK");
# no die cause if the previous backup exit on tarfile copy, the link might not exist
# Link last template file to a better name
system ("find $TEMPLATE_DIR -iname 'vzdump-lxc-$vmid*.tar.*' -mmin -60 -exec ln -s {} $TEMPLATE_DIR/$TEMPLATE_FILE_LINK \\;") == 0 ||
die "link template to a better name failed";
# Ensure to remove template older than $RETENTION_TIME
system ("find $TEMPLATE_DIR -iname 'vzdump-lxc-$vmid*.tar.*' -mtime +$RETENTION_TIME -delete ") == 0 ||
die "remove oldest template failed";
}
} else {
die "got unknown phase '$phase'";
}
exit (0);