Backing up your computer is important. Don’t be the sucker who loses important files and has to deal with it afterwards! In this how-to, you will learn about using rsync and lftp, writing your own scripts and adding them to crontab and of course, backing up to your external storage, NAS and also a remote server running Linux.
Backing up files on a regular basis isn’t difficult, but it can be if you manually type the command each time. In the world of crontab, there is absolutely no sense of wasting such time. Welcome to your rsync script! Anywhere on your machine, create a .sh file. For the sake of simplicity, we are calling ours backup.sh. You can create it with vi, nano or whatever your preferred text client is.
rsync -av /home /mnt/ntfs/Backups
Once you save that file, you could execute it with sh backup.sh. Simple stuff. However, for more flexibility, you can get a little more advanced with your script.
echo Beginning Backup… ;
mount -t vfat /dev/sdc1 /mnt/thumb ;
rsync -a –delete /home /mnt/thumb ;
umount /mnt/thumb ;
echo Backup Complete!
That script will acknowledge that the script has begun, mount the drive, rsync, unmount and then tell you it’s finished. But what about if you want to set up a script to run on a regular basis, or at a specified time when you are not around? Here’s another.
rsync -av /home /mnt/ntfs >> $backup_log
This one establishes a log location and then proceeds to perform the rsync with all output thrown into the backup.log file, for you to take a look at later if need be. What about nightly backups of your source code? How about creating date-coded tar files that automatically saves to your storage device?
echo “Backup Start: `date +%m-%d-%Y-%T`” >> $backup_log;
tar zvcf /mnt/nas/$backup.tar.gz /home/techgage/projects/application_name >> $backup_log;
echo “Backup End: `date +%m-%d-%Y-%T`” >> $backup_log;
Using this example, a backup file format is established and also our static log file. First, the time that the tar process begins is pasted in, followed by the actual process itself. Using this example, a file output would be: nightly_08-20-2007.tar.gz. You can hit up man date and configure the date to your liking though. Note that those are acute accents, and not regular accents.
Want to symlink the fresh .tar.gz file to latest.tar.gz for easy handling? This simply means that you can create scripts to upload the file to a remote server or external device and be able to point it directly to latest.tar.gz, but it would upload the file you want it to. You could add this line to the very end of the above script:
ls -fns `date +%m-%d-%Y-%T`.tar.gz latest.tar.gz
The possibilities of scripting are endless, and if you have a specific need, then there is surely a way to get what you need accomplished done with such a script. Once your script is prepared, how about automation?
So you want to run the script at a certain time each day/week/month? It’s time to hit up crontab. Chances are you have a cron daemon already installed, so to test, type in crontab and see if a small help displays. Once ready to edit, run nano -w /etc/crontab, but replace nano with your preferred text editor.
In my case, I run the backup each morning at 5:00AM, since I never turn the PC off and I happen to be on the PC sometimes right before that period hits, but never after.
0 5 * * * root sh /home/nas_backup.sh
To give a quick Cron primer, the beginning of each task requires five sets of numbers: Minute/Hour/Day of Month/Month/Day of Week. So if you wanted to run your script at 3:00AM on each Wednesday, it would be:
0 3 * * 3 root sh /home/nas_backup.sh
Or for 10:30AM on Monday and Fridays (Sundays = 0, Saturday = 6):
30 10 * * 1,5 root sh /home/nas_backup.sh
Cron will most likely be the most difficult part of your task. To read more, I highly recommend checking out this Wikipedia page.
After reading through the article, you should have a good idea of what to do now: how to rsync and how to create a script. But what exactly do you want to back up? The /home folder is always a good idea. That way if something bad happens, you can easily recover your files. But it can go deeper than that, as well.
All around your hard drive, there are files you could potentially save, to make a potential recovery even easier. Gentoo is my distro of choice, and it saves various configuration files that would help with a re-installation or recovery. Such files include the Portage package and world files, which list all of the installed software on the PC, as well as the make.conf which includes USE flags and gcc configuration. Your distro will have similar distro-specific configuration files, so be sure to figure out where they are, and back them up.
We just touched the basics here. You could write a robust script to do a variety of things. Mine for example, performs the same actions each night in a sequential order. It first cleans up the computer, emptying the trash, various caches and then backs up my /home, configuration files, and my kernel source code folder. Then it grabs a few nightly backups off one server with wget, then rsyncs our website server to make sure we have a current update. All this is done while saving the output into a static log file. Yes.. there is a lot you can do with a simple script, just as long as you are not worried about spending some time setting things up properly.
So what are you waiting for? Accidents can strike at any time…
If you have a comment you wish to make on this review, feel free to head on into our forums! There is no need to register in order to reply to such threads.