Skip to content

Instantly share code, notes, and snippets.

@optiz0r
Created May 1, 2017 20:21
Show Gist options
  • Save optiz0r/68cb93172d72d2f8db48f7ab7b67c28e to your computer and use it in GitHub Desktop.
Save optiz0r/68cb93172d72d2f8db48f7ab7b67c28e to your computer and use it in GitHub Desktop.
Simple bacula ZFS/bpipe fileset and wrapper scripts
#!/bin/sh
#
# A script which does everything bacula bpipe needs ie:
# - create a snapshot with the current date / time
# - do a zfs send out of that snapshot
# - clean up snapshot when complete
#
FILESYSTEM=$1
if [ -z $FILESYSTEM ]; then
exit 1
fi
TIME_NOW=`date +%Y%m%d%H%M%S`
TIMESTAMP_NOW=`date -u +%s`
SNAP_NAME=backup_$TIME_NOW
SNAP_OBJ=$FILESYSTEM@$SNAP_NAME
# Go ahead and snap what we need
# snapshot the filesystem
zfs snapshot $FILESYSTEM@$SNAP_NAME
if [ $? -ne 0 ]; then
exit 2
fi
# do the sending
zfs send $FILESYSTEM@$SNAP_NAME
if [ $? -ne 0 ]; then
exit 3
fi
# clean up
zfs destroy $FILESYSTEM@$SNAP_NAME
if [ $? -ne 0 ]; then
exit 4
fi
# mark as backed up
zfs set myco:last_backup="$TIMESTAMP_NOW|$TIME_NOW" $FILESYSTEM
if [ $? -ne 0 ]; then
exit 5
fi
#!/bin/sh
#
# A script which handes restorals of backups which
# were taken with bacula bpipe or originaly backed
# up from a FIFO
# - given as an output argument to bpipe
# - creates a fifo (in location specified by arg1)
# - redirects output of bacula restore to this fifo
# - its left up to you to either:
# - cat fifo | zfs recv path/to/filesystem
# - cat fifo > /path/to/filename.zfsdump
#
RESTOREFIFO=$1
if [ -z $RESTOREFIFO ]; then
# no input argument given
exit 1
fi
if [ -e $RESTOREFIFO ]; then
# if the $RESTOREFIFO path exists already
exit 2
fi
# create a fifo at the correct path to restore into
mkfifo $RESTOREFIFO
if [ $? -ne 0 ]; then
# creation of the restore fifo failed
exit 3
fi
# redirect the filestream which bacula provides over stdin
# into the $RESTOREFIFO
cat < /dev/stdin > $RESTOREFIFO
RESTOREOK=$?
# Clean up fifo whether or not the restore failed
rm $RESTOREFIFO
CLEANUPOK=$?
if [ $RESTOREOK -ne 0 ]; then
# looks like the restore failed
exit 4
fi
if [ $CLEANUPOK -ne 0 ]; then
# couldnt clean up the $RESTOREFIFO, otherwise good
exit 5
fi
FileSet {
Name = "test-fileset"
Include {
Options {
signature = MD5
readfifo = yes
}
Plugin = "bpipe:/zfsdump/server/pool_filesystem:/usr/local/sbin/bacula-zfs-send 'pool/filesystem':/usr/local/sbin/bacula-zfs-restore /tmp/pool_filesystem.fifo"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment