How to Write a Backup Shell Script using Tar and Gnu Zip

Happy New Year!

As promised I made a rudimentary Linux shell script utilizing the tar and gzip commands to archive or make backups. If you’re new to shell scripting you might like to read this articles.

http://www.dartmouth.edu/~rc/classes/ksh/print_pages.shtml
http://tldp.org/LDP/abs/html/

If you’re lazy like I am. Don’t worry I’ll explain parts of the code.

Our goal is to make a shell script to create backups (in tar-gzip versions) of the folders we want unto a safe location on a disk. We want to be able to list all the folders and have the script loop through them. Now that’s settled, we can proceed to the code.

First, don’t forget to tell linux what scripting interpreter to use. I usually use sh but there are others like bash and ksh. But many experts suggest to use sh for portability to older systems.


#!/bin/sh

Next, we configure the script. The lines below enumerates the folders I want archived ,then stores them in a SHELL VARIABLE named FOLDERS. (duhh!..)

# — LIST OF FILES/FOLDERS TO BACKUP
FOLDERS=”/var/www/html /opt/sandbox”

This line saves the folder path I want the archives to be stored in.

# — BACKUP HERE
BACKUPFOLDER=”/opt/backup”

*REMINDER*
Make sure that the backup folder exist. If it doesn’t run the lines below on the console.

> mkdir /opt/backup
> chown root:root /opt/backup
> chmod 755 /opt/backup

Here I configure the command string I want to use. Regarding the details of the command I used – see this [link].

# — ARCHIVE COMMAND
COMPRESSCMD=”tar czfv “

Here we use the for command to loop through all the items we listed in $FOLDERS and store it in another variable, $itm.

## loop thru folders
for itm in $FOLDERS; do
# commands here
….
done

These lines configures the commands we’re going to use inside the loop. The first line generates a formatted file path for my archive, $FARCHIVE.

FARCHIVE=$BACKUPFOLDER/`basename $itm`_`uname -n`_`date +%F`.tgz
$COMPRESSCMD $FARCHIVE $itm

If you’ve noticed I used the tilde symbol(`). Yes they are not single quotation marks. In linux console, any commands enclosed within tildes are ran first and the result is returned as a string. So for example $itm is equal to /var/www/html. The resulting FARCHIVE value will be:

> echo $FARCHIVE
/opt/backup/html_potato_2009-01-02.tgz

The second line uses $FARCHIVE as well as the command we configured earlier and runs it.

For a breakdown of the enclosed commands we used.

echo the last(base) name in a path string.

> basename /var/www/html
html

echo the system’s computer name.

> uname -n
potato

echo the current date in this format (YYYY-MM-DD)

> date +%F
2009-01-02

Here is the full script.

#!/bin/sh
#set -x
#——————————————–
# ID: cabBackup.sh – BACKUP items to folder
# USAGE: ./cabBackup.sh
#——————————————–
# AUTHOR: Christopher Acido Bautista 2008-2009
# VERSION: 0.01

# TODO: arguments
# TODO: config file
# TODO: functions
# TODO: filtering

# Sources
# http://www.hsrl.rutgers.edu/ug/shell_help.html

# PATH
PATH=/opt/bin:/usr/bin:/bin; export PATH

# CONFIG

# — LIST OF FILES/FOLDERS TO BACKUP if not specified via console

FOLDERS=”/var/www/html /opt/sandbox”
#FOLDERS=””

# — BACKUP HERE
BACKUPFOLDER=”/opt/backup”

# — ARCHIVE COMMAND
COMPRESSCMD=”tar czfv “

# — DO NOT EDIT BEYOND THIS LINE (unless if you knw what ur doing ;] ) —

# Check if the folder exists if not make it
[ ! -d $BACKUPFOLDER ] && mkdir -p $BACKUPFOLDER || :

chown root:root $BACKUPFOLDER
chmod 755 $BACKUPFOLDER

# clean the screen
clear

echo lets start

## loop thru folders
for itm in $FOLDERS; do

FARCHIVE=$BACKUPFOLDER/`basename $itm`_`uname -n`_`date +%F`.tgz
$COMPRESSCMD $FARCHIVE $itm

done

echo .. done

Now name and save the file. I named mine cabBackup.sh.

You need to make the script executable.

> chmod 755 ./cabBackup.sh

There you have it just run it from the console.

> ./cabBackup.sh

There are still some things we need to do to make it a fully fledged automation script like configuration files, some pre-process commands and error trapping. But what we have now serves our purpose very well. I’ll post revisions of this script so watch for that.

Links
http://www.dartmouth.edu/~rc/classes/ksh/print_pages.shtml
http://tldp.org/LDP/abs/html/
Linux: Snippet – Backup using Tar and gzip

10 thoughts on “How to Write a Backup Shell Script using Tar and Gnu Zip

  1. Hello there! I know this is kind of off topic but I was wondering if you
    knew where I could get a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having difficulty finding one?
    Thanks a lot!

  2. Verү nice post. I јust stumbled uρon your weblog аnd wished to sаy that Ι’ve
    truly enjoyed browsing youг blog posts. Аfter all I wіll be subscribing tߋ your
    feed аnd ӏ hope you ᴡrite again ѕoon!

    Feel Free tⲟ check tҺіs too : Virtual Reality 2015

Leave a Reply

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