Skip to content
 

A framework for BackupPC‘s pre/post scripts

This is just one of many possible ways to do this.

The idea is to put something like this in the configuration of each host that is being backed up:


$Conf{DumpPreUserCmd} = [
    '/usr/local/bin/backuppc_prescript.sh', 
    '-c', '$client',
    '-h', '$host',
    '-i', '$hostIP',
    '-x', '$XferMethod',
    '-t', '$type',
];

$Conf{DumpPostUserCmd} = [
    '/usr/local/bin/backuppc_postscript.sh',
    '-c', '$client',
    '-h', '$host',
    '-i', '$hostIP',
    '-x', '$XferMethod',
    '-t', '$type',
    '-o', '$xferOK',
];

This way, each script is passed all the information about the backup that BackupPC makes available. Each host would have the same configuration, so management of per-host configuration files is easy.

Here is a sample backuppc_prescript.sh:


#!/bin/bash

# Standard BackupPC pre-script

client=
host=
hostIP=
xfermethod=
incrfull=

while getopts ':c:h:i:x:t:' opt; do
  case $opt in
    c)
      client=$OPTARG
      ;;
    h)
      host=$OPTARG
      ;;
    i)
      hostIP=$OPTARG
      ;;
    x)
      xfermethod=$OPTARG
      ;;
    t)
      incrfull=$OPTARG
      ;;
    \?)
      echo "Invalid option: -$OPTARG." >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

# exit with error if some argument is missing
( [ "$client" = "" ] || [ "$host" = "" ] || [ "$hostIP" = "" ] || [ "$xfermethod" = "" ] || [ "$incrfull" = "" ] ) && exit 1

error=0

# run extra commands for this host, if any
# if you want to fail the backup, set error to nonzero here
# (or exit nonzero directly)
if [ -r "/etc/backuppc/scripts/${host}-pre.sh" ]; then
  . "/etc/backuppc/scripts/${host}-pre.sh"
fi

exit $error

The backuppc_postscript.sh script follows a similar pattern:


#!/bin/bash

# Standard BackupPC post-script

client=
host=
hostIP=
xfermethod=
incrfull=
xferOK=

hostid=host    # or client or IP

while getopts ':c:h:i:x:t:o:n:w:' opt; do
  case $opt in
    c)
      client=$OPTARG
      ;;
    h)
      host=$OPTARG
      ;;
    i)
      hostIP=$OPTARG
      ;;
    x)
      xfermethod=$OPTARG
      ;;
    t)
      incrfull=$OPTARG
      ;;
    o)
      xferOK=$OPTARG
      ;;
    n)
      extraname=$OPTARG
      ;;
    w)
      hostid=$OPTARG
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument" >&2
      exit 1
      ;;
  esac
done

# exit with error if some argument is missing
( [ "$client" = "" ] || [ "$host" = "" ] || [ "$hostIP" = "" ] || [ "$xfermethod" = "" ] || [ "$incrfull" = "" ] || [ "$xferOK" = "" ] ) && exit 1

# here we know how the backup went, so do anything appropriate
# eg, notify the monitoring system, update some database, whatever

# examples
zabbix_sender -z zabbix.example.com -s "${host}" -k backup.status -o ${xferOK} 2>&1
printf '%s\t%s\t%d\t%s\n' "${host}" backup_status "${xferOK}" "" | send_nsca -H nagios.example.com -c /etc/send_nsca.cfg
mysql -h mysql.example.com -e "INSERT INTO history.backups (date, host, type, status) VALUES (NOW(), '${host}', ${incrfull}, ${xferOK});"

error=0

# run extra commands for this host, if any
# if you want to fail the backup, set "error" to nonzero inside this script
# (or exit nonzero directly)
if [ -r "/etc/backuppc/scripts/${host}-post.sh" ]; then
  . "/etc/backuppc/scripts/${host}-post.sh"
fi

exit $error

Everything shown up to here is fixed. If some host needs to run some extra or special task, then, as can be seen, it's enough to drop a <hostname>-pre.sh or/and <hostname>-post.sh script into /etc/backuppc/scripts (or wherever, really) for it to be run. Note that these scripts are sourced, so they have access to the same complete information (environment) the caller has about backup status, backup type and so on. They can also crucially set the $error variable, thus they can decide the overall success or failure status for the backup (assuming $Conf{UserCmdCheckStatus} is set to 1 in the main configuration, of course, as it is by default).