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

January 02, 2009

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.

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.


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

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

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


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].

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

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

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

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

echo the system’s computer name.

> uname -n

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

> date +%F

Here is the full script.

#set -x
# ID: – BACKUP items to folder
# USAGE: ./
# AUTHOR: codespud 2008-2009
# VERSION: 0.01

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

# Sources

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


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

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


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

# clean the screen

echo lets start

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

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


echo .. done

Now name and save the file. I named mine

You need to make the script executable.

> chmod 755 ./

There you have it just run it from the console.

> ./

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.

Linux: Snippet – Backup using Tar and gzip


Written by @codespud  chrisbautista