In case your Synology or other NAS has no support of RSYNC server (eg Active Backup for Business), this setup will synchronise your config to a (remote) backup location.
Note: pihole teleporter backup will be triggered.
Prepare NAS
- File services: Enable NFS and Rsync
- create User: BackupUsr
- apply RW permissions to shared folder: backup
Mount shared NFS
Following commands will:# Create backup folder
pi@raspberrypi:~ $ sudo mkdir /mnt/backup
# Create credentials file
pi@raspberrypi:~ $ sudo nano .nascreds
# put in username and password
username=BackupUsr
password=<password>
pi@raspberrypi:~ $ sudo nano /etc/fstab
# Add the NFS shared folder to fstab
//192.168.1.9/Backup /mnt/backup cifs credentials=/home/pi/.nascreds,nofail,vers=1.0,x-systemd.automount 0 0
# Mount the NFS share
pi@raspberrypi:~ $ sudo mount -a
# Check if mounted
pi@raspberrypi:~ $ mount |grep Backup
# check for typoos
pi@raspberrypi:~ $ cat -v /etc/fstab
Configure Backup PiHole and Unifi
Prerequisite Rsync
- Install rsync libraries.
- NAS must be mounted (see Mount shared NFS)
sudo apt-get update
sudo apt-get install rsync
Create a script file in: /mnt/backup/config_backup.sh
#!/bin/bash
# backup Unifi + pihole backup files
# Controleren of we gebruiker root zijn!
if [[ ! $(whoami) =~ "root" ]]; then
echo ""
echo "*****************************************"
echo "*** Dit script moet draaien als root! ***"
echo "*****************************************"
echo ""
exit
fi
# set variables
unifi_backup_path=/mnt/backup/unifi
unifi_backup_source=/var/lib/unifi/backup/autobackup
pihole_backup_path=/mnt/backup/pihole
pihole_backup_source=/home/pi/pihole_backup
# backup once a month, retention 3 months is about 100 days
retention_days=100
# rsync Unifi backups to NAS
rsync -avh --no-o --no-g --include="*.unf" --exclude "*" $unifi_backup_source/ $unifi_backup_path/
# Need longer retention on NAS than local. Local retentyion is configured in Unifi Controller "backup Schedule"
find $unifi_backup_path/*.unf -mtime +$retention_days -type f -delete
# Pihole backup
# Create folder if not exist
if [ ! -d "$pihole_backup_source" ]; then
mkdir $pihole_backup_source
fi
cd $pihole_backup_source
# create a piHole backup
pihole -a -t
# rsync Unifi backups to NAS and remove local backup
rsync --remove-source-files -avh --no-o --no-g --include="pi-hole-raspberrypi-teleporter*" --exclude "*" $pihole_backup_source/ $pihole_backup_path/
# cleanup files older than retention
find $pihole_backup_path/pi-hole-raspberrypi-teleporter* -mtime +$retention_days -type f -delete
Schedule
getting the Crontab “code” https://crontab.guru/Script must run as ROOT. Therefor adjust the crontab for user ROOT.
sudo crontab -e
# add following to run every day at 1900h
0 19 * * * /mnt/backup/config_backup.sh