Convention Listings
                                                                                                                        About Us | Donate! | Contribute! | Report Problems
   
Raiden's Realm is always in need of good, community generated articles and content for our site. So if you have a Linux, Open Source or Media Freedom related article, review, tutorial, or editorial you want to contribute, by all means please submit it to admin@raiden.net. Your contributions are always appreciated and will help us out immensely. Thanks.
   
Automated backups in Linux (Page 1 of 1)

Written by Steve Lake
Posted on: Nov 09, 2007 at 03:53pm
Section: Tutorials
Printer Friendly Version
Legacy URL

One of the hardest things to get most users to do is to backup their computer. This is likely due to a false belief that hardware does not fail, or operating systems do not crash, or user error does not occur. Some might even believe that all data is recoverable, even in the worst possible situations. Sad to say, it's not. Besides, getting into the habit of backing up data is always a good thing, no matter what the system or the reason.

Most people who do backups use expensive backup software and/or hardware to do their backups while others use somewhat clumsy, selective backups through manual methods. Those are alright, but I've got something better that will be highly useful to you, will protect your data, and can be setup to run automatically for you so you never have to touch the system again. But what is this miracle system and why does it run so well on Linux? Well, let me show you.

Setting up your backups is very, very easy. It might seem like a lot of work, but it's really not and once you're done you should never have to touch it again, except to update a few configurations every so often to help the backup system keep current with changes on your system.  There are several things you'll need to have installed on your system to make this work.

* rar (this is a program)
* a second internal hard drive

The reason for the second internal hard drive is because it will be used as a backup drive. It can be of nearly any size you want it, however, I recommend that it be no smaller than your primary drive. Larger drives will give you plenty of extra space should you want to manually store important files that you need, but don't change often, if ever. Also, using a second drive to store valuable backup files and data is important because a second drive is 1/50th as likely to fail as your main drive is.

As with any secondary hard drive, you can mount it anywhere you want to in the file system.  However, I recommend mounting it off the root directory with a folder name appropriate to your use of the drive. In this case, we'll be using that drive for backups, thus naming the folder "backups" would be a good idea. If you don't already have a second hard drive in your machine, you'll need to add that first. If you've never added a new hard drive to your computer, then just follow this guide on how to add a hard drive to your system. While it's written for Freebsd, the steps are almost identical to what you'd do when adding a new drive to a regular Linux system.

Now, once you've got the second drive mounted and ready, you'll need to edit a couple of files to setup your backups. Since we'll be using a perl script as part of our backup system, and since most users won't know how to write in perl or create a perl script to automate their backups, well, with the exception of the linux power users, I've gone ahead and written it for you.

=============================

&backup;

