My First Linux Shell Script

Last week I wrote about my Ubuntu installation. At the end of that post I promised some Linux-related posts in the future, and here is my first one.

In my opinion one of the things about Linux that makes it so powerful is the shell. And as powerful as the shell can be, it becomes even more powerful when you create a shell script to execute a multitude of commands. Nobody wants to type ten commands in a row in order to achieve a desired result. Further, nobody wants to type the same ten commands in a row every single day. Enter the shell script.

My first shell script started out as a bit of experimenting but has evolved into quite a useful utility. The end result is a script that automatically backs up the development area of partybody.com at a specified time each night, stores a copy locally on the server, and emails a copy to the relevant administrators.

Let’s take a look at the code (some values have been changed for security reasons):

1 #!/bin/bash
2
3 # “scriptname” – copies all contents of a directory into time stamped tar.gz file in /path/to/backup/file
4
5 TD=$(date +%T-%d_%m_%Y)
6 FILE=”/home/user/web/cms/backup-$TD.tar.gz”
7 DIR=”/home/user/web/dev/”
8
9 # Create the gzipped archive file
10 tar -zcvf $FILE $DIR
11
12 # Send successful backup notification email
13 DEST=”/web/cms”
14 FILENAME=”backup-$TD.tar.gz”
15
16 echo “Automated backup ran successfully at $TD and created file: $FILENAME in FTP location: $DEST.” | mutt -s “Daily Automated Backup Complete” -a $FILE -c cc_email@yoursite.com to_email@yoursite.com
17

To get an idea of what’s going on here let’s examine this code line-by-line.

Line 1 is simply telling the script to use the BASH shell environment. Certain shell environments allow for different scripting capabilities than other shell environments, so it’s necessary to declare which one you’re using on the first line of your script. Note: BASH is the default for most Linux distributions.

Line 3 is a comment. The # character at the beginning of the line tells the shell to ignore all content on that particular line. In this case we’re just using the comment to identify the name of the script and a description of what it is intending to perform.

Line 5 creates our first variable, TD, and gives it the value of $(date +%T-%d_%m_%Y). In this instance we are accessing the built-in date function of the BASH shell and pulling out some values like time, day, month and year in order to create a time stamp.

Line 6 creates a variable called FILE and gives is the value of the backup file to be created. In other words, we’re specifying the filename we want to create and the location where we want to put it.

Line 7 creates a variable called DIR and gives it the value of the directory that we want to backup.

Line 9 is a comment declaring the action that we want to take next, in this case, creating the actual gzipped file.

Line 10 creates the archive file and gzips it taking the $DIR variable as input and the creating the $FILE variable as output.

Line 12 is another comment declaring the final action the script will take, sending the email to relevant administrators.

Lines 13 and 14 are variables that contain values to include as descriptors in the notification email, and have no effect on the execution of the script.

Line 16 echo’s the notification message into the email as the body content. The program Mutt is used to send the email. It accepts the piped input from echo and we specify a subject using the -s flag, an attachment using the -a flag, and a cc address using the -c flag. All that together sends an email to the “to” and “cc” email addresses with the subject of “Daily Automated Backup Complete” and attachment of the $FILE (”/home/user/web/cms/backup-$TD.tar.gz”) the script created.

And that’s it. Only 16 lines to backup any directory (including sub-directories), store a copy on your server, and email to whomever you wish.

There is still one problem with this script, though. As it stands it must be manually executed in order to run, and who wants to have to ssh into their server every time they want to run a backup. Therefore, we must auto-schedule the script using cron.

Cron is a time-based scheduling service driven by a crontab, a configuration file that specifies shell commands to run periodically on a schedule. Basically, we just need to add a line to this crontab file telling the shell to execute our backup script at a time of our choosing.

In this case, I added the following line to my crontab file to execute the script every day at 3 a.m.:

0 3 * * * /path/to/script/scriptname

Also, one final note, be sure to make your script executable or else it will not run. We can do this by using chmod +x as follows:

chmod +x /path/to/script/scriptname

That’s all for now. You should be able to adapt this code into a backup script of your own. Feel free to ask any questions in the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>