Time Machine for Ubuntu

From DiLab
Revision as of 12:48, 26 June 2009 by Girts (talk | contribs)
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) Sync 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