#!/bin/bash
# **************************************************************
# file: backup
# date: 2006-02-08 22:27
# author: Marko Schulz - <info@shellXpress.de>
# description: simple backup script with GNU tar
# **************************************************************
#
# I put the following line in my crontab. This make every day
# an incremental backup other than sunday a full backup.
#
# These lines must be on line in your crontab.
# 0 23 * * * [ -n "`date +%a | grep -E '^(So|Sun)'`" ]
# && ( /usr/local/sbin/backup -f )
# || ( /usr/local/sbin/backup -i ) >/dev/null 2>&1
#
# Directory(s) which will be backup.
backup="/etc /home /root /usr/local/bin /usr/local/sbin /var/cache /var/lib /var/mail /var/spool"
#
# File in which stored the date of the last full backup.
datefile="/var/log/backup/date.log"
#
# Directory in which the created backup stored.
dumpdir="/data/backup"
#
# Files and directorys list in these file will not backup.
prunefile="/var/log/backup/exclude.log"
#
# Mail configuration:
# Switch to line 77 and follow (mail section) to set your
# mail variables...
#
# **************************************************************
# program action...
# Get current date...
current=$( date +"%m/%d/%y %H:%M" )
# Get last backup date...
date=$( cat $datefile 2>/dev/null )
# Temporary error variable...
ERROR=0
# Lets go...
case "$1" in
-f|--full)
# Update full backup date...
( echo $current > $datefile; chmod 0640 $datefile )
# Get Full backup...
( tar --exclude-from $prunefile
-czf $dumpdir/backup-full-$( hostname )-$( date +"%Y-%m-%d_%H%M" ).tar.gz $backup >/dev/null 2>&1 )
# Set exit state...
[ "$?" != 0 ] && ERROR=1
;;
-i|--incremental)
# Get Incremental Backup...
( tar -N "$date"
--exclude-from $prunefile
-czf $dumpdir/backup-incremental-$( hostname )-$( date +"%Y-%m-%d_%H%M" ).tar.gz $backup >/dev/null 2>&1 )
# Set exit state...
[ "$?" != 0 ] && ERROR=2
;;
*)
# Bad exec...
echo -e "Usage: $( basename $0 ) -f --full | -i --incremental"
echo -e "t-i|--incremental get incremental backup"
echo -e "t-f|--full get full backupa"
# Set exit state...
ERROR=3
;;
esac
# Sent notify mail, if an error is given.
if [ "$ERROR" != 0 ]; then
# define subject depending on error state...
if [ "$ERROR" = 1 ]; then
subject="[$( hostname )] Full backup failed"
headline="Full backup failed on $( hostname )"
elif [ "$ERROR" = 2 ]; then
subject="[$( hostname )] Incremental backup failed"
headline="Incremental backup failed on $( hostname )"
elif [ "$ERROR" = 3 ]; then
subject="[$( hostname )] Backup failed"
headline="Backup failed on $( hostname )"
fi
# Define mail header To/From/Subject etc...
mail="To: "Your Name" <your@domain.de>n"
mail="${mail}Reply-to: "Your Name" <your@domain.de>n" mail="${mail}From: "System Operator" <root@$( hostname ).your.domain>n" mail="${mail}X-Mailer: Custom-CGI: $( basename $0 )n" mail="${mail}MIME-Version: 1.0n" mail="${mail}Subject: $subjectn" mail="${mail}Importance: Highn" mail="${mail}Content-Type: text/plain; charset=iso-8859-1n" mail="${mail}Content-Transfer-Encoding: 8bitnn"
# Define mail body...
body="${mail}================================================n" body="${body}program: $0n" body="${body}hostname: $( hostname )n" body="${body}contact: Your Name <your@domain.de>n" body="${body}date/time: $( date +"%Y-%m-%d %H:%M:%S" )n" body="${body}================================================nn" body="${body}*${headline}*nn"
# Here can you read an error file and put the output to message.
# eg. message="$( cat /path/to/errorfile )n"
# or type in what you thing :-)
message=""
# Concatenate message to body...
body="${body}${message}n"
# sent mail with sendmail...
echo -e "${body}" | /usr/sbin/sendmail -t
fi
# exit program...
exit $ERROR
# **************************************************************
# end of this script...