To back up Pi-hole on a schedule, you can use its built-in configuration export tools combined with a cron job. This allows you to save the configuration and restore it if needed. Here’s how to set it up:
1. Pi-hole Configuration Backup
Pi-hole provides a pihole -a command to export and import settings.
Backup Command:
To back up Pi-hole settings, run:
pihole -a -t
This creates a tarball of Pi-hole settings in the current directory, containing:
- DNS configuration
- Whitelist/Blacklist
- Static DHCP leases
- Query logs (optional)
Restore Command:
To restore from the backup:
pihole -a -r
2. Automate Backup with a Cron Job
Step 1: Create a Backup Directory
Choose where to store the backup files (e.g., /home/pi/pihole-backups):
mkdir -p /home/pi/pihole-backups
Step 2: Write a Backup Script
Create a script to handle the backup process.
Example script: /home/pi/backup_pihole.sh
#!/bin/bash
# Define backup directory
BACKUP_DIR="/home/pi/pihole-backups"
# Create a timestamp
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
# Backup file path
BACKUP_FILE="$BACKUP_DIR/pihole-backup-$TIMESTAMP.tar.gz"
# Run Pi-hole backup
pihole -a -t > /dev/null 2>&1
# Move the tarball to the backup directory with a timestamp
mv /etc/pihole/*.tar.gz "$BACKUP_FILE"
# Delete old backups (optional, e.g., keep 7 days)
find "$BACKUP_DIR" -type f -mtime +7 -exec rm {} \;
Make the script executable:
chmod +x /home/pi/backup_pihole.sh
Step 3: Add a Cron Job
Edit the crontab to schedule the script:
crontab -e
Add a line to schedule the script (e.g., daily at 2:00 AM):
0 2 * * * /home/pi/backup_pihole.sh
3. (Optional) Sync Backups to Another Location
For additional safety, copy backups to an external location (e.g., NAS, cloud storage, or another server).
Example: Use rsync to Copy Backups
Add the following line to the script:
rsync -av --delete /home/pi/pihole-backups/ user@remote-server:/backup-location/
4. Verify Backup and Restore
- Run the script manually to test:
/home/pi/backup_pihole.sh - Confirm the backup file exists in
/home/pi/pihole-backups. - Test restoring using:
pihole -a -r
With this setup, Pi-hole backups will occur automatically, and you’ll have a reliable way to restore your configuration when needed. Let me know if you’d like help customizing the process!