header image
Home arrow LUG How Tos arrow How To Backup With TAR
How To Backup With TAR Print
Aug 04, 2007 at 08:12 PM
Article Index
How To Backup With TAR
Page 2
Page 3
Page 4
 

Create file called backup.
Make it executable (chmod 775 backup).
Copy and paste backup script here.

#!/bin/sh
#
# Byron Holldorf
# 7-15-2007
# Backup script
#
###################################
    # CONFIGURATION SETTINGS
    #
    # TODO - THESE SHOULD BE SPLIT OUT INTO A CONFIG FILE
    #
    # level of backup to do.  Parameter 1 passed in
LEVEL=$1
    
    # a temporary directory for some files
TEMPDIR=/tmp
    
    # the remotely mounted directory to place the backup on
REMOTEDIR=/backup
    
    # a file containing patterns that should be ignored
IGNORELIST=/var/backup/ignorelist
    
    # the configuration file
DIRLIST=/var/backup/backup.conf
    
    # The name of this PC (for archive names)
PCNAME=PC1
    
    # files larger than this size will not be backed up
MAXSIZE=50M
    
    
BACKUP=$REMOTEDIR/$PCNAME-Backup-`date +%Y-%m-%d`-$LEVEL.tar.gz
SNAPSHOT1=$REMOTEDIR/$PCNAME-snapshot1.snar
SNAPSHOT2=$REMOTEDIR/$PCNAME-snapshot2.snar
FILELIST=$REMOTEDIR/$PCNAME-Backup.list
TEMPLIST=$TEMPDIR/temp.list
    
###################################

    # if no parameters are entered print usage
if [[ "$#" -lt "1" ]] ; then
    echo Usage backup [N]
    echo N = 0 - Full Backup
    echo N = 1 - Differential
    echo N = 2 - Incremental
    echo
    exit
fi

    # based on the backup level, remove and set the active snapshot file(s)
case $LEVEL in
0)
        # full backup.  Remove all snapshots
    if [[ -e $SNAPSHOT1 ]] ; then
        rm $SNAPSHOT1
    fi
    if [[ -e $SNAPSHOT2 ]] ; then
        rm $SNAPSHOT2
    fi
    SNAPSHOT=$SNAPSHOT1
;;
1)
        # differential.  Copy the latest full backup snapshot to 2
    if [[ -e $SNAPSHOT1 ]] ; then
        cp $SNAPSHOT1 $SNAPSHOT2
    fi
    SNAPSHOT=$SNAPSHOT2
;;
2)
        # incremental.  Use snapshot 2. 
        #If it doesn't exist, do a level 1 backup
    if [[ ! -e $SNAPSHOT2 ]] ; then
        cp $SNAPSHOT1 $SNAPSHOT2
    fi
    SNAPSHOT=$SNAPSHOT2
;;
esac

    # reset the list of backed up files
if [[ -e $FILELIST ]] ; then
    rm $FILELIST
fi

    # read the config file
while read enabled rrecurse rpath rpattern; do

    if [[ $enabled -ne 0 ]] ;
    then
            # set the recursion level based on the config file entry
        if [[ $rrecurse -ne 0 ]]; then
            recurse=100
        else
            recurse=1
        fi

            # find the files that match the current line of the
            # config file and append them to the end of our temp list
        find $rpath -maxdepth $recurse -type f -wholename "$rpath$rpattern" -size -$MAXSIZE -user $USER>>$TEMPLIST
    fi
done <$DIRLIST

    # remove ignored entries from the temp list and write the final list
grep -f $IGNORELIST $TEMPLIST -v >$FILELIST

    # use the incremental option of tar to do the actual backup. 
    #Use nice to allow other programs CPU usage
nice -n 19 tar --no-recursion --listed-incremental=$SNAPSHOT -T $FILELIST -czf $BACKUP



EC_LUG -Eau Claire Linux users group is an informal group of Linux and open source software enthusiasts.

We meet each Thursday at 7 p.m. at  420 Cafe, 420 Washington Ave. Eau Claire, Wisconsin.

Linux 101:
What's All The Fuss? video...

Mac vs. PC: How Would Linux Fit?
video1...

video2...
video3...

Linux User GroupsLug Link Logo