Sunday, 25 January 2009

Mksysb backup shell script for Unix

Here is a shell script I wrote to perform a backup of an AIX system. It has been used but not fully tested and as such I would test running a backup and restore before using on a live server.


#!/bin/ksh

#AIX Backup Script
#Written on 27-11-08

# Using mksysb and savevg for easier system rebuilds.
# Please see notes at the bottom for RESTORE HELP..
# Scheduled for every Friday. cpio used Monday - Thursday
#
# Please add volume groups to backup to the MY_VG below
# This script will ensure all non root groups are backed up

#Setup variables for script

TODAY=`date +%A`
WORKDIR="/home/scripts" # Scipt home directory
BCKLOG="backup.log"
LOG=${WORKDIR}/${BCKLOG} # File to log backup progress to
ERRORLOG=${WORKDIR}/${BCKLOG}.error
MY_VG="datavg " # Separate VG names with a space if more than one ot back up ie " datavg homevg logvg"
FSF_COUNT=0 # Set the FSF count during non root vg backups
MAIL_GROUP="user1@company.com;group@company.com" # Setup addresses for mail

# Setup The Functions >>>

date_func() {
# Find yesterdays date
DOW=`date +%u`
case $DOW in

1)
YESTERDAY="Sunday"
;;
2)
YESTERDAY="Monday"
;;
3)
YESTERDAY="Tuesday"
;;
4)
YESTERDAY="Wednesday"
;;
5)
YESTERDAY="Thursday"
;;
6)
YESTERDAY="Friday"
;;
7)
YESTERDAY="Saturday"
;;
esac
}

# Function - Rotate backup logs
log_rotate()
{
if [ -s ${LOG} ]
then
mv -f ${LOG} ${WORKDIR}/${YESTERDAY}.${BCKLOG}
echo "Backup log rotated successfully" >> ${LOG}
else
echo "Error no Backup log found - Please investigate" >> ${LOG}
fi
}
# Function - Rotate Error logs
errorlog_rotate()
{
if [ -s ${ERRORLOG} ]
then
mv -f ${ERRORLOG} ${WORKDIR}/${YESTERDAY}.${BCKLOG}.error
echo "ERROR log rotated successfully for ${YESTERDAY}" >> ${LOG}
else
echo "" >> /dev/null
fi
}

# Rewind function.
rmt_rewind()
{
# Ensure tape has rewound successfully
echo "Rewinding Tape... at "`date` >> ${LOG}
mt -f /dev/rmt0 rewind 2>${ERRORLOG}
if [ $? -ne 0 ]
then
echo "Error Tape Rewind Failed at "`date` >> ${LOG}
else
echo "Success Tape has Rewound at "`date` >> ${LOG}
fi
}

bck_mksysb() {
# The backup does not use the -i or -m flag as to not overwrite the custom files below
# /image.data and /bosinst.data files
# The mksysb uses the verify option -V
echo "Starting Backup of rootvg at" `date` > ${LOG}
echo "Running mksysb /dev/rmt0 -V " >> ${LOG}
/usr/bin/mksysb /dev/rmt0.1 -V >> ${LOG} 2>${ERRORLOG}
if [ $? -ne 0 ]
then
echo "!!! Error mksysb Failed - Please investigate" >> ${LOG}
else
echo "* Finished Backup of rootvg at" `date` >> ${LOG}
fi
rmt_rewind
}

bck_non_rootvgs() {
# Begin Non root Backup function
# Finds number of volume groups to backup
NUM_VG=`print ${MY_VG}| wc -w`
FSF_TOTAL=`expr ${NUM_VG} + 3`
# Begin volume backup loop
for NRVG in ${MY_VG}
do
while [ $FSF_COUNT -lt $NUM_VG ]
do
FSF_COUNT=`expr 1 + $FSF_COUNT`
FSF=`expr $FSF_COUNT + 3`
# Skip tape forward to end of mksysb ready for next VG
echo " Preparing to backup ${NRVG}" >> ${LOG}
echo "Skipping forward ${FSF} blocks at "`date` >> ${LOG}
# Fast Forward tape position for non root vg
/usr/bin/tctl -f /dev/rmt0.1 fsf $FSF 2>${ERRORLOG}
if [ $? -ne 0 ]
then
echo "!!! Failed to skip to end of backup - Please investigate" `date` >> ${LOG}
else
echo "* Skipped forward ${FSF} Blocks" `date` >> ${LOG}
fi
# Beginning backup of vg's.
# The flag -i Creates the data file by calling the mkvgdata command; -f specifies the device.
echo "* Starting Backup of ${NRVG} at" `date` >> ${LOG}
/usr/bin/savevg -i -f /dev/rmt0 ${NRVG} >> ${LOG} 2>${ERRORLOG}
if [ $? -ne 0 ]
then
echo "!!! Failed to savevg for ${NRVG} - Please investigate" >> ${LOG}
else
echo "* Finished Backup of ${NRVG} at" `date` >> ${LOG}
fi
rmt_rewind
done
echo "* Backup completed at" `date` >> ${LOG}
done
}