sub backup {

   # Define arrays for the day of the week and month of the year.           #
   @days   = ('Sunday','Monday','Tuesday','Wednesday',
              'Thursday','Friday','Saturday');
   @months = ('January','February','March','April','May','June','July',
              'August','September','October','November','December');

   # Get the current time and format the hour, minutes and seconds.  Add    #
   # 1900 to the year to get the full 4 digit year.                         #
   ($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
   $year += 1900;

   # Format the date.                                                       #
   $date = "$months[$mon] $mday, $year";

   mkdir("/drive2/backup_folder/$date", 0755);
   $makeme = "/usr/local/bin/rar a \"/drive2/backup_folder/$date/backup.rar\" @/drive2/home-backup.lst";
   system($makeme);
}

================================

This script is designed to create a new folder, rename it to the current date the backup is being run on, and then save the file to this new folder.  To get started, copy and paste this script into your favorite text editor, change the red text items to whatever you'll be using on your computer, and then save to the root of your backup drive as "backup.cgi".  IE, /backups/backup.cgi, if backups is the name you used for your second drive.  You can also put this script into a sub folder titled "scripts" if you'd like to keep the backup script files you need somewhere other than the root of the second drive.

To avoid confusion, let me clarify each of these red items listed above. All instances of "drive2" should be changed to whatever folder your 2nd hard drive is mounted to. In our example above I used "/backups" as the folder name for where the second drive was mounted. If you used that too, then change "drive2" to "backups". Next, where it says "backup_folder", you should change that to something like "automated_backups" or something that will tell you what is in that folder. It's your choice how to name it.

Lastly, where it says "/usr/local/bin/rar", that should be changed to the path where you have rar installed. For example, if rar on your machine is installed in "/usr/bin", then you'd change that line to read "/usr/bin/rar".  If rar isn't installed, make sure to add it before using this script.

The next part will take you a few minutes to do, because this is the part where you decide what you want backed up and what you don't. Open your favorite text editor and enter the following two lines at the top.

/etc
/root

Both of those are highly important system directories, and will be required when and if you ever need to restore your system files at a later date. Depending on what you do on your computer, you may also want to backup /var if it also contains any configuration files you need to keep. But if you do, be specific about which directories you backup under /var because not everything in /var needs to be backed up.  One that you may consider backing up would be /var/logs.  

The next step is to go through your home directory and choose what you want to backup. If you want to do a blanket backup and grab everything for yourself and everyone else, just put "/home" in as the next line in your backup list. While this is good idea in theory, it's not always that great in practice. The reason being is that if you grab everything, you'll also be backing up a lot of unneeded files as well, which can lead to some very LARGE backup files which contain a lot of data that really doesn't need to be backed up.

Typically what you want to do when choosing what files and folder to backup is to choose only those folders and files that are most vital to you, or that change often. For example, here's a list of folders and files I backup regularly. Comments are in parenthesis.

/home/raiden/Mail (this is for your local mail. It may use a different name or location depending on your mail client or where it chooses to store its mail.)
/home/raiden/.gaim (Instant messenger config file)
/home/raiden/.gaimrc (ditto)
/home/raiden/.profile (only backup if you've made special changes to it.)
/home/raiden/.kde (this is the config directory for the KDE window manager. If you use a different window manager, this will be named differently)
/home/raiden/.bash_profile (config file for bash shell)
/home/raiden/.bashrc (ditto)
/home/raiden/.login (same as .profile)
/home/raiden/.xchat (program configuration file for xchat)
/home/raiden/.xinitrc (only save if you've made special changes to how Xorg loads your window manager)
/home/raiden/.xsession (ditto)
/home/raiden/.mozilla/default/6f8labsd.slt/bookmarks.html (path to my bookmarks file for Mozilla)
/home/raiden/.mozilla/default/6f8labsd.slt/*.s (password files for mozilla.)
/home/raiden/.mozilla/firefox/5pyv5haz.default/*.s (same as with mozilla, but for firefox)
/home/raiden/.mozilla/firefox/5pyv5haz.default/bookmarks.html (ditto)

That's just the basics. There may be other folders in your home directory, such as your music, a storage folder, pictures, etc, that you'll want to backup. As stated before, the general rule when choosing files and folders to backup is to choose those that will frequently change, or that are most important to you. Again, it's your backups and you can choose to backup as much or as little as you like. Also, remember that when you're putting in the lines for each item you want to backup, be sure to put in the full path. Otherwise those items won't get backed up because rar won't know where to find them.

Once you're done with that, save the file as "backup.lst" and put it in the same folder as you specified in the backup.cgi script above. Ok, so far, so good. Now you need to test and be sure what you've done works. To do this, open a console screen and type the following command: perl /drive2/backup.cgi

If all goes well, you should see rar start up and begin listing all the files and folders it's backing up. If it encounters any errors, it'll let you know. Make note of those errors and then go back and make any corrections in the "backup.lst" file if nessisary to fix those errors.

Lastly, you need to create a cron entry to automate this backup. You'll need to be root to do this. Type "su - root" and hit enter. It'll ask you for root's password. Type it in and hit enter. Now you're running as root. Be careful here because root is "god mode" on Linux and you can break things really easily if you're not careful.

Now, type "pico /etc/crontab" and hit enter. If you prefer VI, you can use that or any other console text editor you choose. Next, go to the bottom of the file and enter this line:

1       13      *       *       0       root    /usr/bin/perl /drive2/backup.cgi >> /drive2/log.txt

This cron entry is setup to do my backup every Sunday at 1:01pm. You however can choose to do it anytime you wish. I chose to have my backups done then because it was best for me.  If you're unsure of how to adjust the time in cron to meet your needs, see our guide to setting up cron for more information on working with cron.

Since cron is completely oblivious to where anything is on the system, you'll need to specify the full path to everything. "/usr/bin/perl" is the path to wherever perl is installed on your system. If it's in a different folder, change the entry to the correct path. If you're not sure where perl is, or any program for that matter, type "whereis program" where "program" is the name of the program or application you're looking for. In our case it's perl. Sometimes you'll get several entries produced by the whereis script. The one you're is the first entry.

"/drive2/backup.cgi" should be changed to reflect the location of your "backup.cgi" file. "/drive2/log.txt" should be put wherever you feel you want to store your log file for your backups. The purpose behind having the log file is so that you don't keep getting a message from cron every time the script runs, and so that you can come back at any time and research any past backups for errors that might have occurred.

If you would like to store your logs as separate files for each backup instead of as one master log file, remove " >> /drive2/log.txt" from the cron entry and add  ">> /drive2/log$date.txt"  to the "$makeme" line of your perl script.

Well, that's all that it takes to start doing automated backups on your computer in Linux or BSD.  Now don't forget to periodically check on your backups to make sure they're working, and also don't forget to periodically make manual backups of all important files and copy them and your automated backups to another storage device such as an external hard drive or network share in case something happens to your second hard drive.

I hope you enjoyed this tutorial and if you have any questions, please let me know. Thanks.
Discuss this!  ( 2 comments )

Raiden's Realm Social Bookmarking
If you have any problems with any of these links, please let us know.  Thanks.

Digg it! Slashdot It! Del.icio.us Add to StumbleUpon Add to Technorati Reddit! Add to Google Bookmarks Add to FaceBook Share Add to Twitter

Average vistor rating: 4.3 out of 5 (8 total votes)

Community Image Gallery

More Images
Submit new images to gallery

Upcoming Shows and Cons

 1.  LinuxCon 2010
 2.  OpenSource World 2010
 3.  Ohio Linux Fest 2010
 4.  Atlanta Linux Fest 2010
 5.  ESC Boston 2010
More

Announcements

This is just a reminder to everyone that we're always looking for articles for posting on our site.  So if you have a Linux, Open Source or Media related article, review, tutorial, or editorial you want to post, by all means please send it to admin@raiden.net and we'll be glad to post it.  Thanks.

Have you ever bought a PC or laptop preloaded with Linux?

Yes
No
Considering it
Other ( Please specify )

More Polls
Latest Releases
(courtesy of Distrowatch)

1. Clonezilla Live 1.2.5-38...
Released: 07/29
2. RIPLinuX 10.6
Released: 07/29
3. Mint 9-rc (Fluxbox)
Released: 07/29
4. Nexenta 3.0-rc3
Released: 07/29
5. Salix 13.0.1
Released: 07/29
6. AUSTRUMI 2.1.6
Released: 07/28

More
All original content on this site is copyright of Raiden's Realm via the Creative Commons license. All rights reserved.

Any non original content is the sole property of the respective owners.