Linux / UNIX Tar Full and Incremental Tape Backup Shell Script

by on March 8, 2009 · 20 comments

  1. #!/bin/bash
  2. # A UNIX / Linux shell script to backup dirs to tape device like /dev/st0 (linux)
  3. # This script make both full and incremental backups.
  4. # You need at two sets of five tapes. Label each tape as Mon, Tue, Wed, Thu and Fri.
  5. # You can run script at midnight or early morning each day using cronjons.
  6. # The operator or sys admin can replace the tape every day after the script has done.
  7. # Script must run as root or configure permission via sudo.
  8. # -------------------------------------------------------------------------
  9. # Copyright (c) 1999 Vivek Gite <vivek@nixcraft.com>
  10. # This script is licensed under GNU GPL version 2.0 or above
  11. # -------------------------------------------------------------------------
  12. # This script is part of nixCraft shell script collection (NSSC)
  13. # Visit http://bash.cyberciti.biz/ for more information.
  14. # -------------------------------------------------------------------------
  15. # Last updated on : March-2003 - Added log file support.
  16. # Last updated on : Feb-2007 - Added support for excluding files / dirs.
  17. # -------------------------------------------------------------------------
  18. LOGBASE=/root/backup/log
  19.  
  20. # Backup dirs; do not prefix /
  21. BACKUP_ROOT_DIR="home sales"
  22.  
  23. # Get todays day like Mon, Tue and so on
  24. NOW=$(date +"%a")
  25.  
  26. # Tape devie name
  27. TAPE="/dev/st0"
  28.  
  29. # Exclude file
  30. TAR_ARGS=""
  31. EXCLUDE_CONF=/root/.backup.exclude.conf
  32.  
  33. # Backup Log file
  34. LOGFIILE=$LOGBASE/$NOW.backup.log
  35.  
  36. # Path to binaries
  37. TAR=/bin/tar
  38. MT=/bin/mt
  39. MKDIR=/bin/mkdir
  40.  
  41. # ------------------------------------------------------------------------
  42. # Excluding files when using tar
  43. # Create a file called $EXCLUDE_CONF using a text editor
  44. # Add files matching patterns such as follows (regex allowed):
  45. # home/vivek/iso
  46. # home/vivek/*.cpp~
  47. # ------------------------------------------------------------------------
  48. [ -f $EXCLUDE_CONF ] && TAR_ARGS="-X $EXCLUDE_CONF"
  49.  
  50. #### Custom functions #####
  51. # Make a full backup
  52. full_backup(){
  53. local old=$(pwd)
  54. cd /
  55. $TAR $TAR_ARGS -cvpf $TAPE $BACKUP_ROOT_DIR
  56. $MT -f $TAPE rewind
  57. $MT -f $TAPE offline
  58. cd $old
  59. }
  60.  
  61. # Make a partial backup
  62. partial_backup(){
  63. local old=$(pwd)
  64. cd /
  65. $TAR $TAR_ARGS -cvpf $TAPE -N "$(date -d '1 day ago')" $BACKUP_ROOT_DIR
  66. $MT -f $TAPE rewind
  67. $MT -f $TAPE offline
  68. cd $old
  69. }
  70.  
  71. # Make sure all dirs exits
  72. verify_backup_dirs(){
  73. local s=0
  74. for d in $BACKUP_ROOT_DIR
  75. do
  76. if [ ! -d /$d ];
  77. then
  78. echo "Error : /$d directory does not exits!"
  79. s=1
  80. fi
  81. done
  82. # if not; just die
  83. [ $s -eq 1 ] && exit 1
  84. }
  85.  
  86. #### Main logic ####
  87.  
  88. # Make sure log dir exits
  89. [ ! -d $LOGBASE ] && $MKDIR -p $LOGBASE
  90.  
  91. # Verify dirs
  92. verify_backup_dirs
  93.  
  94.  
  95. # Okay let us start backup procedure
  96. # If it is monday make a full backup;
  97. # For Tue to Fri make a partial backup
  98. # Weekend no backups
  99. case $NOW in
  100. Mon) full_backup;;
  101. Tue|Wed|Thu|Fri) partial_backup;;
  102. *) ;;
  103. esac > $LOGFILE 2>&1

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



4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • ayyanagouda

    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
    Ayyanagouda

  • pk

    Great … Good one :)

    Helped a lot .

  • Divakar

    Hi Vivek,

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

    Ex-
    /fr302/orar1/jcr1db/9.2.0/*
    /fr301/orar1/arch/jcr1/*
    /fr300/finr1/jcr1appl1/*

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

    Regards,
    Divakar

  • Tawatchai

    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.

    Thanks
    Tawatchai

  • Vivek Gite

    Install mt command

  • Aswin

    nice script
    but when I try got error like this
    ./backupAll.sh: line 1: syntax error near unexpected token `(‘
    ./backupAll.sh: 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
    /home/
    /home2/
    /home3/
    and your script BACKUP_ROOT_DIR=”home sales”
    can I bring comma (,) like this BACKUP_ROOT_DIR=”home,home2,home3″

    regards
    aswin

  • Aswin

    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,
    regards
    aswin

  • Aswin

    how to description $EXCLUDE_CONF file

    regards
    aswin

  • Vivek Gite

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

    access_log
    error_log
    /var/log/*.err
    
  • Arjan

    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,
    Arjan

  • Arjan

    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

  • Jane

    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?

  • Jane

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

  • Vivek Gite

    Add function like as follows:

    backup_status(){
     local s=$1
    if [ $s -eq 0 ]
    then
          echo "Backup Successfully done @ $(hostname)" | mail -S 'Backup Done' you@example.com
    else
          echo "Backup FAILED @ $(hostname)" | mail -S 'Backup Failed' you@example.com
    fi
    }

    Find the line in full_backup()

    $TAR $TAR_ARGS -cvpf $TAPE $BACKUP_ROOT_DIR

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

    $TAR $TAR_ARGS -cvpf $TAPE $BACKUP_ROOT_DIR
    backup_status $?

    Also, update partial_backup() as follows:

    $TAR $TAR_ARGS -cvpf $TAPE -N "$(date -d '1 day ago')" $BACKUP_ROOT_DIR
    backup_status $?

    HTH

  • mrlayance

    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?

    verify_backup_dirs(){
    local s=0
    for d in $BACKUP_ROOT_DIR
    do
    if [ ! -d /$d ];
    then
    echo “Error : /$d directory does not exits!”
    s=1
    fi
    done
    # if not; just die
    [ $s -eq 1 ] && exit 1
    }

  • hrabbit

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

  • hrabbit

    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.

  • azerttyu

    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)

  • azerttyu

    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

  • Cr0t

    You might want to check out dar.

Previous Script:

Next Script: