Skip to content

Instantly share code, notes, and snippets.

@twkm
Last active April 9, 2018 03:49
Show Gist options
  • Save twkm/e27c3a671b804e66369aef161c940a6e to your computer and use it in GitHub Desktop.
Save twkm/e27c3a671b804e66369aef161c940a6e to your computer and use it in GitHub Desktop.
Duplicity Backups Script
#!/bin/bash
# Text enclosed in {} throughout should be replaced with your desired values
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
# May also need to export $HOME: <https://bugs.launchpad.net/duplicity/+bug/758077>
# This seems to be necessary on a Mac crontab process
ulimit -n 4096
if [ -r .last_backup ]; then
LAST_BACKUP=$(cat .last_backup)
else
LAST_BACKUP=0
fi
# 3540 secs = 59 minutes
SECS_BEFORE_BACKUP=3540
NOW=$(date +%s)
LOGTIME=$(date -r $NOW +"%F %T %z")
ELAPSED=$[NOW-LAST_BACKUP]
if [ -z $1 ]; then
printf "[%s] %ss since last backup check...\n" "$LOGTIME" $ELAPSED
fi
if [ $ELAPSED -gt $SECS_BEFORE_BACKUP ]; then
if [ -z $1 ]; then
printf "Backing up...\n"
fi
# See duplicity man page to learn how to set up an access token
export DPBX_ACCESS_TOKEN=""
# doing a full backup every 3 months, otherwise incremental.
duplicity --full-if-older-than 3M --include-filelist {/path/to/}backuplist --gpg-options "--trust-model always" --encrypt-key {GPG key email} {/path/to/local/folder} {scheme://path/to/backup}
# cleaning the remote backup space (deleting backups older than 6 months (6M, alternatives would 1Y fo 1 year etc.)
duplicity remove-older-than 9M --force {scheme://path/to/backup}
echo $NOW > .last_backup
unset DPBX_ACCESS_TOKEN
else
if [ -z $1 ]; then
printf "No backup needed yet.\n"
fi
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment