User Tools

Site Tools


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

# backup : a backup script who saves space using links when files doesn't change
# configure source and destination
# properly
exit 0
# safe settings
umask 077
# 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"
# 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/"  \
linux/backup.txt · Last modified: 2014/12/24 10:42 (external edit)