2016-12-09 16:08:26 +01:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
|
|
|
# hook script to copy a dump as a new LXC template (with --script option)
|
|
|
|
|
|
|
|
# Template directory
|
|
|
|
my $TEMPLATE_DIR = "/mnt/zfsbkp/template/cache";
|
2018-02-13 11:37:33 +01:00
|
|
|
# Template file name
|
2018-02-16 09:52:09 +01:00
|
|
|
my $TEMPLATE_FILE_LINK = "jessie.template.ipr.univ-rennes1.fr.tar.gz";
|
2016-12-09 16:08:26 +01:00
|
|
|
# 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";
|
2018-02-13 11:37:33 +01:00
|
|
|
|
|
|
|
# Unlink (eg hostname=jessietpl.ipr.univ-rennes1.fr)
|
2018-02-21 09:19:34 +01:00
|
|
|
system ("unlink $TEMPLATE_DIR/$TEMPLATE_FILE_LINK");
|
2018-02-13 11:37:33 +01:00
|
|
|
# no die cause if the previous backup exit on tarfile copy, the link might not exist
|
|
|
|
|
|
|
|
# Link last template file to a better name
|
2018-02-21 09:45:38 +01:00
|
|
|
system ("find $TEMPLATE_DIR -iname 'vzdump-lxc-$vmid*.tar.*' -mmin -60 -exec ln -s {} $TEMPLATE_DIR/$TEMPLATE_FILE_LINK \\;") == 0 ||
|
2018-02-13 11:37:33 +01:00
|
|
|
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";
|
2016-12-09 16:08:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
die "got unknown phase '$phase'";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
exit (0);
|