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:
[code lang=”bash”]*/5 * * * * /root/backupversion.sh[/code]
to run the backup every 5 minutes. Don’t forget to chmod +x /root/backupversion.sh!
[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.
Leave a Reply