Posted on August 30th, 2007
17004 views 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:" What this does is:
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:" What this does is:
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 And there we have it, your script - run it as a cron job each day and you'll have nice tidy backups :) -Matt |