Time Machine-style Backups with rsync — Sam Hewitt

Date:

Share:


Posted on 05 Jun 2018

Data Hoarding 101

I have digital backups of data, especially of my photos, going back almost a decade (I’m not that old) but it’s only recently that I got into creating time-snapshots of my backups, after I mistakenly deleted a bunch of (non-crucial) personal files with a stray rsync --delete on a directory I had not backed-up.

My home server used be compatible with macOS’s Time Machine and when I kept most things on my Mac that was useful, but I changed my data hoarding habits and that solution started to not be the optimal one.

Time Based Snapshots Using rsync

rsync has got to be one of my favourite and most used utilities. I use it constantly to transfer files between machine on my local network (and remote ones) but I’ve discovered it’s very useful as a backup tool. The following is a short script that uses rsync to create snapshots of the my data directory to another directory (they are mount points on two different drives):

#!/bin/sh

TIMESTAMP=`date "+%Y-%m-%dT%H-%M-%S"`
USER=user
SOURCEDIR=/var/data/backups
TARGETDIR=/var/redundancy

# Create new backup using rsync and output to log
rsync -avPh --delete --link-dest=$TARGETDIR/current $SOURCEDIR/$USER/ $TARGETDIR/$USER-$TIMESTAMP > /var/log/rsync/$TIMESTAMP.log 2>&1

# check exit status to see if backup failed
if [ "$?" = 0 ]; then
    # Remove link to current backup
    rm -f $TARGETDIR/current
    # Create link to the newest backup
    ln -s $TARGETDIR/$USER-$TIMESTAMP $TARGETDIR/current
else
    # Rename directory if failed
    mv $TARGETDIR/$USER-$TIMESTAMP $TARGETDIR/failed-$USER-$TIMESTAMP
fi

Essentially this script creates incremental copies of backups for a given user on the system by comparing any changes to that user’s data directory ($SOURCEDIR) with the current backup ($TARGETDIR) and making a new snapshot of any changes with the date.

Now all that remains is to save this script somewhere and create a cron job to run it on an automatic interval:

# daily at 5am
0 5 * * * bash /usr/local/bin/rsync-time-machine

This method uses rsync’s --link-dest option to create hard-links so that isn’t duplicative of file data (the contents are only stored once, so you don’t use twice as much) and you can delete snapshots at will. (The links do still take up some space but it’s relatively insignificant.)

Be careful when deleting old snapshots. A new full backup of should be made before further incremental ones.
I have to give a shoutout to Mike Rubel whose post was helpful in coming up with this solution.

Source link

Subscribe to our magazine

━ more like this

The AI deployment gap is widening, don’t get left behind

It’s interesting to see how much AI adoption has accelerated over the last year. Our latest research shows that 82% of senior leaders invested in...

Error'd: Spacetime Anomalies

Do we need better verb tenses to describe a counterfactual present from the future perspective? Any trained linguists in the audience, please helped out. Reinier...

Hailey Bieber Wore the Grown-Up Naked Dress at the 2026 Grammys

Tonight, the spotlight is on the 2026 Grammys, one of music’s most exciting award shows of the year. It’s being hosted by Trevor Noah...

Are cosmetics safer in Europe than in the US? Episode 101

We had a technical glitch this week & Perry is on vacation so enjoy a show from the archives…episode 101 Question of the week: How...

Fifteen Years Ago Today, Craig Ferguson Spends his Entire Show Interviewing Dr. Cornel West about Black History Month

I try to watch this every year. Hearing Dr. West talk about race, identity, what it means to...