Triggering some script at the end of the backups

dominikp

Member
Aug 28, 2018
36
3
13
39
Is is possible to trigger some action/script after backups end?

Im doing backups to local drive. I want to synchronise them to NFS share. No, i dont want to set backups to NFS directly. Offcourse i could use cron, to start the script on specific time after backups usually end. But its prone to errors. I want to start this script (syncing) after each backups action is done.
 
Of course thats possible, keyword is hook script for vzdump.

short version:
add the following line to your "/etc/vzdump.conf":
Code:
script: /etc/your-custom-script.sh

And in that script you can do something like this:
Code:
#!/bin/bash

if [ "$1" == "job-start" ]; then
  do something
fi

if [ "$1" == "job-end" ]; then
  do something else
fi

exit 0

Now vzump will call the hook-script at different stages during the backup. For example it will call the hook-script with the parameter "job-start" before and after the backup with "job-end".

So you can do pretty much anything starting from here - there are many threads in the forum around this...
 
Thanks for answers! But before i implement it i want to clarify two more things. I usually backup more than one container/vm.
1.Does backup-end trigger starts at the end of each subtask (container backup) (like vm1backup->backup-end-script->vm2backup->backup-end-script-> ...) or is it for entire backup job (vm1-backup->vm2backup->backup-end-script-> ...)
2. If its triggered for subtask does vm2backup have to wait until backup-end-script for vm1 is done or does those scripts are just triggered and can overlap each other?

Overall i would want to make backups of all VMs/CTs first and then start to copy them to my mounted share.

I striped down example script to this form ...
Code:
#!/usr/bin/perl -w

# Example hook script for vzdump (--script option)
# This can also be added as a line in /etc/vzdump.conf
# e.g. 'script: /usr/local/bin/vzdump-hook-script.pl'


use strict;

print "HOOK: " . join (' ', @ARGV) . "\n";

my $phase = shift;

# example: copy resulting backup file to another host using scp
if ($phase eq 'backup-end') {
    my $mode = shift; # stop/suspend/snapshot
    my $vmid = shift;
    my $vmtype = $ENV{VMTYPE}; # lxc/qemu
    my $dumpdir = $ENV{DUMPDIR};
    my $storeid = $ENV{STOREID};
    my $hostname = $ENV{HOSTNAME};
    my $tarfile = $ENV{TARFILE};
    my $nfsbackupdir = '/mnt/pve/freenas-bckp-encrypted/dump/';
    # tarfile is only available in phase 'backup-end'
    print "HOOK-ENV: vmtype=$vmtype;dumpdir=$dumpdir;storeid=$storeid;hostname=$hostname;tarfile=$tarfile\n";

    system ("scp $tarfile $nfsbackupdir") == 0 ||
        die "copy tar file to backup-host failed";
}

exit (0)

EDIT. backup-end triggers actions at each backup subtask and job-end triggers actions at the end of entire backup job. Am i right?
 
Last edited:
i just looked at my logs and i can confirm that "job-start/-end", what i use, is triggered at the start and end of the whole Backup-Job and not inbetween the different vm's.

Unfortunately i can't say anything about backup-start/end, i've never used it.

From looking at my logs vzump waits for the custom script to finish before it keeps on going. So if you need some overlapping you have to come up with seomthing in your script like sending a task to background and immediately give a return code to vzudmp.
 
Thanks a lot! This script works well for me:

Code:
#!/bin/bash
backupdir='/data/local-2/dump'
nfsbackupdir=/mnt/pve/freenas-bckp-encrypted/dump

dobackup(){
for file in $backupdir/*
do
  #echo "---$file"
  if [[ $file == *"$(date '+%Y_%m_%d')"* ]]; then
    echo "Copying to external storage (NFS): $file"
    cp $file $nfsbackupdir/
  fi
done
}

if [ "$1" == "job-end" ]; then
  dobackup
fi

exit 0
prox-back.png
 
Thanks a lot! This script works well for me:

Code:
#!/bin/bash
backupdir='/data/local-2/dump'
nfsbackupdir=/mnt/pve/freenas-bckp-encrypted/dump

dobackup(){
for file in $backupdir/*
do
  #echo "---$file"
  if [[ $file == *"$(date '+%Y_%m_%d')"* ]]; then
    echo "Copying to external storage (NFS): $file"
    cp $file $nfsbackupdir/
  fi
done
}

if [ "$1" == "job-end" ]; then
  dobackup
fi

exit 0
View attachment 12843



How can I check in your script whether it is a backup that is displayed locally?
I have a VM with an NFS (running OMV). When I make a backup I can only do it locally since the share is on the VM.
I can make all other backups directly on the NFS.
 
How can I check in your script whether it is a backup that is displayed locally?
I have a VM with an NFS (running OMV). When I make a backup I can only do it locally since the share is on the VM.
I can make all other backups directly on the NFS.


Update/Solution:

Code:
if [ "$1" == "job-end" ]; then
    if [ "${STOREID}" == "local" ]; then
        echo "es ist ein locales Backup, ich starte die Übertragung."
        dobackup
    else
    echo "es ist kein locales Backup, ich muss nichts übertragen"
    fi 
fi
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!