20090907

My backup script in Mac/Linux

My backup script in Mac/Linux

#!/bin/sh


# This script backs up a set of files and directories incrementally,
# storing only the modified files in a folder-per-day basis. Each time
# you run it, the "current" directory in the backup server gets
# refreshed to the newest copy, and the previous version of a modified
# file is stored in a directory corresponding to "today" (20090905,
# for instance)

# Root to be safeguarded
BDIR=/home/...?

# File and directory patterns, to avoid saving all those useless .dat
# files every day. More on this later.
INCLUDES=Backup.incl

# Backup server
BSERVER=your.backup.server

########################################################################

BACKUPDIR=`date +%Y%m%d`
OPTS="--force --ignore-errors --delete-excluded --include-from=$INCLUDES
--delete --backup --backup-dir=~/DESTINATIONCOPYDIR/$BACKUPDIR/ -az"

export PATH=$PATH:/bin:/usr/bin:/usr/local/bin

# Generate the "backup dir"
[ -d /home/YOURHOMEDIR/emptydir ] || mkdir /home/YOURHOMEDIR/emptydir
rsync --delete -a /home/YOURHOMEDIR/emptydir/ $BSERVER:DESTINATIONCOPYDIR/$BACKUPDIR/
rmdir /home/YOURHOMEDIR/emptydir

# Copy
rsync $OPTS $BDIR $BSERVER:DESTINATIONCOPYDIR/current

# Uncomment the following line if you have Growl in a Mac
# growlnotify -n 'Backup' -m 'Backup finished correctly' -s

The pattern file should look like this. An exclamation mark as a first line. A minus sign avoids certain directory/files, a plus adds a directory (start by adding the directory), then add the pattern of files. If you want to include all subdirectories, you have to explicitly state so.

!
- DontWantPDFsFromThisDirectory/*.pdf
+ Directory
+ DirectoryWithSubfolders
+ DirectoryWithSubfolders/**/
+ Directory/*.tex


You may also like some Mac/Linux related posts:
Written by Ruben Berenguel