scripts/proxmox/vzdump-hook-lxc-jessie-temp...

84 lines
2.2 KiB
Perl
Raw Normal View History

#!/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";
# Template file name
my $TEMPLATE_FILE_LINK = "jessie.template.ipr.univ-rennes1.fr.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=jessietpl.ipr.univ-rennes1.fr)
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);