I’m using this script in a lot of Linux Server, especially some file server with a little ERP software written in COBOL with all files in /home/erp.
I’m saving all /home and /etc to have a full backup for disaster recovery purpose.

This scrit will do the following :

- Define variables
- Define DATE
- Create a compress archive of /home and /etc
- Rewind the TAPE
- Write all the data on TAPE
- Verify the TAPE
- Eject the TAPE

Obviously all the output of the above operations will be redirected to a log file fr diagnostic purpose.

- Mail all logged operations to the Server admin

The only prerequisites for let all works is “mail” packages installed on your system and a local mail server configured for routing mail to your “Server Admin” email address, I’m using Postfix, but you coul’d use whatever you want (eg. QMail or EXIM).
The following is the script :


##############
# Start Script
##############
#!/bin/sh
#
# Backup Script created by Riccardo Riva
#

BACKUP_BASE=/home/backup;                      export BACKUP_BASE
DATA_DIR=/home;                                                export DATA_DIR
DATA_EXP=$BACKUP_BASE/home.tgz;      export DATA_EXP
CONFIG_DIR=/etc;                                                 export CONFIG_DIR
CONFIG_EXP=$BACKUP_BASE/etc.tgz;       export CONFIG_EXP
LOG_BASE=$BACKUP_BASE/log;                    export LOG_BASE
LOG=$LOG_BASE/backup.log;                          export LOG
LOG_HISTORY=$LOG_BASE/history.log;    export LOG_HISTORY
TAPEDEV=/dev/st0;                                              export TAPEDEV
MT=/usr/bin/mt;                                                     export MT
ADMIN_MAIL=you@yourdomain.com;       export ADMIN_MAIL

# Define DATE to put it in the log
DATE ()
{
date +"%d-%m-%Y %H:%M:%S\t"
}
# Define DATE_SHORT to put in the mail report
DATE_SHORT ()
{
date +"%d-%m-%Y"
}
# End defining DATE

# Delete old exported files
rm -rf $DATA_EXP >/dev/null 2>&1;
rm -rf $CONFIG_EXP >/dev/null 2>&1;

# Put the last log in history log
cat $LOG >> $LOG_HISTORY;

# Write in the log the backup procedura START DATE
printf "############\n" >$LOG;
printf "%s `DATE` Start BACKUP procedure \n" >>$LOG;
printf "###########\n" >>$LOG;

printf "############\n" >>$LOG;
printf "%s `DATE` Start BACKUP and compress  $DATA_DIR \n" >>$LOG;
printf "############\n" >>$LOG;

# Create an archive with /home escluding /home/backup/ from the compress file
tar -czvf $DATA_EXP --exclude=$BACKUP_BASE/* $DATA_DIR >/dev/null 2>&1;

printf "############\n" >>$LOG;
printf "%s `DATE` Start BACKUP and compress $CONFIG_DIR \n" >>$LOG;
printf "############\n" >>$LOG;

# Create an archive with /etc
tar -czvf $CONFIG_EXP $CONFIG_DIR >/dev/null 2>&1;

printf "############\n" >>$LOG;
printf "%s `DATE` End EXPORT procedure \n" >>$LOG;
printf "###########\n" >>$LOG;
printf "############\n" >>$LOG;
printf "%s `DATE` Start rewind tape \n" >>$LOG;
printf "###########\n" >>$LOG;

# Rewind TAPE
$MT -f $TAPEDEV rewind  >/dev/null 2>&1;

printf "############\n" >>$LOG;
printf "%s `DATE` Start writing on TAPE \n" >>$LOG;
printf "############\n" >>$LOG;

# Write on TAPE all $BACKUP_BASE excluding $LOG_BASE/* e write the output in the log
tar cf $TAPEDEV --exclude=$LOG_BASE/* $BACKUP_BASE >/dev/null 2>&1;

# Check the operation result
if [ $? -eq 0 ];then

printf "############\n" >>$LOG;
printf "%s `DATE` Write operation on TAPE was succesfully \n" >>$LOG;
printf "############\n" >>$LOG;

else

printf "############\n" >>$LOG;
printf "%s `DATE` Write operation on TAPE FAILS !!! \n" >>$LOG;
printf "############\n" >>$LOG;

fi

printf "############\n" >>$LOG;
printf "%s `DATE` End writing n TAPE \n" >>$LOG;
printf "############\n" >>$LOG;

printf "############\n" >>$LOG;
printf "%s `DATE` Start checking TAPE \n" >>$LOG;
printf "############\n" >>$LOG;

# Check TAPE content and write output in the log
tar tf $TAPEDEV >>$LOG;
if [ $? -eq 0 ];then

printf "############\n" >>$LOG;
printf "%s `DATE` Checking TAPE content successfully \n" >>$LOG;
printf "############\n" >>$LOG;

# Eject TAPE
$MT -f $TAPEDEV eject   >/dev/null 2>&1;
else

printf "############\n" >>$LOG;
printf "%s `DATE` Checking TAPE content FAILS !!! \n" >>$LOG;
printf "############\n" >>$LOG;

fi

printf "############\n" >>$LOG;
printf "%s `DATE` End checking TAPE \n" >>$LOG;
printf "############\n" >>$LOG;

printf "############\n" >>$LOG;
printf "%s `DATE` End BACKUP procedure \n" >>$LOG
printf "############\n" >>$LOG;
printf "\n\n" >>$LOG;

# Send BACKUP report via mail to $ADMIN_MAIL
cat $LOG | mail -s "Backup Report for `uname -n` date `DATE_SHORT` " $ADMIN_MAIL

##############
# End Script
##############

Hope this help

Bye
Riccardo

Print This Post Print This Post

One Response to “Backup script for Linux File Server on Tape”

Leave a Reply

Spam Protection by WP-SpamFree

Contacts
Look at me at Linkedin Follow me on Twitter
My Flickr Albums My Facebook profile My YouTube Videos
SkypeMe My Linux Counter GMail me
Search
Google Search
Categories
Tag Cloud 3D
FeedBurner RSS

Visitors
Locations of visitors to this page
VMware related Blogs
The following are Blog sites with feeds I personally follow. When I'll have some spare time I will complete all Feed and Twitter links.