<p style=”margin-bottom: 0in;”>Being relatively new to the world of Linux, I am often overwhelmed by the functionality that is built into the Linux operating systems that was previously unavailable to me under the Windows platform. This was the case when I discovered two powerful built-in pieces of my Ubuntu 7.10 Desktop (now upgraded to 8.04) software: cron and TAR. “Cron” is a daemon that runs in UNIX and Linux systems that automates tasks. A fresh install of Ubuntu has many cron scripts already installed. Cron will run hourly, daily and monthly, and you can see these scripts by looking in the /etc/cron.hourly, /etc/cron.daily, and /etc/cron.monthly directories. Each file under those directories is an executable script that gets called by the cron daemon either hourly, daily or monthly. So imagine any task that you can write a script for in Linux can now be executed automatically. Isn’t that the main goal of computers, to automate mundane human tasks?</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>TAR is the next internal program that I have come to love. TAR is a tape backup archive file program. It basically allows you to pull in tons of data, and make a copy of that data into one file for backup purposes. In Windows, in order to create a stable backup, I’d have to use something proprietary. Which means purchasing software. The built-in Windows backup software leads a lot to be desired, but generally, all Windows backup software will to the same thing. It will compress the files into a proprietary file format. Now, if I did have a disaster, I’d have to find or build an emergency recovery disk just to restore the files back into the computer. As I’ve referred in previous articles, the main reason to switching my home PCs to Linux was ease of recovery and re-installation over Windows. In the Linux world, I can use TAR to extract all my backed up files, and the file permissions and ownership (something critical in the Linux world) are preserved. So my goal was to use cron daily scripts and TAR to automate a daily backup routine that would ensure my data is preserved. Once I had that data into one file, the last step would be to figure out how to automate an ftp upload of my data to a separate PC for offline storage.</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>It’s pretty easy to go online, do a search for “TAR” and “backup” and get a beginning script that will give you a basis to begin a backup strategy. The script I started with is found here (http://www.faqs.org/docs/securing/chap29sec306.html). This is a good basic script that allows you to enter personal information about your system, and create incremental backups daily, with full backups being built on Sundays and the first of each month. Unfortunately, I immediately found that this script would not work for my Ubuntu installation. After much tracking, tracing, and online searching, I tracked the problem to the second step in this process. There is a reference file that stores the date the last full backup was completed. It is listed as:</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>[root@deep] /# date +%d%b < /backups/last-full/myserver-full-date</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>Some close analysis shows that the “<” character is pointing the wrong way. This step creates the backup reference file, so it is a critical part of getting a script to work successfully. I changed to code to:</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>[root@deep] /# date +%d%b > /backups/last-full/myserver-full-date</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>This small error makes a huge difference. Let me reiterate that the above script will not run until this first step is completed. From here forward, I will focus on how you can get a daily backup job running. I won’t point out the errors of this online reference, but I will refer back to the original script, as it is the source of my successful backup jobs. The online script is a general one that is meant as a reference for Linux, not Ubuntu specifically.</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>The first step is to copy the script and save it to a local text file. Edit the file and change the five variables to local ones. “COMPUTER” should be the name of your computer, or the base name you want the backup file to be called. “DIRECTORIES” should be the location of the directories you want to back up. Make sure they are fully qualified, so if you want to back up your user directory, use “/home/username”. “BACKUPDIR” is the fully qualified location of your backup directory. Make sure the backup directory exists, or create it before running the script. Create the date reference file by running the code above. If the file is not created, manually create the directory and file (“last-full” and computername”-full-date”). Put today’s date in the file in the form of “27-Jan”. </p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>There’s just three simple steps left to getting this cron script working. First, copy the cron script to the /etc/cron.daily directory. Next, rename it so that there are only alphanumeric characters in it. Ubuntu’s cron daemon will not run any scripts that have a period, hash, or any other non-alphanumeric character in the name. This took my several weeks to figure out. Finally, make the cron script executable. This can be done by the “sudo chmod 755 /etc/cron.daily/filename” command.</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>Now, wait until the next day. A default installation of Ubuntu 7.X Desktop will fire off the daily cron jobs at 7:30 AM. After that time, check the /backups directory (or wherever you set your backups to be made) and you should see your backup TAR files. Although this all works, for a real backup solution, that file needs to be moved off of the same physical computer to ensure a safe copy of your files in case of a hardware malfunction on the PC.</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>There are several ways to accomplish this task. The simplest is to have a separate hard drive for your backup files. This can either be an internal hard drive, or a portable USB-connected one. Just make sure the drive is installed and automatically mounts when the system boots, so that you can be ensured the files will be copied over when the archive file is made. If prefered, you can edit the backup cron file you just created to use the new hard drive and create your TAR file there originally. I like to keep a local backup (so I can quickly restore files after I screw something up), so I added the following lines to my cron backup scripts:</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>FILENAME=filename.tar</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>This is added after the variables section in the cron script. This allows for the filename to be captured each run, and then I can copy the file to an offline location.</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>The next changes are inserted in the monthly, weekly, and daily sections of the cron backup script.</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>Monthly:</p><p style=”margin-bottom: 0in;”></p>
FILENAME=$BACKUPDIR/$COMPUTER-$DM.tar #Build monthly filename/bin/cp $FILENAME /media/500gb/backup >/dev/null #copy new TAR file<p style=”margin-bottom: 0in;”></p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>The second line needs to be inserted after the TAR command.</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>Weekly:</p><p style=”margin-bottom: 0in;”></p>
FILENAME=$BACKUPDIR/$COMPUTER-$DOW.tar #Build weekly filename/bin/cp $FILENAME /media/500gb/backup >/dev/null #copy new TAR file <p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>Daily:</p><p style=”margin-bottom: 0in;”></p>
FILENAME=$BACKUPDIR/$COMPUTER-$DOW.tar #Build daily filename/bin/cp $FILENAME /media/500gb/backup >/dev/null<p style=”margin-bottom: 0in;”></p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>Notice that the copy command is the same in each, as is the general way you insert these commands into the cron script. First, the “FILENAME=” line is inserted before the $TAR command line. Then the /bin/cp line is inserted after the $TAR command line.</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>I then use an ftp macro in my .netrc file to automatically upload the TAR files to my ftp site. I utilize the crontab feature of Ubuntu to actually run the ftp script, since ftp uploads from the daily.cron scripts do not work all that consistantly. Crontab is basically a sub-set of cron that allows the user or administrator to enter single jobs to be run, and control when and how often they are run. You can see the crontabs on your system by using the crontab -l command. Crontab is documented pretty well, so I won’t elaborate here, unless someone needs more information.</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>At this point, if you’ve followed this tutorial, you have a system that takes a full backup on the first day of the month, and on each Sunday. It takes incremental backups every other day of all the files that have changed since the last weekly backup. The best part is, it’s all automated for you. I recently rebuilt one of my Ubuntu machines, and thanks to the TAR backups it took me only hours to completely restore the computer to it’s fully functioning state. That was the goal of my original mission to use Linux at home, and I’ve come to validate that solution, and the best part it, it didn’t cost me a single dollar.</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>Full script copies are below, including the ftp upload crontab script and the ftp macro needed to automate the login and upload of files.</p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>Daily cron script:</p><p style=”margin-bottom: 0in;”></p>
#!/bin/sh# full and incremental backup script# created 07 February 2000# Based on a script by Daniel O’Callaghan# and modified by Iain McMullin<p style=”margin-bottom: 0in;”><iainmcmullin@yahoo.com></iainmcmullin@yahoo.com></p><p style=”margin-bottom: 0in;”></p>
#Change the 5 variables below to fit your computer name and#directories to be backed up<p style=”margin-bottom: 0in;”></p><p style=”margin-bottom: 0in;”></p>
COMPUTER=mcmfileshare # name of this computerDIRECTORIES=”/home/samba” # directoris to backupBACKUPDIR=/backup # where to store the backupsTIMEDIR=/backup/last-full # where to store time of full backupTAR=/bin/tar # name and locaction of tar<p style=”margin-bottom: 0in;”></p><p style=”margin-bottom: 0in;”></p>
# placeholder for current filenameFILENAME=filename.tar<p style=”margin-bottom: 0in;”></p><p style=”margin-bottom: 0in;”></p>
#You should not have to change anything below herePATH=/usr/local/bin:/usr/bin:/binDOW=`date +%a` # Day of the week e.g. MonDOM=`date +%d` # Date of the Month e.g. 27DM=`date +%d%b` # Date and Month e.g. 27Sep<p style=”margin-bottom: 0in;”></p>
# On the 1st of the month a permanet full backup is made# Every Sunday a full backup is made – overwriting last Sunday’s backup# The rest of the time an incremental backup is made. Each incremental# backup overwrites last week’s incremental backup of the same name.## if NEWER = “”, then tar backs up all files in the directories# otherwise it backs up files newer than the NEWER date. NEWER# gets it date from the file written every Sunday.<p style=”margin-bottom: 0in;”></p><p style=”margin-bottom: 0in;”></p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”># Monthly full backup</p><p style=”margin-bottom: 0in;”></p>if [ $DOM = "01" ]; then NEWER=”"FILENAME=$BACKUPDIR/$COMPUTER-$DM.tar $TAR $NEWER -cf $BACKUPDIR/$COMPUTER-$DM.tar $DIRECTORIES/bin/cp $FILENAME /media/500gb/backup >/dev/nullfi<p style=”margin-bottom: 0in;”></p><p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”># Weekly full backup</p><p style=”margin-bottom: 0in;”></p>if [ $DOW = "Sun" ]; then NEWER=”" NOW=`date +%d-%b`# Update full backup date echo$NOW > $TIMEDIR/$COMPUTER-full-dateFILENAME=$BACKUPDIR/$COMPUTER-$DOW.tar $TAR $NEWER -cf $BACKUPDIR/$COMPUTER-$DOW.tar $DIRECTORIES/bin/cp $FILENAME /media/500gb/backup >/dev/null
# Make incremental backup – overwrite last week’selse# Get date of last full backup NEWER=”–newer `cat $TIMEDIR/$COMPUTER-full-date`”FILENAME=$BACKUPDIR/$COMPUTER-$DOW.tar $TAR $NEWER -cf $BACKUPDIR/$COMPUTER-$DOW.tar $DIRECTORIES/bin/cp $FILENAME /media/500gb/backup >/dev/nullfi<p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>.netrc ftp upload script:</p><p style=”margin-bottom: 0in;”></p>
machine 192.168.1.50login usernamepassword password <p style=”margin-bottom: 0in;”></p>
macdef backupfilesprompt offlcd /backupscd 200gb/linuxbackupmput *.tar <p style=”margin-bottom: 0in;”>
</p><p style=”margin-bottom: 0in;”>Crontab entry to kick off the nightly ftp upload:</p><p style=”margin-bottom: 0in;”>0 2 * * * echo “$ backupfiles” | ftp 192.168.1.50</p><p style=”margin-bottom: 0in;”>This entry calls the macro “backupfiles” each morning at 2 AM.</p>