Time Machine for Ubuntu

From DiLab
Jump to: navigation, search

Here I described scripts I created to make backups on Ubuntu, similar to those used by Time Machine on Mac OSX.

The template for this script is taken from Mac Geekery.

WARNING: There is absolutely no guarantee, that this script is working, moreover, there is no guarantee, that it will not harm your computer!

Things to be done:

  1. Age old files
  2. Archive new files

Aging old files

The snapshots will be kept in directories named snap.0 (newest), snap.1, snap.2, etc. Aging old files means making hard links of old files (duplicating them). Aging is accomplished with the following script:

#!/bin/bash

unset PATH # suggestion from H. Milz: avoid accidental use of $PATH

SNAPSHOT_RW=/path/to/your/backup/directory

# change these paths to your needs, when desired
ECHO=/bin/echo
RM=/bin/rm
MV=/bin/mv
CP=/bin/cp
TOUCH=/usr/bin/touch
CPIO=/bin/cpio
RSYNC=/usr/bin/rsync

# how many snapshots will be kept
SNAP_COUNT=56

if [ -d $SNAPSHOT_RW ]; then

# rotating the snapshots
# step 1: delete the oldest snapshot, if it exists:
    if [ -d $SNAPSHOT_RW/snap.$SNAP_COUNT ]; then
        $ECHO Clearing oldest snapshot...
        $RM -rf $SNAPSHOT_RW/snap.$SNAP_COUNT
    fi
# step 2: shift the middle snapshots(s) back by one, if they exist
    ip=$SNAP_COUNT;
    i=$(($ip-1));
    while [ $i -ge 1 ]
    do
        if [ -d $SNAPSHOT_RW/snap.$i ]; then
            $ECHO "Aging snap.$i..."
            $MV $SNAPSHOT_RW/snap.$i $SNAPSHOT_RW/snap.$ip
        fi
        i=$(($i-1))
        ip=$(($ip-1))
    done

# step 3: make a hard-link-only (except for dirs) copy of the latest snapshot, # if that exists
    if [ -d $SNAPSHOT_RW/snap.0 ] ; then
        $ECHO Aging snap.0...
        cd $SNAPSHOT_RW/snap.0 && /usr/bin/find . -print | $CPIO -dplm $SNAPSHOT_RW/snap.1
    fi

else 
    $ECHO $SNAPSHOT_RW not present. Dying.
fi

Archiving new files

#!/bin/bash

unset PATH # suggestion from H. Milz: avoid accidental use of $PATH

SNAP_FROM=/path/to/directory/which/has/to/be/backed/up
SNAPSHOT_RW=/path/to/your/backup/directory
EXCLUDES=/file/containing/excludes/one/in/each/line
#exclude list file may contain wildcards. For example,
#*.AVI - do not backup AVI files
#Pictures/2005 - do not backup directory "Pictures/2005"

ECHO=/bin/echo
RM=/bin/rm
MV=/bin/mv
CP=/bin/cp
TOUCH=/usr/bin/touch
CPIO=/bin/cpio
RSYNC=/usr/bin/rsync
SNAP_COUNT=56

if [ -d $SNAPSHOT_RW ]; then

# rotating the snapshots
# step 1: delete the oldest snapshot, if it exists:
    if [ -d $SNAPSHOT_RW/snap.$SNAP_COUNT ]; then
        $ECHO Clearing oldest snapshot...
        $RM -rf $SNAPSHOT_RW/snap.$SNAP_COUNT
    fi
# step 2: shift the middle snapshots(s) back by one, if they exist
    ip=$SNAP_COUNT;
    i=$(($ip-1));
    while [ $i -ge 1 ]
    do
        if [ -d $SNAPSHOT_RW/snap.$i ]; then
            $ECHO "Aging snap.$i..."
            $MV $SNAPSHOT_RW/snap.$i $SNAPSHOT_RW/snap.$ip
        fi
        i=$(($i-1))
        ip=$(($ip-1))
    done

# step 3: make a hard-link-only (except for dirs) copy of the latest snapshot, # if that exists
    if [ -d $SNAPSHOT_RW/snap.0 ] ; then
        $ECHO Aging snap.0...
        cd $SNAPSHOT_RW/snap.0 && /usr/bin/find . -print | $CPIO -dplm $SNAPSHOT_RW/snap.1
    fi

# step 4: rsync from the system into the latest snapshot (notice that
# rsync behaves like cp —remove-destination by default, so the destination
# is unlinked first. If it were not so, this would copy over the other
# snapshot(s) too! 

    $RSYNC -va --delete --delete-excluded $SNAP_FROM $SNAPSHOT_RW/snap.0 --exclude-from=$EXCLUDES

# step 5: update the mtime of snap.0 to reflect the snapshot time

    $TOUCH $SNAPSHOT_RW/snap.0

# and thats it!