Skip to content

Instantly share code, notes, and snippets.

@chuckhoupt
Last active February 20, 2023 18:48
Show Gist options
  • Select an option

  • Save chuckhoupt/6265268 to your computer and use it in GitHub Desktop.

Select an option

Save chuckhoupt/6265268 to your computer and use it in GitHub Desktop.
A shell script to backup a user's recently modified files to a remote server via rsync.
#!/bin/bash
set -eux
BACKUP_ACCOUNT=backupuser@backup.example.com
MAXSIZE=45000000000
# Wait for the net to come up
while [ ! $(ifconfig | grep 'status: active') ]; do
sleep 1
done
HOST=$(hostname -s)
DAYS=180
SHADOW_DIR=/tmp/$HOST-$USER-recent
if true
then
rm -rf $SHADOW_DIR
mkdir $SHADOW_DIR
date
find "$HOME" \
-path "$HOME/Library" -prune -or \
-path "$HOME/Pictures/iPhoto Library/Thumbnails" -prune -or \
-path "$HOME/Music/iTunes" -prune -or \
-path "$HOME/.Trash" -prune -or \
-path "$HOME/.m2" -prune -or \
-path "$HOME/Downloads" -prune -or \
-name .svn -prune -or \
-name CVS -prune -or \
-name 'iPod Photo Cache' -prune -or \
-not -type d \
-not -name .DS_Store \
-not -name '*.o' \
-not -name '*.class' \
-not -name '*.jar' \
-mtime -$DAYS \
-print \
| cpio -pdl $SHADOW_DIR
date
du -sk $SHADOW_DIR
fi
# omit dir times, because cpio doesn't preserve dir times, causing re-sync every time
rsync -avz --delete --stats --omit-dir-times \
"$SHADOW_DIR"/ $BACKUP_ACCOUNT:"$HOST-$USER-recent"
rm -rf $SHADOW_DIR

Backup-Recent.command

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment