Emulate Time Machine using rsync

Many people using a Mac love Time Machine. It’s a simple and (often enough) powerful tool for performing incremental backups with file history. It relies on hard links.

For people using GNU/Linux or for Mac users who desire a bit more control it’s possible to simulate almost all of Time Machines features (except for the nice GUI) using rsync. I have written such a script and I might just as well share it. Please give feedback if you have any suggestions for how to improve it. As usually you have to tweak it to match your folder structure. Please note that I do not take any legal responsibility regarding how it’s used or what it may do to your data. I just believe it works!

If you use Microsoft Windows then checkout DeltaCopy which is a popular front end to rsync.

Update:
The script below is a quick and simple hack but it seems to work just fine. A more complete and mature solution for *NIX that is also based on rsync and hard links is rsnapshot.

#!/bin/bash

dirs2back=(~/Music ~/Documents)
destdirs=(/Volumes/media)

function back {
BDIR=$1

if [ -d $BDIR ]
then
	foofile="$BDIR/foofile"
	if touch "$foofile"
	then
		echo $BDIR is writable

		if ln $foofile ${foofile}_lnk
		then
			rm ${foofile}_lnk
			rm "$foofile"
		else
			echo "Error: Sorry this filesystem does not support hard linking"
			rm "$foofile"
			exit
		fi

		RDIR=$BDIR/rsyncmachine
			
		TODAY=`date | awk '{print $4 "_" $3 "_" $5}' | sed s/://g` 
		TODAY=$RDIR/$TODAY
		echo $TODAY
	
		LASTTXT=$RDIR/last.txt

		if [ -d $RDIR ]; then
			echo "Running incremential backup"

			if [ -e $LASTTXT ]; then
				echo "Performing incremential backup"
				YESTERDAY=`cat $LASTTXT`
				echo $YESTERDAY

				if [ -d $YESTERDAY ]; then
					for item in ${dirs2back[*]}
					do
						rsync --progress -av --include=".*" --link-dest $YESTERDAY $item "$TODAY"
					done

					echo $TODAY > $LASTTXT
				else
					echo "Error: $YESTERDAY is not a directory"
					exit
				fi
			else
				echo "Error: No last backup, please update last.txt manually!"
			fi
		else
			echo "First time running rsyncmachine"
			echo $TODAY

			mkdir -p $TODAY

			for item in ${dirs2back[*]}
			do
				rsync --progress -av --include=".*" $item "$TODAY"
			done

			echo $TODAY > $LASTTXT
		fi

	else
		echo "$2 is not writable, not syncing"
	fi

else
	echo "Error: $BDIR is not connected"
fi
}

for item in ${destdirs[*]}
do
	back $item
done
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

4 Responses to Emulate Time Machine using rsync

  1. pawel.kw says:

    Hi,

    Have you tried rsnapshot? I suppose it does something like you’re doing here. I was looking for a backup method, being close to writing something that does what I want I found rsnapshot. It’s also based on rsync, so doing incremental backups, and ready to use, so I though ‘no point re-inventing the wheel..’

    • kristianr says:

      Yes I have heard about it. I don’t use either, I rely on git, time machine and git-annex for my data management. I wrote the script above just for fun but it should work fine, it’s very light weight and easy to get started with🙂

  2. kristianr says:

    Sometimes I reinvent the wheel just to learn how the wheel works and why it’s round, not square.

  3. ISXPRO says:

    This does not work. It simply duplicates the backup each time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s