# Backup test script
#test_backup() {
# Verify VG headers
# check
# Uncomment to enable backup verification
# This takes a little while to complete as it has to perform several checks to verify integrity.
# You should not rely on this test and should perform full test restores to ensure they work.
# lsmksysb -l -f /dev/rmt0.1
#rmt_rewind
#}

# Gather system information
vg_info()
{
for VG_LIST in `/usr/sbin/lsvg`
do
lsvg -l ${VG_LIST} >> ${LOG} 2>${ERRORLOG}
echo "\n\n" >> ${LOG}
done
for PV_LIST in `lspv | awk ' { print $1 } '`
do
lspv -l ${PV_LIST} >> ${LOG} 2>${ERRORLOG}
echo "\n\n" >> ${LOG}
done
/usr/bin/df -m | tr -s " " | sed 's/ /, /g' | sed '1 s/, / /g' >> ${LOG} 2>${ERRORLOG}
echo "\n\n" >> ${LOG}
}

bck_email()
#Mail Function configure names in MAIL_GROUP
{
# Mail LOG files if they exist
if [ - s ${LOG} ]
then
cat ${LOG} | mail -s "Backup of ${BCK_HOST} at `date +%A`" $MAIL_GROUP 2>${ERRORLOG}
else
mail -s "!Problem - ${BCK_HOST} backup cannot mail logs at `date +%A`" $MAIL_GROUP 2>${ERRORLOG}
fi
# Mail Error LOG files if they exist
if [ - s ${ERRORLOG} ]
then
cat ${ERRORLOG} | mail -s "!Backup of ${BCK_HOST} at `date +%A` has an error" $MAIL_GROUP 2>${ERRORLOG}
else
mail -s "!Problem - ${BCK_HOST} backup cannot mail Error logs at `date +%A`" $MAIL_GROUP 2>${ERRORLOG}
fi
}

# Functions Finish <<<

# Begin Backup Script MAIN >>>

# Run functions to clean up logs and rotate
date_func
log_rotate
errorlog_rotate
echo "\n\n" >> ${LOG}

# Run mksysb
bck_mksysb
echo "\n\n" >> ${LOG}

# Run all non-rootvg backup
bck_non_rootvgs
echo "\n\n" >> ${LOG}

# Compete log files with some useful info
echo "More Information on Volume groups" >> ${LOG}
vg_info

# Eject the tape and make device offline
mt -f /dev/rmt0 offline
echo "Tape Device rmt0 offline" `date` >> ${LOG}
echo " " >> ${LOG}
echo "Backup logs can be found" ${LOG} >> ${LOG}

# Send backup logs via email.
# Uncomment out if you wish to use it.
# bck_email
# Backup up script END <<<

###############################
# #
# How to RESTORE the Backups #
# #
###############################

# If you are reading this then something has gone wrong with your backup
# First Rule don't panic !
# Second Rule understand the backup process and recovery.
# Third Rule I am not responsible for any data loss, All backups should be tested regularly to ensure you can recover
# from an unforeseen situation.
# The procedure above has been tested and worked for me, it may need some changing depending on your hardware set-up.

# I will explain to you the process to restore from the above backup procedure
# NOTE OF CAUTION ALWAYS ENSURE THE BACKUP TAPE IS WRITE PROTECTED.
# DO NOT TOUCH THE SYSTEM UNTIL TAPE IS SECURE.

# Insert tape into server drive
# A mksysb only contains the rootvg, you will need to restore the data after this process.
# To ensure the server will boot from tape drive
# bootlist -m normal -o
# List the boot order
# bootlist -m normal rmt0 hdisk0 cd0
# Sets the boot order to tape disk then cd; Choose accordingly.
# If this fails or you can't run these commands you will need to boot from the CD media used to install the server,
# note it has to be the same version.
# The mksysb tape should load and ask if you are ok to install, accept defaults and 15mins later your system will reboot.
# You should now have the OS back

# Restore the datavg
# Rewind tape to the beginning
# mt -f /dev/rmt0 rewind
# Skip tape forwards to where the datavg is, this will be after the mksysb which is on blocks 0,1,2,3
# tctl -f /dev/rmt0.1 fsf 4
# restvg -f /dev/rmt0 hdiskX replace with the drive you wish to restore too.
# This should restore the volume group and the data.
# If you are still reading this and our lost so am I and I wrote it!
# Some further commands to help.
# lspv ; List's the Physical Volumes, will show the volume groups on your server
# lspv -l hdisk3 ; list's the contents of the hard disk specified.
# lsvg -l datavg ; Lists the logical volume details in the vg specified.

No comments:

Post a Comment