Table of Contents

A savvy space backup system for linux

The idea

Recently, in Linux Journal I came across with a backup system that really shocked me: the first time, it uses rsync to make a copy of all the data.

The second day, it uses cp to create a copy of all the data by creating a new link to all the files that existed.

After this, it uses rsync again: because rsync not delete the changed files, but unlink them, only the changed files were copied -and therefore consume disk space-.

Conclusion: you have some copies of day-1, day-2, etc keeping only links to the files that have changed.

Let's go for it

#!/bin/bash
#
# backup : a backup script who saves space using links when files doesn't change
#
#
 
# configure source and destination
# properly
echo "CONFIGURATION PENDING"
exit 0
 
 
source=/home/xxx/tmp/10/source
dest=/home/xxx/tmp/10/dest
 
 
# safe settings
umask 077
 
PATH="/usr/bin:/bin:/sbin"
 
# write a message (to a file, for screen, etc.)
function msg
{
  echo $1
} #msg
 
function finish
{
  # remove possibly open files
  echo 'Finished.'
}
 
function fail
{
  msg $0"Have to finish unexpectedly"
  error_result=$?
  finish
}
 
#
# Here starts the program
# -----------------------
#
 
msg "Trapping signals SIGHUP, SIGINT, SIGQUIT, SIGTERM"
trap "fail" 1 2 3 15
 
 
rm -rf  "$dest/07"
mv "$dest/06" "$dest/07"
mv "$dest/05" "$dest/06"
mv "$dest/04" "$dest/05"
mv "$dest/03" "$dest/04"
mv "$dest/02" "$dest/03"
mv "$dest/01" "$dest/02"
 
# -a: preserves dates and times, permissions, etc.
# -l: create hard links instead of copying
cp -al "$dest/current" "$dest/01"
 
 
rsync -avz --delete-after \
           "$source/"  \
           "$dest/current"
 
finish