0

I have a linux (Ubuntu 12.04) web server and I want to create a cronjob to generate a differential backup, locally (in the same machine).

Is that possible? With differential I mean that the first time it copies everything, but the following times it only copies new files. But I also want to have many points in time, I mean at least 3 or 4 backups from which I can restore everything.

I have checked this page but there are too many different tools, and I guess each one has its own gotchas: https://help.ubuntu.com/community/BackupYourSystem#Types_of_Backup

For example, I've been told that storing .tar.gz has a limit in size.

If you have any simple suggestion, even if it's not differential, it's welcome.

2 Answers 2

1

Assuming there is a backup disk in /media/mydisk and I want backup /home/user, this little script will let you keep 10 history's backup, but keeping only different files more than one time.

This need that backup disk is formated with a POSIX filesystem, like Ext4, XFS or else, but no ntfs, nor fat!

#!/bin/bash
backRepo=/media/mydisk
backSrce=/home/user
backDest=home
backCopy=copy
backCount=9

[ -d "$backRepo/$backDest" ] || mkdir "$backRepo/$backDest"

cd $backSrce || exit 1
rsync -ax --delete --exclude '*~' --exclude '.DStore' . "$backRepo/$backDest/."

cd $backRepo
[ -d "$backCopy.$backCount" ] && rm -fR "$backCopy.$backCount"
for ((i=$backCount;i--;));do
    [ -d "$backCopy.$i" ] && mv "$backCopy.$i" "$backCopy.$((i+1))"
  done
((i++))

cp -al $backDest $backCopy.$i

This (cp -al) make hardlinks of files instead of copying it. So there are no more space used.

Rsync will re-create (copy on write) each modified files, so rotated backup keep old version.

4
  • Interesting, thanks. I have some questions: What is .DStore? And it seems like this script will keep ~deleted files forever, is that right? Dec 30, 2012 at 23:24
  • .DStore and *~ stand there for sample only. This will exclude all .DStore files (some Mac OS finder config cache files) and all file which name end with a tilde ~ (backup for editors like emacs). Dec 30, 2012 at 23:31
  • Not forever, after 10 rotation, older will be definitively deleted. Dec 30, 2012 at 23:33
  • I love this script, alternative could use datepart, as i=$(date +%A) instead of a simple counter, for daily rotation or date +%m for monthly, etc... Dec 30, 2012 at 23:38
1

rsync is a great tool for copying files, it can copy only the change bits in a file and it can backup the destination files before overwriting.

You can easily edit your crontab to run rsync periodically for your main backup. Then you can copy the backups for the changed files and you'll have more time points. Or you can copy the backed up data before running rsync to have complete image for each time point.

Unless you are going to later copy the files elsewhere, I don't think it is necessary to use tar.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .