Local Backup with rsync and bash
Introduction: 15 gigabytes is not enough
Not having all the projects and assignments I did in college neatly organized in a single directory is one of my biggest regrets as someone who spends most of his life in front of a computer. Whether it was because of the infinite Google Drive storage my university provided, or the countless flash drives, emails, and random files I sent and received through every possible communication channel during those five years.
As with all good things, I knew the infinite drive wouldn’t last forever, and as soon as I graduated, I transferred everything to my personal Google Drive.
Big mistake
In one second, 99% of my free tier storage was gone, filled with messy, duplicated, and poorly formatted files, with the bonus that not everything had actually been transferred. The solution: throw everything onto my computer and pray the hard drive doesn’t melt.
For about two years, it actually didn’t melt, and the data stayed there, but the paranoia of losing everything started to take up a few bytes of my mental hard drive.
Until last week...
Hardware recycling
Eventually, a 256 GB SSD from an old PC landed on my desk. It was the perfect excuse to finally bring some order to the chaos and, at the same time, stop depending on Google Drive.
With a cheap external case, I turned that loose SSD into an external drive, and with a bit of bash, decided to write my own simple, straightforward local backup script. And to make it even better, I decided not only to back up my college files but also my code repositories and my Obsidian vault, my semi-second brain.
Rsync and paranoia
My idea was to have two mirrors:
From PC to SSD, the main backup. From SSD to PC, a local copy, because redundancy is never too much.
This way, even if the SSD dies, I still have a version on my computer, and vice versa.
Without reinventing the wheel, I chose Rsync, the command-line utility for syncing and transferring files between locations.
The script
The script below mounts the SSD, if it isn’t already, synchronizes the configured directories, and generates logs for each run. Simple, direct, and efficient.
#!/bin/bash
# === Configurations ===
SSD_MOUNT="/run/media/SSD" # the directory where I mount the SSD
SSD_UUID="062C43D82C43C303" # obtained via lsblk -f
PC_BASE="/home/me" # my home folder
BACKUP_DIR="$PC_BASE/Backup"
LOGFILE="$PC_BASE/sync-backup.log"
# === Functions ===
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOGFILE"
}
check_or_mount() {
if mountpoint -q "$SSD_MOUNT"; then
log "SSD already mounted at $SSD_MOUNT"
else
log "SSD not mounted, attempting to mount..."
sudo mount UUID="$SSD_UUID" "$SSD_MOUNT"
if mountpoint -q "$SSD_MOUNT"; then
log "SSD mounted successfully."
else
log "Failed to mount SSD at $SSD_MOUNT, aborting."
exit 1
fi
fi
}
# === Execution ===
log "=== Starting sync ==="
check_or_mount
log "PC → SSD (Repositories)"
rsync -avh --delete --exclude 'node_modules' "$PC_BASE/Work/Repositories/" "$SSD_MOUNT/Repositories/" | tee -a "$LOGFILE"
log "PC → SSD (Obsidian)"
rsync -avh --delete "$PC_BASE/Work/Obsidian/" "$SSD_MOUNT/Obsidian/" | tee -a "$LOGFILE"
log "SSD → PC (Local backup)"
rsync -avh --delete "$SSD_MOUNT/" "$BACKUP_DIR/" | tee -a "$LOGFILE"
log "=== Sync completed ==="
Running the script is simple, just grant execute permission.
chmod +x local-sync.sh # or whatever name you gave it
./local-sync.sh
The small details
Fixed UUID, avoids headaches when the SSD changes its mount path. Logs, each execution is recorded, so you know what got updated.
--exclude 'node_modules', saves time and space.
--delete, keeps mirrors truly in sync, but use with caution.
To run automatically, you can add this script to cron and let the system handle the rest.
Conclusion: goodbye infinite drive
With a small SSD and a 40-line script, I built a local backup routine, fast and 100% under my control. No subscriptions, no slow uploads, and no dependency on any big tech goodwill.
Sometimes progress is just deciding to take care of your own chaos, but I still miss that infinite drive, maybe I’ll enroll in a new college somewhere again...
- ← Previous
Discomfort Zone and Arch Linux - Omarchy OS - Next →
New blog layout (again)