From 999bc04056e498fc6ccc540d17fbd42ceeb71735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Fri, 9 Dec 2016 16:08:26 +0100 Subject: [PATCH] Add proxmox hook to copy a dump as a template --- README.md | 39 +++++++++++++++- proxmox/vzdump-hook-lxc-template.pl | 72 +++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 2 deletions(-) create mode 100755 proxmox/vzdump-hook-lxc-template.pl diff --git a/README.md b/README.md index e99b594..e88cd8e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,42 @@ +# ipr_scripts -* grav_cron_: A daily cron to check if [grav website][Grav] got available updates. +#### Table of Contents + +1. [Overview](#overview) +2. [Scripts](#scripts) + * [Proxmox](#proxmox) + * [Grav](#grav) + + +## Overview + +Some usefull scripts for admin or users. + +## Scripts + +### Proxmox + +#### vzdump-hook-lxc-template.pl + +This script must be used as a vzdump's hook (the backup utility for CT and VMs for Proxmox). + +1. The script will : + * Remove the templates oldest than 2 days (by default). + * Copy the current dump as a template in **/mnt/zfsbkp/template/cache** (by default). +2. How-to use : + * Define a backup as usual (in the Proxmox's webgui) and prefer to select only one container. + * In command on the hypervisor, open **/etc/pve/vzdump.cron** and edit the line of the new dump to add : **--script /usr/local/bin/vzdump-hook-lxc-template.pl**. +3. Customization : + * If you don't store template in the default path (**/mnt/zfsbkp/template/cache**), please edit the variable **$TEMPLATE_DIR** in the script. + + +### Grav + +#### grav_cron +A daily cron to check if [Grav][grav website] got available updates. * It need to work from the Grav's root directory. - * If an update is available, it will create a empty file (${GRAV_ROOT}/logs/update), else it will ensure the "update" file is removed. + * If an update is available, it will create a empty file (${GRAV_ROOT}/logs/update), else it will ensure the "update" file is removed. + * Be sure to monitore if **${GRAV_ROOT}/logs/update** exist. [grav website]: https://getgrav.org/ diff --git a/proxmox/vzdump-hook-lxc-template.pl b/proxmox/vzdump-hook-lxc-template.pl new file mode 100755 index 0000000..a4b978e --- /dev/null +++ b/proxmox/vzdump-hook-lxc-template.pl @@ -0,0 +1,72 @@ +#!/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);