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


  1. goodBoy78 says:

    Wow im first! Nice script! I hope you continue sharing. More power!

  2. Anonymous says:

    Thanks for the script and the excellent detailed explanation!

    1. chris says:

      You’re welcome!

  3. Maxwell says:

    Thanx man this script is awesome good luck to your future creations

  4. Wonderful, ѡhat а website it іs! Тɦiѕ blog providеs
    ᥙseful data tߋ us, keep it up.

    Feel Free to check this too : Reality Uk

  5. 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!

    1. chris says:

      Hi brigitte,

      There’s a plethora of good ones from the wordpress directory.

      If you’re knowlegeable enough, you can also make one.

      If you just want to prevent bots from abusing your comment form. Try Anti Spam or akismet

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

    1. chris says:

      You;re welcome! Will do!

Leave a Reply

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