A lifesaving backup mechanism

I have seen many complaints about problems with the Sep 1/2 KDE upgrade. Well, fact is that sometimes things go wrong with upgrades, and this was a big one. Now, I myself had some trouble with this upgrade, but not to nearly the same extent as some people are reporting; I solved my problems with a bit of work (and a lot of grumbling). However, even if I had needed to roll back, I could have rolled back to the previous system with no difficulty and only a few minutes work.
Basically, once a day at around 4AM my system does a backup of the important stuff to other hard drives on my computer. I back up / and I back up /home separately to different drives. In the current situation, had I not been able to fix my KDE installation I would have rolled back using the backup of /.
This backup mechanism is not original with me; I found it on the net probably a decade ago. But I have used it ever since, and I have set up many other systems with this scheme. It works very well.
Essentially, I have two scripts in /root that are run as cron jobs. One script, called dosysbackup, does the nightly backup. The other script, called doweeklysysbackup runs once a week on saturday night.
Here is dosysbackup:
Here is what happens. This backup resides on /mnt/sda6/backup on my machine. Within backup, there are the subdirectories backup0 through backup6 and the subdirectory weeklybackups.
When dosysbackup runs, the first thing it does is delete the directory backup6. Poof...gone. Then it renames backup5 as backup6, backup4 as backup5, and so forth. Finally, it copies using hard links the contents of backup0 to backup1. Since this copy is done using hard links, the actual files are not copied, only a new directory structure is created which points to the same files.
Finally, the script runs rsync on / with the indicated exclusions and with the flag --delete set, comparing the contents of / with the contents of backup0 and updating backup0 with only the changes that have occurred since the last backup.
Now, once a week, a cron job runs the script doweeklysysbackup, before doweeklybackup runs. Here is that script
This script merely moves backup6 into the weeklybackups subdirectory with a directory name that is derived from today's date. Now, backup 6 is one week old when this happens, so to get the actual date of the backup, I need to subtract one week. I was too lazy to do that in the bash script, since I know what I have.
So I wind up with a daily backup going back one week, and a weekly backup going back as far as I choose to let it remain on the drive - usually 6 months. A couple times a year, I take the most recent weekly backup, tar/gzip it, and archive it on a backup drive. Then I delete the weekly backups.
I do the same thing with /home, but I do it to a different drive.
Now, had my KDE upgrade been hosed, forcing me to roll back, I could have rolled back by replacing all of /usr with last night's backup, like this:
(as root)
rsync -a --delete /mnt/sda6/backup/backup0/usr/ /usr
Simple, and saves a LOT of aggravation when something gets fouled up.
For laptops, you do this backup to a USB drive...which, of course, you have to plug into the laptop. Even if you don't do this daily, you can do it from time to time and you should always do it before accepting an upgrade. If you only have one drive, just set up a backup directory on the same drive. In this event, it isn't a true backup, just a duplicate copy you can use to get you out of trouble.
Basically, once a day at around 4AM my system does a backup of the important stuff to other hard drives on my computer. I back up / and I back up /home separately to different drives. In the current situation, had I not been able to fix my KDE installation I would have rolled back using the backup of /.
This backup mechanism is not original with me; I found it on the net probably a decade ago. But I have used it ever since, and I have set up many other systems with this scheme. It works very well.
Essentially, I have two scripts in /root that are run as cron jobs. One script, called dosysbackup, does the nightly backup. The other script, called doweeklysysbackup runs once a week on saturday night.
Here is dosysbackup:
- Code: Select all
rm -rf /mnt/sda6/back/backup6
mv /mnt/sda6/back/backup5 /mnt/sda6/back/backup6
mv /mnt/sda6/back/backup4 /mnt/sda6/back/backup5
mv /mnt/sda6/back/backup3 /mnt/sda6/back/backup4
mv /mnt/sda6/back/backup2 /mnt/sda6/back/backup3
mv /mnt/sda6/back/backup1 /mnt/sda6/back/backup2
cp -al /mnt/sda6/back/backup0 /mnt/sda6/back/backup1
rsync -a --delete --exclude /dev --exclude /tmp --exclude /home --exclude /media --exclude /proc --exclude /sys --exclude /mnt --exclude /var/tmp --exclude /var/cache / /mnt/sda6/back/backup0
Here is what happens. This backup resides on /mnt/sda6/backup on my machine. Within backup, there are the subdirectories backup0 through backup6 and the subdirectory weeklybackups.
When dosysbackup runs, the first thing it does is delete the directory backup6. Poof...gone. Then it renames backup5 as backup6, backup4 as backup5, and so forth. Finally, it copies using hard links the contents of backup0 to backup1. Since this copy is done using hard links, the actual files are not copied, only a new directory structure is created which points to the same files.
Finally, the script runs rsync on / with the indicated exclusions and with the flag --delete set, comparing the contents of / with the contents of backup0 and updating backup0 with only the changes that have occurred since the last backup.
Now, once a week, a cron job runs the script doweeklysysbackup, before doweeklybackup runs. Here is that script
- Code: Select all
#!/bin/bash
dirname=`date`;
mv /mnt/sda6/back/backup6 /mnt/sda6/back/weeklybackups/"$dirname"
This script merely moves backup6 into the weeklybackups subdirectory with a directory name that is derived from today's date. Now, backup 6 is one week old when this happens, so to get the actual date of the backup, I need to subtract one week. I was too lazy to do that in the bash script, since I know what I have.
So I wind up with a daily backup going back one week, and a weekly backup going back as far as I choose to let it remain on the drive - usually 6 months. A couple times a year, I take the most recent weekly backup, tar/gzip it, and archive it on a backup drive. Then I delete the weekly backups.
I do the same thing with /home, but I do it to a different drive.
Now, had my KDE upgrade been hosed, forcing me to roll back, I could have rolled back by replacing all of /usr with last night's backup, like this:
(as root)
rsync -a --delete /mnt/sda6/backup/backup0/usr/ /usr
Simple, and saves a LOT of aggravation when something gets fouled up.
For laptops, you do this backup to a USB drive...which, of course, you have to plug into the laptop. Even if you don't do this daily, you can do it from time to time and you should always do it before accepting an upgrade. If you only have one drive, just set up a backup directory on the same drive. In this event, it isn't a true backup, just a duplicate copy you can use to get you out of trouble.