Skip to main content
28-02-2021

rsyncSince many years ago, I use "rsync" to make snapshots of my system and keep backups which are separated from the main system. This, coupled with the synchronization of my different devices (desktop, laptop and tablet) with "unison", which I will explain in another future article, allows me to have all my information updated and be covered of the danger of data lost in case of faulty hardware. In this brief article I will explain my configuration for these snapshot backups. For a more detailed explanation of this system, and possibly a better configuration than mine, please consult this article:

http://www.mikerubel.org/computers/rsync_snapshots/

It is better to follow the above article in order to have all the details of the system and a more robust guide than this article. Here I simply explain how it works for me. It works so well for me and since so many years that I often forget that I have this working in the background.

What does this system get me and what is needed? The system allows me to have copies (snapshots) of the volumes with directories and files that I want with the frequency that I want. For instance, in my case I keep copies of all my data (the whole hard disk) of 1, 2, 3 and 4 hours ago, of 1, 2, 3 and 4 days ago, of 1, 2, 3 and 4 weeks ago, and 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 , 11 and 12 days ago. In other words, I can see exactly what I had in my disk in any of these past moments, or instance 7 months ago. This also is helpful to undo any non-wanted elimination of files or directories. If they existed one hour ago, I have to simply go to the snapshot of one hour ago and recover the erased data.

The directory where I keep the snapshots looks like this:

What you can see are directories of the different snapshots with all the information (the whole hard disk) for these different moments specified above. You will think that a lot of space is needed to keep 23 different versions of the same hard disk! The answer is not, because actually there is a single copy and the rest contains only the differences with respect to this initial copy, the so called incremental copies. This can be accomplished with the "cp -a" command, which is a standard command from the Linux shell, plus the "rsync" program which is an old Linux program used for unidirectional synchronizations. In the above mentioned article you can find the technical details on how these tools work. What the proposed system really does is to copy only once all the desired information (for instance the whole hard disk) with "rsync", and update the rest of incremental copies with "cp -a". The space needed for the backups is double the space of the information we want to protect. For instance if we have a 1TB hard disk and we want to maintain a copy of the whole disk, we need a space of 2TB for the copies, but we will not fill it because we will never fill the original 1TB disk, but we may be close to fill it so we need double its space.

How does it work? The first copy that it does when we start the system is a full copy of the system in "hourly.0" with "rsync". At the next hour it copies with "cp -a" the "hourly.0" to "hourly.1" and creates a new copy in "hourly.0" with rsync. So "hourly.1" will have the system as it was one hour ago. After one hour "hourly.1" is moved to "hourly.2", a new "hourly:1" is created with "cp -a" of "hourly.0" and a new "hourly.0" is created with rsync. After three hours "hourly.2" is moved to "hourly.3", "hourly.1" to "hourly.2", and the rest is the same as in the previous step, a new "hourly.1" and "hourly.0" is created. Afterwards, "hourly.3" is removed, and the whole process is repeated every hour. Each midnight "hourly.3" is copied to "dayly.0" and "dayly.1", "dayly..2" and "dayly.3" are rotated as the hourly snapshots. Same thing is done once a week with "weekly.0" to "weekly.3", and every month with "montly.0" to "montly.12". All these processes can be seen in the scripts linked at the end of the article.

The only thing left is to program cron so that these scripts are executed at the expected moment. I actually keep two disks of 2 TB each for more security, but otherwise the scripts are as explained. Here is my crontab:

MAILTO="correu electrònic"

0 */4 * * * /home/wgarcia/jobs/make_snapshot_1.sh

0 2,6,10,14,18,22 * * * /home/wgarcia/jobs/make_snapshot_2.sh

0 1 * * * /home/wgarcia/jobs/daily_snapshot_rotate_1.sh

0 3 * * * /home/wgarcia/jobs/daily_snapshot_rotate_2.sh

0 2 * * 0 /home/wgarcia/jobs/weekly_snapshot_rotate_1.sh

0 4 * * 0 /home/wgarcia/jobs/weekly_snapshot_rotate_2.sh

0 3 28 * * /home/wgarcia/jobs/monthly_snapshot_rotate_1.sh

0 5 28 * * /home/wgarcia/jobs/monthly_snapshot_rotate_2.sh

I don't explain here the meaning of the crontab entries, but basically they call the scripts with the desired frequency. The scripts are quite simple, there is no configuration needed except the volumes that you want to backup and the place were you keep the backups, which for me is /mnt/Backup1/Work for the first disk and /mnt/Backup2/Work for the second disk. Ideally they should be mounted in a different place as the main system, just in case that the place where you have your system gets catastrophic damage like a fire or similar. But if you keep the backups in another disk, internal or external, should be enough to protect your data.

Here you have the scripts. There is also an exclude in case you want to exclude some files/directories from the backup, for instance temporary files that you don't need. You have to only change the place were you want to have your backup, that is change /mnt/Backup1/Work for the path to your disk, and change the directories and volumes that you want to copy with "make_snapshot_1.sh". If you want to have a second disk as myself just create version 2 of these scripts only changing the backup place so that it points to the second disk.

Files:

make_snapshot_1.sh

daily_snapshot_rotate_1.sh

weekly_snapshot_rotate_1.sh

monthly_snapshot_rotate_1.sh

backup_exclude

 

 

 

Tags:
Powered by Drupal

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer