Skip to content

Instantly share code, notes, and snippets.

@aleclarson
Last active May 1, 2024 00:23
Show Gist options
  • Save aleclarson/1badd2a92bec6d1b34125b712b3a31d0 to your computer and use it in GitHub Desktop.
Save aleclarson/1badd2a92bec6d1b34125b712b3a31d0 to your computer and use it in GitHub Desktop.
rsync-backup

Selective backup for OSX

Using the awesome rsync-time-backup script by @laurent22, OSX users can selectively backup any directory on their computer.

  1. Download rsync_tmbackup.sh into your ~/bin directory:
wget https://raw.githubusercontent.com/laurent22/rsync-time-backup/master/rsync_tmbackup.sh -O ~/bin/rsync-backup
chmod +x ~/bin/rsync-backup
  1. Download backup.sh into your ~/bin directory:
wget https://gist.githubusercontent.com/aleclarson/1badd2a92bec6d1b34125b712b3a31d0/raw/backup.sh -O ~/bin/backup
chmod +x ~/bin/backup
  1. Ensure node is installed on your computer. 🙂

  2. Connect your external hard drive, create a partition named "Backup", and run this:

mkdir -p "/Volumes/Backup/Selective-Backups"
  1. Run backup ./path/to/dir from anywhere to create a backup! 😎

  2. Wait a few minutes if the directory has a lot of files... 😜

Once finished, your backups can be found in the Selective-Backups directory on your external hard drive! Each backup is named with its basename and a SHA256 hash of its full path (to avoid collisions).

To know the original full path of the backup, you can run:

cat "/Volumes/Backup/Selective-Backups/$BACKUP_NAME/.backup-of"

Let me know if this helped you! 😘

# Selective backup for OSX
SRC=`node -e "
console.log(path.resolve('$PWD', '$1'));
"`
NAME=`node -e "
var src = '$SRC';
var hash = crypto.createHash('sha256')
.update(src).digest('hex').slice(0, 20);
console.log(path.basename(src) + '-' + hash);
"`
# Setup the destination directory.
DEST="/Volumes/Backup/Selective-Backups/$NAME"
mkdir -p "$DEST"
touch "$DEST/backup.marker"
# Store the original file path.
echo "$SRC" > "$DEST/.backup-of"
# Start the backup.
rsync-backup "$SRC" "$DEST"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment