Skip to content

Instantly share code, notes, and snippets.

@BjarniRunar
Last active April 20, 2018 21:11
Show Gist options
  • Save BjarniRunar/bd34c1e2e8101f6ff237add5db43eb78 to your computer and use it in GitHub Desktop.
Save BjarniRunar/bd34c1e2e8101f6ff237add5db43eb78 to your computer and use it in GitHub Desktop.
Backup snapshots, using rsync. CLI user's Time Machine replacement.
#!/bin/bash
#
# Backup scriptlet. Public Domain. Made by Bjarni, https://bre.klaki.net/
#
# This will look in any device mounted on /media/$USER/, and if it has a directory
# named home-00000000 or data-00000000, it will use one of two rsync-based strategies
# to create a backup snapshot. To start using new media, just create one of those
# directories and run the script.
#
# I have two strategies because some of my backup disks are too small for my "Data"
# directory. If you don't have that constraint, you can delete half the script!
#
# Notes:
# - Before use, edit the paths and exclusion lists to match your needs.
# - Encrypted media is recommended!
# - The target file system must support hard links for this to be useful.
# - If you care about access times, read-only bind mount your /home/ directory!
# - Oh, look, it's Apple's Time Machine in a gist! :-)
#
sudo date
COPIED=0
TODAY=$(date +'%Y%m%d')
for USB_DISK in /media/"$USER"/* /mnt/backups/; do
if [ -d "$USB_DISK/home-00000000" ]; then
echo "Starting backup of home directories to $USB_DISK ..."
sudo rsync -rxa --info=progress2 \
--link-dest="$(ls -1d "$USB_DISK"/home-* |grep -v $TODAY |sort |tail -1)" \
--exclude '*.iso' \
--exclude '*.pyo' --exclude '*.pyc' --exclude '*~' \
--exclude 'cache*' --exclude .cache --exclude Cache \
--exclude tmp --exclude temp --exclude Temp \
/etc /home/bre /home/pkbre /home/bredroid "$USB_DISK"/home-$TODAY
COPIED=1
fi
if [ -d "$USB_DISK/data-00000000" ]; then
echo "Starting backup of home and data to $USB_DISK ..."
sudo rsync -rxa --info=progress2 \
--link-dest="$(ls -1d "$USB_DISK"/data-* |grep -v $TODAY |sort |tail -1)" \
--exclude Torrents \
--exclude '*.iso' \
--exclude '*.pyo' --exclude '*.pyc' --exclude '*~' \
--exclude 'cache\*' --exclude .cache --exclude Cache \
--exclude tmp --exclude temp --exclude Temp \
/etc /home/{bre,pkbre,bredroid,Data} "$USB_DISK"/data-$TODAY
COPIED=1
fi
done
[ "$COPIED" = 0 ] && {
echo "Backup disk is not mounted!"
exit 1
}
@BjarniRunar
Copy link
Author

Oh, and keeping track of whether you're running out of space and deleting old snapshots as necessary is left as an exercise for the reader. Different strokes for different folks, all that. 😄

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