Home » Scripts » Tape Backup Script

Tape Backup Script

terminalTape Backup Script

 

 

 

Tape Backup Script

A quick script using tar, gzip and dump to write backups to a tape backup device.

 

tape-backup.sh
#!/bin/sh
#
# backup.sh
# a small shell script to tar and gzip a remote dir locally, then dump a local directory to tape
#
# If you want to run from cron or automatically you must use ssh keys
#
# VARIABLES, update per your needs
#
HOST=192.168.1.254 #hostname or IP of host with directory to back up
USER=admin #username to run as
RDIRECTORY=”/u01″ #remote dir
LDIRECTORY=”/backup” #dir to backup to locally
FILENAME=”oracle_backup”
RECIPIENT=”john@domain.com” #change this to your
LHOST=/bin/hostname
TSOFT=/usr/sbin/mtx #tape drive software location
TDRIVE=’/dev/sg5′
#
# END VARIABLES
#
#
TIME_STAMP=`date +’%Y%m%d’`
#
HSIZE=`ssh -l $USER $HOST du -hcs $DIRECTORY | awk ‘/total/ {print $1}’`
/usr/bin/logger Starting to pull backup from $HOST to $LHOST. Uncompressed backup size is $HSIZE .

ssh -l $USER $HOST tar -cf – $LDIRECTORY | gzip | cat > $FILENAME_`/bin/date +%Y%m%d`.tar.gz
#
# Check to see if backup completed or failed
if [ X$? != X0 ]; then
mail -s “$HOST backup failed” $RECIPIENT < error.log
/usr/bin/logger Pulling backup from $HOST Failed.
exit 1
fi
#
# If dump completed, then we are ok.
/usr/bin/logger Backup Complete
#
#
# Since backup is complete, we can delete yesterday’s backup.
# as it was already archived to tape.
# UNCOMMENT AT YOUR OWN RISK: This is SUPPOSED to delete the day prior’s backup
#rm -rf $LDIRECTORY/$FILENAME_`/bin/date -d ‘1 days ago’ +%Y%m%d`.tar.gz
#rm -rf $LDIRECTORY/$FILENAME_`/bin/date -d ‘1 week ago’ +%Y%m%d`.tar.gz

#
LSIZE=`ls -alh $LDIRECTORY/$FILENAME__${TIME_STAMP}.tar.gz | awk ‘{print $5}’`
/usr/bin/logger local compressed file size is $LSIZE

# dump to tape after pulling backup
#
TAPENUM=`$TSOFT -f $TDRIVE status |grep Empty | awk ‘{print $1, $2, $3}’ | awk -F’:’ ‘{print $1}’`
# add entry to /var/log/messages
/usr/bin/logger Running script /root/BACKUPS/dump.sh onto $TAPE
#
#
DAY_TIME_STAMP=`/bin/date -d ‘1 day ago’ +%Y%m%d`
#
#todays dump
OSIZE=`ls -alh $LDIRECTORY/$FILENAME_${TIME_STAMP}.tar.gz | awk ‘{print $5}’`
#
# add entry to /var/log/messages defining total dump size
DUMP_SIZE=`du -hcs /backup/ | awk ‘/total/ {print $1}’`
#
/usr/bin/logger Starting backup to tape. Total dump size is $DUMP_SIZE .
#
# This is the tape dump command
/sbin/dump -b 256 -0u -f /dev/st0 /backup/ 2>>/tmp/tape-error.log.$$
#/sbin/dump -0u -f /dev/st0 /backup/ 2>>/tmp/tape-error.log.$$
#
# Check to see if backup completed or failed
if [ X$? != X0 ]; then
/bin/mail -s “Backup to tape failed” $RECIPIENT < /tmp/tape-error.log.$$
/usr/bin/logger Backup to tape FAILED
exit 1
fi
#
#
/usr/sbin/mtx -f /dev/sg5 unload
#
# If dump completed, then we are ok.
#
/usr/bin/logger Backup to tape COMPLETE. Total dump size is was $DUMP_SIZE .
echo “Backup to tape COMPLETE. Total dump size is was $DUMP_SIZE . Tape is in $TAPE”>/tmp/dump.log
#
echo ” ” >>/tmp/dump.log
#
ls -alh /backup/Oracle >> /tmp/dump.log
#
echo ” ” >>/tmp/dump.log
#
df -h |grep File >> /tmp/dump.log
df -h | grep backup >> /tmp/dump.log
#
cat /tmp/dump.log |/bin/mail -s “Daily Tape backup info” $RECIPIENT
#
# Add entry to /var/log/mesages for script completion
/usr/bin/logger script $SCRIPTHOME/tape_backup.sh COMPLETED
rm -rf /tmp/tape-error.log.$$

Was this helpful?
If you found my posts interesting, or I helped you figure something out, please consider tossing me a tiny gratuity for my time, research, to help offset the web hosting/development costs or to buy me a cup of coffee.

* This post may contain affiliate links. This means that I “might” receive a tiny amount of money, if you make a purchase using one of our links. It does not cost you anything extra, and helps us defray the costs of hosting and maintaining this site.

Leave a Reply