Created
October 2, 2025 16:28
-
-
Save laamaa/339b800086a83fbb66e2c434dc78a77d to your computer and use it in GitHub Desktop.
A bash script that creates incremental backups of an SD card on macOS using rsync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # Incremental backup script for SD card on macOS with rotation | |
| # | |
| # === CONFIGURATION === | |
| SDCARD="/Volumes/M8" # Mount point of the SD card | |
| BACKUPDIR="$HOME/Backups/m8-sd-backups" # Where backups are stored | |
| DATE=$(date +%Y-%m-%d-%H%M%S) # Timestamp for snapshot folder | |
| DEST="$BACKUPDIR/backup-$DATE" # New backup folder | |
| LATEST="$BACKUPDIR/latest" # Symlink to last backup | |
| KEEP=5 # Number of backups to keep | |
| # === CHECKS === | |
| if [ ! -d "$SDCARD" ]; then | |
| echo "Error: SD card not mounted at $SDCARD" | |
| exit 1 | |
| fi | |
| echo "$(date): Starting backup of $SDCARD to $DEST" | |
| mkdir -p "$BACKUPDIR" | |
| # === RSYNC BACKUP === | |
| if [ -d "$LATEST" ]; then | |
| rsync -a --delete --link-dest="$LATEST" --progress "$SDCARD/" "$DEST/" | |
| else | |
| rsync -a --delete --progress "$SDCARD/" "$DEST/" | |
| fi | |
| # === UPDATE SYMLINK === | |
| rm -f "$LATEST" | |
| ln -s "$DEST" "$LATEST" | |
| if [ $? -eq 0 ]; then | |
| echo "Backup completed successfully" | |
| else | |
| echo "Backup encountered an error, check the logs for details" | |
| exit 1 | |
| fi | |
| # === ROTATE OLD BACKUPS === | |
| cd "$BACKUPDIR" || exit 1 | |
| BACKUPS=(backup-*) | |
| COUNT=${#BACKUPS[@]} | |
| if [ "$COUNT" -gt "$KEEP" ]; then | |
| REMOVE=$((COUNT - KEEP)) | |
| echo "Removing $REMOVE old backup(s)..." | |
| for OLD in "${BACKUPS[@]:0:$REMOVE}"; do | |
| rm -rf "$OLD" | |
| echo "Deleted $OLD" | |
| done | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment