Skip to content

Instantly share code, notes, and snippets.

@DocCyblade
Last active October 27, 2016 22:33
Show Gist options
  • Save DocCyblade/fea83685faafac88d5745310e60bd128 to your computer and use it in GitHub Desktop.
Save DocCyblade/fea83685faafac88d5745310e60bd128 to your computer and use it in GitHub Desktop.
Sample TKLBAM bash hook script using functions
#!/bin/bash -e
# ------------------------------------------------------
# Script by Ken Robinson(https://github.com/DocCyblade)
#
# THIS SCRIPT IS COVERED UNDER THE GNU/V3 LICENSE.
# (https://www.gnu.org/licenses/gpl-3.0.txt)
# ------------------------------------------------------
# This is a disabled hook example.
# To enable, make it executable: chmod +x tklbam-hook-sample.sh
# hooks are always called with two arguments
op=$1
state=$2
# Change this in your function to a non zero to raise a hook error
exit_code=0
# -----------------------------------
# BACKUP - PRE,INSPECT,POST
# -----------------------------------
# Add code in this function that will run BEFORE the BACKUP
function pre_backup {
echo "hook invoked BEFORE BACKUP starts - Extras path = $(pwd)"
# BEFORE the BACKUP Code Here
}
# Add code in this function that will run BEFORE Duplicity uploads backup archive
function inspect_backup {
echo "hook invoked before Duplicity uploads backup archive. Extras path = $(pwd)"
# BEFORE the BACKUP is Uploaded Code Here
}
# Add code in this function that will run AFTER the BACKUP
function post_backup {
echo "hook invoked AFTER BACKUP - Extras path = $(pwd)"
# AFTER the BACKUP is Uploaded Code Here
}
# -----------------------------------
# RESTORE - PRE,INSPECT,POST
# -----------------------------------
# Add code in this function that will run BEFORE the RESTORE
function pre_restore {
echo "hook invoked BEFORE RESTORE starts - Extras path = $(pwd)"
# BEFORE the RESTORE Code Here
}
# Add code in this function that will run after Duplicity downloads backup archive
function inspect_restore {
echo "hook invoked after Duplicity downloads backup archive. Extras path = $(pwd)"
# AFTER the RESTORE is Downloaded - Code Here
}
# Add code in this function that will run AFTER the RESTORE
function post_restore {
echo "hook invoked AFTER RESTORE finishes - Extras path = $(pwd)"
# AFTER the RESTORE is Uploaded Code Here
}
#
# Main script here, calls the correct function and outputs some debug echo lines
#
echo -n "HOOK $0 :: op=$op state=$state pwd=$(pwd) :: "
if [ "$state" = "pre" ]; then
if [ "$op" = "restore" ]; then
pre_restore
elif [ "$op" = "backup" ]; then
pre_backup
fi
elif [ "$state" = "inspect" ]; then
inspect_backup
if [ "$op" = "restore" ]; then
inspect_restore
elif [ "$op" = "backup" ]; then
fi
elif [ "$state" = "post" ]; then
if [ "$op" = "restore" ]; then
post_restore
elif [ "$op" = "backup" ]; then
post_backup
fi
else
echo "bad hook invocation"
fi
exit $exit_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment