Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

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.

share|improve this question
up vote 1 down vote accepted

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!


[ -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))"

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.

share|improve this answer
Interesting, thanks. I have some questions: What is .DStore? And it seems like this script will keep ~deleted files forever, is that right? – ChocoDeveloper Dec 30 '12 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). – F. Hauri Dec 30 '12 at 23:31
Not forever, after 10 rotation, older will be definitively deleted. – F. Hauri Dec 30 '12 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... – F. Hauri Dec 30 '12 at 23:38

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.

share|improve this answer

You must log in to answer this question.

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