Skip to content

Instantly share code, notes, and snippets.

@hammady
Last active October 1, 2023 02:25
Show Gist options
  • Save hammady/119701dcbec4652ef630cec875bb9f48 to your computer and use it in GitHub Desktop.
Save hammady/119701dcbec4652ef630cec875bb9f48 to your computer and use it in GitHub Desktop.
A bash script to backup Raspberry Pi home to OneDrive using duplicity
# Place this in /etc/logrotate.d/duplicity
/var/log/duplicity.log {
maxsize 100K
missingok
rotate 10
compress
notifempty
create 0640 pi root
}
#!/bin/bash
# The above shebang is useless when running as a cronjob
# To run it as a cronjob, insert the below after typing 'crontab -e':
# SHELL=/bin/bash
# PATH=/home/pi/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
# 0 0 * * * /home/pi/backup.sh &>> /var/log/duplicity.log
# Do not forget to remove the leading # in the previous lines
# The script should be stored in /home/pi/backup.sh
# Also the log file /var/log/duplicity.log should be writable by the user pi
cd $HOME
echo "================================================="
echo "Backing up Pi on `date`"
encryption_key=<PLACE_GPG_KEY_ID_HERE>
dest=onedrive://backups/rpi
date=`date +%d`
mode=
if [ $date = 01 ]; then
mode=full
echo "New month detected"
fi
echo Backing up Pi-Hole data
[ -d pihole-backups ] || mkdir pihole-backups
(
cd pihole-backups
# generate a tar backup using pihole admin teleporter
pihole -a teleporter
ls -lh
# remove files older than 30 days
find . -name "pi-hole-teleporter*.tar.gz" -mtime +30 -exec rm -f {} \;
)
echo Backing up PiVPN configuration
pivpn backup
ls -lh pivpnbackup/
# remove files older than 30 days
find . -name "pivpnbackup/*.tgz" -mtime +30 -exec rm -f {} \;
echo Backup up $PWD to $dest using duplicity and mode ${mode:-incremental}...
duplicity --version
duplicity $mode --encrypt-key $encryption_key $PWD $dest
echo "Cleaning backups older than 3 months"
duplicity remove-older-than 3M --force $dest
echo Done
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment