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 to a folder of your choice (let’s say /root/

You could use this file in crontab, as follows:

[code lang=”bash”]*/5 * * * * /root/[/code]

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

[code lang=”bash”]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[/code]

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 Responses

  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!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.