Linux / UNIX Tar Full and Incremental Tape Backup Shell Script

in Categories Backup last updated June 10, 2009

Install this script using cronjob.

See how to use tar and mt command.

To restore files / data from tar archives.

List the files:
# tar tvf /dev/st0
Extract the entire archive into current directory:
# tar xvpf /dev/st0
Extract only certain files or dirs into current directory. For example. Extract only home/vivek directory
# tar xvpf /dev/st0 home/vivek
You can also restore one file:
# tar xvpf /dev/st0 home/vivek/app/src/main.c

Share this on:

20 comment

  1. Hi

    Nice work, to better internationalization usage it looks better to use “normalized” date. Then use :
    NOW=$(date +”%w”)
    with case test from 0 to 6

    and to partial_backup() function tar -N yesterday disable error problems in another language context.

    With no English locales, there some pb without theses hacks

  2. Hi

    i have just found your website, and i try your nice script. Maybe a little update to do.
    Your script works only with English locales, for a better internationalization i twill be nice to change NOW=$(date +”%a”) to NOW=$(date +”%w”)

    Then do test between 0 and 6, with full backup to 1 (Monday)

  3. Thanks for the nice script Vivek,

    Good to see you haven’t added compression which is so widely used but can lead to disasters when tapes fail.

    Might be nice to extend this script to allow spanned archives however, in the event that data is larger than the storage capacity.

  4. how I add email notification to a user when the backup has finished successful or has stopped with errors.

    1. Add function like as follows:

      Find the line in full_backup()

      and append the following code to send email status backup_status $?, in the end should look as follows:

      Also, update partial_backup() as follows:


      1. When I run the backup everything seems to run fine, log looks clean. However, everytime I get a Backup failed.

        Could someone explain this part of the code?

        local s=0
        for d in $BACKUP_ROOT_DIR
        if [ ! -d /$d ];
        echo “Error : /$d directory does not exits!”
        # if not; just die
        [ $s -eq 1 ] && exit 1

        1. @ mrlayance,

          That function loops over all entries defined in $BACKUP_ROOT_DIR and ensures they exist. If they all exist, the function returns, which is correct, otherwise each directory that doesn’t exist is printed to STDOUT and then the script dies afterwards.

  5. How do I add that an email be sent when tar has been completed or if it has completed with errors or it just didn’t backup?

  6. Hi,

    I have used your script, but I get an error line 101: $LOGFILE: ambiguous redirect
    I run the script as root.

    The variable $LOGFILE get defined as /root/backup/log/Tue.backup.log

    The last part of the script is
    case $NOW in
    Mon|Tue|Wed|Thu|Fri) full_backup;;
    *) ;;
    esac > $LOGFILE 2>&1

    Kind regards,

    1. I found it. in line 34 of the script the variable is defined as $LOGFIILE instead of $LOGFILE

      Correct LOGFIILE=$LOGBASE/$NOW.backup.log in LOGFILE=$LOGBASE/$NOW.backup.log and it runs

    1. You have to create /root/.backup.exclude.conf as follows to exclude access_log or error_log file:

  7. ok I can fix the problem, but when I start again
    I got this message
    /bin/tar: /dev/st0: Wrote only 0 of 10240 bytes
    /bin/tar: Error is not recoverable: exiting now

    how to fix this problem,

  8. nice script
    but when I try got error like this
    ./ line 1: syntax error near unexpected token ('
    ./ line 1:
    UNIX / Linux shell script to backup dirs to tape device like /dev/st0 (linux)’

    I just copy n paste from your script n change some directory, can you help me… how to fix ?
    and how to backup multiple directory
    example I have
    and your script BACKUP_ROOT_DIR=”home sales”
    can I bring comma (,) like this BACKUP_ROOT_DIR=”home,home2,home3″


  9. Hi
    It’s great, but I can not use this script with Linux Enterprise 5. When I use command:
    “mt -f /dev/st0”, its return this error “-bash: mt: command not found”. Please suggest me, how can I backup file to tape.


  10. Hi Vivek,

    I want to use mutiple directory/sub-directory to get the files…


    Please guide me how to include mulitple folders in this script.


  11. I want to learn shell scripting so I need information about shell scripting in solaris unix or linux.Please send me detail information with basic concept.
    Thank U

    Have a question? Post it on our forum!