Time Machine for Linux – Bash and Rsync

Recently i made “the switch”! Not in the way you might imagine though. Ever since 2003 i have been a passionate mac user. I still use macs at home and work and am still passionate about them.

Before 2003 i was playing a lot of games. Linux was and is something that i have been working/playing around with since 1996. To make a long story short: I wanted to use Linux and also play games (read dual boot linux/windows). The price tags on new macs ware also something that got me thinking about building a new shiny i7 based PC.

I am now back on a PC with Ubuntu Linux 9.04 and Windows 7. So far so god. One thing that i enjoy with Linux is that i am able to customize it to my needs. With Linux now being my primary OS (by choice) i had to fix some kind of backup for photos and all that other stuff on my PC that i would not like to loose in case of an accident. The first thing that came to my mind was rsync.

I used rsync to mirror my files on to an usb-disk. But after a few days i started to miss Time Machine which i was using when i had a Mac as my primary computer at home. Just mirroring files was not enough to give me that warm and cozy feeling. I needed to go back in time!

As a Linux user you can choose between a vast amount of backup solutions. But hey, where is the fun in that?! So i decided to make my own backup proggie. Just for fun and at the same time i also had a learning experience (i like Linux, can you tell?).

The backup script i wrote takes incremental backups with links to different directories, just like Time Machine does. You can delete old backup directories as you please. Restore files by copying them and so on.

If you are interested in a simple, yet useful backup solution for Linux feel free to use the code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
# Thorsten Rhau
# 2009-08-29
#
# Backup for Linux with rsync
 
# What we want to backup
SOURCE="/home/foo /etc"
 
# Where we want to backup
DESTINATION="/media/BACKUP"
 
# What NOT to backup
EXCLUDE="
.mozilla*
.gvfs
.wapi
.thumbnails
.gconf
.wine
.gimp*
.gnome*
.gconf*
.local
.VirtualBox
"
###
### DO NOT EDIT BELOW THIS !
###
 
# Testign if the destination directory exists
if [ ! -d $DESTINATION ]; then
  echo backup destination does not exist, please create it >&2
  exit 101
fi
 
# Testing if the 'rsync' command is on your system
RSYNC="`which rsync`"
 
if [ -z $RSYNC ]; then
  echo could not find rsync on your system >&2
  exit 102
fi
 
# Generating a string of things to exclude
for a in $EXCLUDE; do
  EXCLUDESTRING="$EXCLUDESTRING --exclude $a "
done
 
DATE=`date "+%Y-%m-%d_%H:%M:%S"`
 
# Executing 'rsync' as root to perform the backup
sudo rsync -aP --link-dest=$DESTINATION/current $EXCLUDESTRING \
$SOURCE $DESTINATION/backup-$DATE
sudo rm "$DESTINATION"/current
sudo ln -s $DESTINATION/backup-$DATE $DESTINATION/current
Aug 29th, 2009 | Posted in Computers
  1. Free tattoo designs
    Apr 14th, 2010 at 16:03 | #1

    I came across your website, i think your blog is cool, keep us posting.

  2. expekt
    May 10th, 2010 at 23:57 | #2

    I see a lot of interesting posts here. I have bookmarked for future referrence.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">