Skip to content

Instantly share code, notes, and snippets.

@maca
Created July 20, 2012 20:30
Show Gist options
  • Save maca/3153054 to your computer and use it in GitHub Desktop.
Save maca/3153054 to your computer and use it in GitHub Desktop.
Script to emulate time machine snapshot based backup.
#!/bin/bash
## my own rsync-based snapshot-style backup procedure
## (cc) marcio rps AT gmail.com
## modified by github.com/maca
lock=/tmp/snapshot-lock
if [ -f $lock ] ; then
echo "snapshot: a lock file seems to exist $lock"
exit
fi
touch $lock
# config vars
host=<your-backup-server>
port=22
src="/home/$(whoami)/" #dont forget trailing slash!
snap="/backup/snapshots/$(whoami)"
opts="-zia --delete --delay-updates"
minchanges=200
# run this process with real low priority
ionice -c 3 -p $$
renice +12 -p $$
# sync
ssh -p $port $host "mkdir -p $snap"
rsync $opts --rsh="ssh -p$port" $src $host:$snap/latest | ssh -p $port $host "cat - >> $snap/rsync.log"
# check if enough has changed and if so
# make a hardlinked copy named as the date
datetag=$(date +%y-%m-%d-%h-%m)
ssh -p $port $host "
sleep 20
count=\`wc -l $snap/rsync.log | cut -d ' ' -f1\`
if [ \$count -gt $minchanges ] ; then
if [ ! -e $snap/$datetag ] ; then
cp -al $snap/latest $snap/$datetag
mv $snap/rsync.log $snap/$datetag
fi
fi
"
rm $lock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment