-# Set the email address to send notification to
-mailto=alan@pippins.net
-
-# The ssh connection doesn't find zfs without this.
-zfs=/usr/sbin/zfs
+# Setup our cleanup and exit trap
+cleanup() {
+ if [[ -e "$local_list" ]]; then
+ rm -f $local_list
+ fi
+ if [[ -e "$remote_list" ]]; then
+ rm -f $remote_list
+ fi
+ if [[ -n "$remote" ]]; then
+ ssh $remote ls -d "$lockdir" > /dev/null 2>&1
+ if [[ $? == 0 ]]; then
+ ssh $remote rm -rf "$lockdir"
+ fi
+ fi
+}
+fatal_and_exit() {
+ echo -e 2>&1 "$1"
+ # Destroy the backup markers on the local filesystem if they exist
+ if [[ -n "$current_backup_marker" ]]; then
+ zfs list -t snapshot ${local_pool}/${current_backup_marker} > /dev/null 2>&1
+ if [ $? == 0 ]; then
+ $zfs destroy ${local_pool}/${current_backup_marker}
+ fi
+ fi
+ if [[ -n "$previous_backup_marker" ]]; then
+ zfs list -t snapshot ${local_pool}/${previous_backup_marker} > /dev/null 2>&1
+ if [ $? == 0 ]; then
+ $zfs destroy ${local_pool}/${previous_backup_marker}
+ fi
+ fi
+ # send email notification
+ if [[ -n "$2" ]]; then
+ echo -e "$1" | $mailx -s "zfs replicate on $hostname failed" "$2"
+ fi
+ # exit
+ exit 1
+}
+trap fatal_and_exit INT
+trap cleanup EXIT