Backup & versioning with RSYNC

You may already know rsync – the powerful tool which you use to synchronize files from one location to another. I am using it on every Linux server that needs scheduled backups, but this time I had to not only backup some files, but also to keep track of previous versions, due to users being sloppy with their work and sometimes it happened they have permanently deleted the files, without moving to Trash. (sigh!)

The following line of code may be saved in a file, as backupversion.sh to a folder of your choice (let’s say /root/backupversion.sh).

You could use this file in crontab, as follows:

*/5 * * * * /root/backupversion.sh

to run the backup every 5 minutes. Don’t forget to chmod +x /root/backupversion.sh!

rsync -avz --backup --delete --backup-dir=/backup/OUTATIME/`date +%Y.%m.%d-%H.%M/` /home/ /destbackup/home/ >/dev/null 2>&1 && find /backup/OUTATIME/ -mmin +65 -type d -exec rm -rf {} \; >/dev/null 2>&1

Because WordPress converts < and > to HTML codes, press the <> icon in order to see real code or copy/paste this code:

rsync -avz --backup --delete --backup-dir=/backup/OUTATIME/`date +%Y.%m.%d-%H.%M/` /home/ /destbackup/home/ >/dev/null 2>&1 && find /backup/OUTATIME/ -mmin +65 -type d -exec rm -rf {} \; >/dev/null 2>&1

As you can see, the command synchronizes the files and folders (with recursion) from /home/ to /destbackup/home/ folder and creates subfolders with timestamp in /backup/OUTATIME/ on the local machine. The subfolders’ name looks like 2015.08.14-21.10 (YYYYMMDD-HH.MM).

On the same line, after &&, the command finds files and folders older than 65 minutes (+65) and deletes them permanently.

You can adjust the command to fit your needs (you will probably change /home//backup/OUTATIME/ and /destbackup/home/ to your desired folders) and also you can change +65 if you would like to keep more or less versions.

3 thoughts on “Backup & versioning with RSYNC

  1. Navigand pe netul din Romania iata ca am gasit pe saitul dumeavoastra.

    Trebuie sa apreciez ca sunt placut surprins de calitatea informatiilor
    de pe acest sait si va doresc cat mai mult succes!

  2. Thank you, this is what I was looking for. As far as I understand at this stage, it does the same as EVAcopy for Linux.

    1. I don’t know about EVAcopy, this is a homebrew script I made because I needed something like this in a newspaper office, for backup purposes of editorials.
      I am glad you liked it!

Leave a Reply

Your email address will not be published. Required fields are marked *