Skip to content

Instantly share code, notes, and snippets.

@cnrd
Created March 8, 2018 02:07
Show Gist options
  • Save cnrd/40b7c6d79d5d645b21eb2da561c17b91 to your computer and use it in GitHub Desktop.
Save cnrd/40b7c6d79d5d645b21eb2da561c17b91 to your computer and use it in GitHub Desktop.
#!/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
@ilium007
Copy link

ilium007 commented Nov 3, 2019

After reading the docs again I am assuming I do an 'init' in the root of the data to be backed up and the .duplicity directory is then available from within the ZFS snapshot. The preferences file would then have the storage URL and other metadata to allow the backup to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment