Publishing System Settings Logout Login Register
BASH Scripting: Backup Script
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on August 30th, 2007
16669 views
Unix
Backup Script


Introduction:

While this tutorial is in the Linux section it applies to all *nix systems with a BASH Terminal.

I'm not going to go into detail about what BASH is as to explain it requires a lot of background knowledge, instead I shall point you to this piece of literature - it'll provide you with a good footing and foundation.

A BASH script is really just a list of commands for the shell to run.  This is in order to simplify long-winded operations that may be done often.



Right onto the script:

What does it do?

This script backs-up a directory you specify and stores it in another directory (which you specify) as a .tgz file.

I shall go through the script section by section and explain each 'bit' of code as we go along:

The Code:

Let's start the script:

#!/bin/bash


That's the first line of the bash script, all shell scripts have to start with #!

The "/bin/bash" is just defining which shell to use with the script.



Now we'll define the directory where want to store the backup and format the name of the .tgz file we want to create:

OUTPUT=/some/directory/to/store/the/backup_$(date +%Y%m%d).tgz


The date is added to the filename: that way you can have different backups from different dates stored in the folder - the date format is: YYYYMMDD

"OUTPUT" is just the variable name we're giving to the backup-storage directory.

Now we'll define the directory we want to backup:

BUDIR="/some/directory/to/backup/"


"BUDIR" is the variable name given to the directory we want to backup.

Now we'll display a message to the user that we're going to initiate the backup:

echo "Starting backup of directory $BUDIR to file $OUTPUT"


Notice the use of "$BUDIR" and "$OUTPUT" - these are the variables being displayed in the message we print in the terminal.



Now we need to actually start the backup:

tar -cZf $OUTPUT $BUDIR


Again, notice the use of the variables - as the format of the "tar" command is: "tar [flags] [destination] [source]" all the variables are doing is filling in the places of the destination and source files / directories.

Now after this we could simply display a message saying the script is done - but what if it hasn't backed up for some reason?  What if there was an error?  We'll deal with this on the next page by displaying a message if an error occurred during the process.



To do this we'll make use of a special variable in the shell - this is the "$?" variable which displays the status of the last program run.  POSIX-compliant programs all return "0" if they run successfully - so we can simply check if the variable "$?" is equal to 0.  If it is we can display a confirmation message, if it isn't we can display an error message.

How will we do this?  With an "if" statement - that's right, theyre that good that even the shell can make use of them (the shell can make use of a lot of things):

Right, the semantics is similar to most coding languages, but the syntax is different to most you'll probably know:

if [ $? == 0 ]; then


That starts our "if" loop - now we need something to do if it evaluates to true:

echo "The file:"
echo $OUTPUT
echo "was created as a backup for:"
echo $BUDIR


What this does is:

  • Displays the text "The file:"
  • Displays the output directory and file (on a new line)
  • Displays the text "was created as a backup for:" (on a new line)
  • Displays the directory we backed up (on a new line)

So that's what happens if the backup completed successfully.



So what if it didn't?  We use an "else" statement:

else


Now something to do if the backup didn't complete successfully:

echo "There was a problem creating:"
echo $OUTPUT
echo "as a backup for:"
echo $BUDIR


What this does is:

  • Displays the text "There was a problem creating:"
  • Displays the output directory and file (on a new line)
  • Displays the text "as a backup for:" (on a new line)
  • Displays the directory we backed up (on a new line)

Now we need to close the "if" / "else" statement:

fi


And that's the completed script.

On the next page is the full script with comments added.



#!/bin/bash
#######################################################
##              Simple backup script..     
##  Created by Matthew Brunt: ([email protected])
## Licensed under GNU GPL v3 or later, at your option.
##       http://www.gnu.org/licenses/gpl.html
#######################################################

#Defines our output file
OUTPUT=/some/directory/to/store/the/backup_$(date +%Y%m%d).tgz

#Defines our directory to backup
BUDIR="/some/directory/to/backup/"

#Display message about starting the backup
echo "Starting backup of directory $BUDIR to file $OUTPUT"

#Start the backup
tar -cZf $OUTPUT $BUDIR

#Checking the status of the last process:
if [ $? == 0 ]; then
    #Display confirmation message
    echo "The file:"
    echo $OUTPUT
    echo "was created as a backup for:"
    echo $BUDIR
else
    #Display error message message
    echo "There was a problem creating:"
    echo $OUTPUT
    echo "as a backup for:"
    echo $BUDIR
fi


And there we have it, your script - run it as a cron job each day and you'll have nice tidy backups :)

-Matt
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Brunty

I enjoy web design and development, learning and understanding new concepts, I enjoy the open source movement. Enjoy a bit of photography here and there too.
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top