Personal tools

Skip to content. | Skip to navigation

This Logo Viewlet registered to qPloneSkinTechlight
You are here: Home Snippets Rsync --link-dest wrapper script

Rsync --link-dest wrapper script

— filed under: ,

Poor man's incremental backup

Intro

Rsync has this neat --link-dest option allow you to fool around using hard-links on your filesystem. You'll find all details on this feature in the man page or on the net, but here is a script using it...

Download here

Code

#!/bin/sh
#
# Script to use nifty hard-link feature (--link-dest) of rsync providing
# poor man's incremental backup
#
# What it does: 
#
#    Every-time the script is run a new copy of the src-dir made, but every
#    file that has not changed since the last run is not copied but hard-linked
#    from the previous copy. Thus saving enormous amounts of data in the right
#    conditions.
# 
# How to use : 
#
#    Set the right vars further on (no to command-line options), run from cron
#    and check the exit code!
#
# Note on configurable Vars : 
#
# SRCDIR    = Specify the dir that will be copied from, check rsync doc on
#             trailing slash.
#             For copy from remote use something like user@host:/src/dir and
#             set the correct RSYNC_OPT
#
# DSTDIR    = Destination DIR to copy files to, must exist and must be local (not
#             on remote host)
#
# RSYNC_OPT = allows to set some extra rsync opt, useful might be "-e ssh" for
#             remote source or "-v" if you want to log what is copied
#
# VER       = number of version you want to keep
#
#################################################################
# 
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Wouter Godefroy 01/2011 - wgodefro at users.sourceforge.net
#

### Configurables

SRCDIR=/tmp/spath
DSTDIR=/tmp/dpath
RSYNC_OPT="" 
VER=60


### Lesser configurables

PATH=/bin:/sbin:/usr/bin/:/usr/sbin
export PATH

RUNNING_COPY_DIR=running_copy

### Here we go

if [ ! -d $DSTDIR ]; then
    echo "ERROR: Destination dir $DSTDIR does not seem to exist!"
    exit 1
fi

cd $DSTDIR

# Clean-up prev run if exist

[ -d $RUNNING_COPY_DIR ] && rm -rf $RUNNING_COPY_DIR

# Check we're doing a first run

if [ ! -d 1 ] ; then
    echo "This is a first run, making initial copy!"
    rsync -ar $RSYNC_OPT $SRCDIR 1
    exit 0
fi

# Copy to temp-dir 

rsync -ar --delete --link-dest=$PWD/1 $RSYNC_OPT $SRCDIR $RUNNING_COPY_DIR
RETVAL=$?

if [ $RETVAL -ne 0 -a $RETVAL -ne 23 -a $RETVAL -ne 24 ]; then
    echo "ERROR: Copy failed (non-zero return code from rsync)"
    exit 2
fi

# Copy was ok, move around dirs

COUNT=`expr $VER - 1`
NEXTD=$VER

[ -d $NEXTD ] && rm -rf $NEXTD

while [ $COUNT -ge 1 ]; do
    [ -d $COUNT ] && mv $COUNT $NEXTD

    NEXTD=$COUNT
    COUNT=`expr $COUNT - 1`
done

mv $RUNNING_COPY_DIR 1
Document Actions