Asphyxiation by Oxygen Hands
Asphyxiation by Oxygen Hands
Piotr Gajos
About
Last.fm
GitHub
Instagram
LinkedIn
Automated Raspberry Pi Backup with rsync
Homelab
February 1, 2025 4 minute read
On this page
Backup Script
Excluding Stuff
Automate Backup
Don’t Forget
A couple of weeks ago, I made an attempt to reduce USB hard drive noise by idling it automatically. It didn’t work out that well – the drive seems to be possessed, turns itself on seemingly randomly, and the logs tell me nothing. I figured a better solution would be to mount it only when it’s needed, which coincides with my decision to use it just for backups.
This post is a part of my Homelab Series. See the index here.
Backup ScriptPermalink
I asked ChatGPT to write the backup script for me and I feel empty. It was just too easy. I missed the dopamine kick from figuring out the arcane arts of shell scripting, ugh. And because I was very lazy at prompting, it took several attempts to make it do exactly what I want. And again, instead of a sense of satisfaction or learning, it felt like a frustrating encounter with a incredibly talented but woefully unimaginative person.
And after that, the universe heard my request for a challenge and for two straight days, I wasn’t able to figure out why rsync wouldn’t accept my exclude lists. I finally figured out a solution but I have no idea why the original approach (using an array with --exclude= option) didn’t work.
Anyway, here’s the script. It stops all docker containers, mounts the external drive, performs 2 backup operations, restarts docker containers, unmounts the drive and sends a report to my phone. It even has a dry run mode – simply run sudo backup.sh test for a test run with more logging and some actions (like unmounting) disabled.
Copy code#!/bin/bash
Check for root privileges
[[ $EUID -ne 0 ]] && { echo "This script must be run as root."; exit 1; }
Piotr Gajos shares a post on February 1, 2025, detailing the automation of backup processes using a script for a Raspberry Pi in a "Homelab" setup. He recounts his struggles with a noisy USB hard drive and the challenge of writing backup scripts, particularly with the rsync command. The script stops Docker containers, mounts the external drive, performs backups, restarts the containers, and sends notifications using Pushover. It includes features like a dry run mode and automatic exclusion of unnecessary files via specified text files. Gajos also mentions scheduling the script to run weekly using crontab and offers a reminder to remove the backup drive's entry from /etc/fstab to prevent it from auto-mounting on boot.
backupDriveMount/Backups"scriptPath="
(dirname"
Define backup sources and destinations
declare -a backups=(
"/home/ home"
"/mnt/ssd-sandisk/ ssd-sandisk"
)
logMessage "Starting backup of description:{source} -> destination"rsyncOutput=(rsync -avzh dryRunFlag−−delete−−stats{options:+"options"} "source" "destination" 2>&1)
if [[ ? -ne 0 ]]; then
sendNotification "Pi Backup ❌" "Failed to back up description."logMessage"Errorduringbacku{description}."
logMessage "rsyncOutput"
else
# Extract the number of regular files transferred
numFilesTransferred=(echo "rsyncOutput" | awk '/Number of regular files transferred/{print NF}')
logMessage "Completed backup of description.Filestransferred:{numFilesTransferred}."
backupReport+="description:{numFilesTransferred} files\n"
fi
}
Unmount backup hard drive
unmountMedia() {
if ! sudo umount backupDriveMount;thenlogMessage"backupDriveMount is busy. Attempting lazy unmount."
if ! sudo umount -l backupDriveMount;thensendNotification"PiBackup❌""FailedtounmobackupDriveMount."
logMessage "Lazy unmount of backupDriveMountfailed."exit1backupDriveMount succeeded."
fi
else
logMessage "Unmounted $backupDriveMount."
fi
}
#############################################
if [[ "$1" != "test" ]]; then
Stop all docker containers
logMessage "Stopping all docker containers…"
docker stop $(docker ps -q)
fi
Mount backup hard drive
if ! mountpoint -q backupDriveMount;thenif!sudomountbackupDrive backupDriveMount;thensendNotification"PiBackup❌""FailedtomoubackupDrive at backupDriveMount."exit1fielselogMessagbackupDriveMount is already mounted."
fi
Ensure the backup directory exists
sudo mkdir -p $backupPath
Perform backups
for backupItem in "{backups[@]}"; do
IFS=' ' read -r source description <<< "backupItem"
backup "source""description"
done
if [[ "$1" != "test" ]]; then
Unmount backup drive
unmountMedia
Restart docker containers
logMessage "Restarting all docker containers."
docker start $(docker ps -a -q)
fi
Send success notification
sendNotification "Pi Backup ✅" "(printf"backupReport")"
logMessage "$backupReport"
Excluding StuffPermalink
If you want to exclude directories or files from backup, create a text file in the same directory as the script, which has to be named the same as backup destination with -exclude suffix. Backup script will pick it up automatically.
Example time! If you declare your backup like this:
Copy codedeclare -a backups=(
"/ root-sd-card"
)
Your exclude file needs to be named root-sd-card-exclude.txt. Here’s how I set up mine:
Copy code.caltrash
.stversions
DS_Store
/Downloads/
logs*
logs/*
Automate BackupPermalink
Thank god for crontab.guru. I want to run the script every Saturday at 1 AM and apparently this does it:
Copy codesudo crontab -e
Copy code0 1 * * 6 /home/pe8er/backup.sh
Don’t ForgetPermalink
To remove backup drive’s entry from /etc/fstab so that it doesn’t mount on boot anymore.