CodeSpud

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

January 02, 2009

bashlinuxscript

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 these articles.

Unix shell scripting with ksh/bash Advanced Bash-Scripting Guide

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: codespud 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

By @codespud  
DISCLAIMER This is my personal weblog and learning tool. The content within it is exactly that – personal. The views and opinions expressed on the posts and the comments I make on this Blog represent my own and not those of people, institutions or organisations I am affiliated with unless stated explicitly. My Blog is not affiliated with, neither does it represent the views, position or attitudes of my employer, their clients, or any of their affiliated companies.