Skip to content

Instantly share code, notes, and snippets.

@refractalize
Last active August 29, 2015 14:02
Show Gist options
  • Save refractalize/ca2f84bada0ad62256af to your computer and use it in GitHub Desktop.
Save refractalize/ca2f84bada0ad62256af to your computer and use it in GitHub Desktop.
time machine style backups with rsync

Time Machine backups with Rsync

Usage

make-backup special-content special-content-backups

Will create the following directory structure:

special-content/
    dir1/
        file1
    file2

special-content-backups/
    ${YEAR}_${MONTH}_${DAY}T${HOUR}_${MINUTE}_${SECOND}/
        special-content/
            dir1/
                file1
            file2
    latest -> ${LATEST_BACKUP_DIRECTORY}

The second time you run it, it will place content in a new directory under the current date/time, but use hard links for those files that haven't changed, thereby being very space efficient.

Limitations

Currently only works on local file systems, i.e. won't copy files to other servers via SSH. I accept patches though ;).

#!/bin/sh
source=$1
backups=$2
date=`date -u "+%Y_%m_%dT%H_%M_%S"`
mkdir -p $backups
if [ -e $backups/latest ]
then
previous=`readlink $backups/latest`
rsync -avvh --link-dest=../$previous $source $backups/$date/
else
rsync -avvh $source $backups/$date/
fi
rm $backups/latest
ln -s $date $backups/latest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment