Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env bash
## Configs ##
ZFSSNAPSHOTNAME="duplicacy"
mountSnapshots () {
mkdir -p "/mnt/$ZFSSNAPSHOTNAME"
mount -t zfs "$ZFSSNAPSHOTBASE@$ZFSSNAPSHOTNAME" "/mnt/$ZFSSNAPSHOTNAME"
while read -r line
do
MOUNTPATH=$(echo "$line" | sed "s/$ESCAPEDZFSSNAPSHOTBASE\///" | sed "s/@$ZFSSNAPSHOTNAME//")
if [ -d "/mnt/$ZFSSNAPSHOTNAME/$MOUNTPATH" ]; then
mount -t zfs "$line" "/mnt/$ZFSSNAPSHOTNAME/$MOUNTPATH"
fi
done < <(zfs list -t snapshot | grep "$ZFSSNAPSHOTNAME" | grep "$ZFSSNAPSHOTBASE" | grep -v "$ZFSSNAPSHOTBASE/\." | grep -o "$ZFSSNAPSHOTBASE/[0-9,a-z,A-Z,/,[:space:]]*@$ZFSSNAPSHOTNAME" | sed 's/[[:space:]]*$//')
}
unmountSnapshots () {
while read -r line
do
MOUNTPATH=$(echo "$line" | sed "s/$ESCAPEDZFSSNAPSHOTBASE\///" | sed "s/@$ZFSSNAPSHOTNAME//")
if [ -d "/mnt/$ZFSSNAPSHOTNAME/$MOUNTPATH" ]; then
umount "/mnt/$ZFSSNAPSHOTNAME/$MOUNTPATH"
fi
done < <(zfs list -t snapshot | grep "$ZFSSNAPSHOTNAME" | grep "$ZFSSNAPSHOTBASE" | grep -v "$ZFSSNAPSHOTBASE/\." | grep -o "$ZFSSNAPSHOTBASE/[0-9,a-z,A-Z,/,[:space:]]*@$ZFSSNAPSHOTNAME" | sed 's/[[:space:]]*$//' | tail -r)
umount "/mnt/$ZFSSNAPSHOTNAME"
rmdir "/mnt/$ZFSSNAPSHOTNAME"
}
exitCleanup () {
trap SIGINT
cd "/"
unmountSnapshots
zfs destroy -r "$ZFSSNAPSHOTBASE@$ZFSSNAPSHOTNAME"
rm $PIDFILE
exit
}
createPIDFile () {
if [ -f $PIDFILE ]
then
PID=$(cat $PIDFILE)
ps -p $PID > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "Process already running"
exit 1
else
## Process not found assume not running
echo $$ > $PIDFILE
if [ $? -ne 0 ]
then
echo "Could not create PID file"
exit 1
fi
fi
else
echo $$ > $PIDFILE
if [ $? -ne 0 ]
then
echo "Could not create PID file"
exit 1
fi
fi
}
if [ $# -eq 1 ]
then
BASENAME=$(basename $1)
RUNNAME="$ZFSSNAPSHOTNAME-$BASENAME"
ZFSSNAPSHOTBASE=$(zfs list | grep "$BASENAME" | grep -v "$BASENAME/" | awk '{print $1}')
ESCAPEDZFSSNAPSHOTBASE=$(echo "$ZFSSNAPSHOTBASE" | sed 's/\//\\\//')
PIDFILE=/var/lock/$RUNNAME.pid
ISOTIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
YEAR=$(date -u +"%Y")
createPIDFile
zfs snapshot -r "$ZFSSNAPSHOTBASE@$ZFSSNAPSHOTNAME"
trap "exitCleanup" INT
mountSnapshots
# Do tha backup dance!
cd "/mnt/$ZFSSNAPSHOTNAME"
duplicacy -log backup -threads 10 -stats >> "/var/log/$RUNNAME.log"
cd "/"
unmountSnapshots
zfs destroy -r "$ZFSSNAPSHOTBASE@$ZFSSNAPSHOTNAME"
rm $PIDFILE
else
echo "Argument should be: path remote"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment