73 lines
1.7 KiB
Perl
73 lines
1.7 KiB
Perl
|
#!/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";
|
||
|
# 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') {
|
||
|
# 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";
|
||
|
# Copy the dump as a LXC template
|
||
|
system ("cp -- $tarfile $TEMPLATE_DIR") == 0 ||
|
||
|
die "copy tar file as a template failed";
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
|
||
|
die "got unknown phase '$phase'";
|
||
|
|
||
|
}
|
||
|
|
||
|
exit (0);
|