Rockie, apologies for the delay in getting back to you, but much as I enjoy my work, I tend not to spend 24 hours a day here if I can help it ;-)
Anyway, as far as I can see, you need to do the following:
Add a line to your shutdown script above the case statement:
touch <path>/normal_shutdown.flag
Then copy your sys_backup script to /etc/init.d and link it to S99sys_backup in /etc/rc3.d:
ln /etc/init.d/sys_backup /etc/rc3.d/S99sysbackup
You then need to edit your sys_backup file (in /etc/init.d) to include the following conditional test:
ls <path>/normal_shutdown.flag 2>&1 /dev/null
if [ $? != 0 ]
then
exit
else
............YOUR BACKUP SCRIPT........
fi
rm <path>/normal_shutdown.flag
In this way you create the flag file at the beginning of the shutdown and it is tested for by the backup script when the machine comes back up. If the flag file doesn't exist, the script exits, but if it does the backup continues and the flag file is removed at the end.
Thanks kencunningham.
sys_backup looks like this.. you want me to test like this..
sys_backup script:--
#!/bin/ksh
#
# Name: sys_backup
# Purpose: This script makes a backup copy of filesystems that has a
# corresponding "b" filesystem. For example, the root (/) filesystem
# will be backup if there is a /backup_root filesystem mounted. The /usr
# filesystem will be backup if a /backup_usr is existing. And so on ...
# This script also prepares the alternate root file system
# for use as a bootable file system in the event that the real root (/)
# crashes.
# Assumptions:
# The alternate file systems are normally mounted in a read-only state.
# This shell needs to remount them read/write, and after we're done,
# restore them to read-only state. (We do this so no one can mistake
# "unmounted" slices as spares to be used for whatever.) So .. we assume
# that fstab has desired mount specs/options specified, so we don't use
# device names in mount cmds
# Revision History:
# 01/03/2000 -
# * checks for existence of "b" filesystems, quits if is existing
# * only backups filesystems with corresponding "b" filesystem.
# * sys_backup now uses newfs to clean the "b" fs
# * ufsdump now uses the raw device as input
# * won't quit if DiskSuite is not installed
# * comments out swap not on backup disk
# * eliminated all SunOS 4.x commands
#
#
#
##################################START OF FUNCTIONS##########################
#
ls <path>/normal_shutdown.flag 2>&1 /dev/null
if [ $? != 0 ]
then
exit
else
trap_handler()
{
# mounts the /b fs read only
CTR=1
/usr/sbin/sync
while [ $CTR -le $NUMFS ]
do
echo \"remounting ${BFS[$CTR]} ro\"
remount ${BFS[$CTR]} ro
CTR=$((CTR+1))
done
echo "sys_backup ended at `date +"%Y%m%d%H%M%S"`" | /usr/bin/tee -a $LOGFILE
}
baderror()
{
# put message out on stderr and mail to admin if warranted
if /usr/bin/tty -s
then
echo "$1"
else
echo $1 | /usr/bin/tee -a $LOGFILE
echo $1 | $MAILER -s "sys_backup error on $HOSTNAME" $ERRORSTO
fi
}
remount()
{
# unmount, remount file system $1 with desired $2 ro/rw option
/usr/sbin/umount $1 >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
baderror "failed to unmount $1"
exit 1
fi
/usr/bin/sleep 2
/usr/sbin/mount -o ${2:?} $1 >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
baderror "failed to remount $1 with $2 option"
exit 1
fi
/usr/bin/sleep 2
/usr/sbin/mount | /usr/bin/grep "^$1" > /dev/null 2>&1
if [ $? -ne 0 ]
then
baderror "failed to remount $1 with $2 option"
exit 1
fi
}
cleanup()
{
# remove old data in backup filesystem specified in $1
/usr/sbin/umount $1 >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
baderror "failed to unmount $1"
exit 1
fi
/usr/bin/sleep 2
echo y | /usr/sbin/newfs -m 1 $2 >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
baderror "failed during newfs of $2"
exit 1
fi
/usr/bin/sleep 2
/usr/sbin/mount -o rw $1 >> $LOGFILE 2>&1
if [ $? -ne 0 ]
then
baderror "failed to mount $1 with rw option"
exit 1
fi
/usr/bin/sleep 2
/usr/sbin/mount | /usr/bin/grep "^$1" > /dev/null 2>&1
if [ $? -ne 0 ]
then
baderror "$1 not mounted after newfs"
exit 1
fi
}
dodump()
{
# Check again if backup filesystem is mounted
/usr/sbin/mount | /usr/bin/grep "^$4" > /dev/null 2>&1
if [ $? -ne 0 ]
then
baderror "$4 not mounted before ufsdump/ufsrestore"
exit 1
fi
if [ $? -ne 0 ]
then
baderror "failed to dump $1 to $4"
exit 1
fi
echo "`date`: filesystem $1 on device $2 was dumped to filesystem $4 on device $5" | /usr/bin/tee -a $1/README-DUMPDATE | /usr/bin/tee -a $4/README-DUMPDATE | /usr/bin/tee -a $LOGFILE
}
editfstab()
{
# edit the backup vfstab (or equiv) to be sure the backup fs's
# are the one's that are mounted as real /
# real filesystem $1, device $2, raw device $3
# backup filesystem $4, block device $5, raw device $6
OLDREAL=`/usr/bin/grep "^$2[ | ]" $BFSTAB`
/usr/bin/cp -p $BFSTAB $BFSTAB.$DATE.sys_backup
/usr/bin/sed -e "\\%$1[ | ]%{
s%$2%$5%
s%$3%$6%
i\###bkup###$OLDREAL
a\###bkup###ABOVE ENTRIES CHANGED TO ALTER THE PRIMARY DEVICE (USING BKUP)
}
\\%$4[ | ]%{
s%^%###bkup### %
a\###bkup###ABOVE ENTRY WAS DISABLED - THIS IS NOW THE PRIMARY DEVICE
}" $BFSTAB.$DATE.sys_backup > $BFSTAB
}
##################################END OF FUNCTIONS##########################
#
# check for sys_backup alias in /etc/aliases and NIS if existing
# if not, send error messages to root on machine
#
if /usr/bin/grep "^sys_backup:" /etc/aliases > /dev/null 2>&1
then
ERRORSTO=`/usr/bin/grep "^sys_backup:" /etc/aliases | /usr/bin/cut -d\: -f2-`
elif /usr/bin/ypwhich > /dev/null 2>&1
then
if /usr/bin/ypcat -k aliases | /usr/bin/grep "^sys_backup" > /dev/null 2>&1
then
ERRORSTO=`ypcat -k aliases | /usr/bin/grep "^sys_backup" | /usr/bin/cut -d" " -f2-`
else
ERRORSTO="root"
fi
else
ERRORSTO="root"
fi
#
# Check if user running the script is root
#
if [ `/usr/ucb/whoami` != root ]
then
baderror "This script must be run as root."
exit 1
fi
echo "\nStarting sys_backup: $DATE" | /usr/bin/tee -a $LOGFILE
echo "-----------------------------------" | /usr/bin/tee -a $LOGFILE
echo "errors will be mailed to $ERRORSTO" | /usr/bin/tee -a $LOGFILE
case `/usr/bin/uname -s` in
SunOS) case `/usr/bin/uname -r` in
5.4) HOSTNAME=`/usr/ucb/hostname`
BOOTBLK=/usr/lib/fs/ufs/bootblk
;;
5.5.1|5.6|5.7|5.8) HOSTNAME=`/usr/bin/hostname`
BOOTBLK=/usr/platform/`/usr/bin/uname -i`/lib/fs/ufs/bootblk
;;
*) baderror "SunOS `/usr/bin/uname -r` no longer supported"
exit 1
;;
esac
;;
*) baderror "`/usr/bin/uname -s` platform not supported at the moment"
exit 1
;;
esac
#
# check if there is /backup_root fs on the system,
# if missing, no use continuing, get out now!
# if existing, store in as the first entry in the arrays
#
/usr/sbin/mount $BROOT > /dev/null 2>&1
if [ $? -eq 1 ]
then
baderror "there is no $BROOT filesystem"
exit 1
else
FS[1]=/
FSDEV[1]=`/usr/sbin/mount | /usr/bin/grep "^${FS[1]} " | /usr/bin/awk '{print $3}'`
FSRDEV[1]=`echo ${FSDEV[1]} | /usr/bin/sed -e 's/dsk/rdsk/g'`
BFS[1]=$BROOT
BFSDEV[1]=`/usr/sbin/mount | /usr/bin/grep "^$BROOT " | /usr/bin/awk '{print $3}'`
BFSRDEV[1]=`echo ${BFSDEV[1]} | /usr/bin/sed -e 's/dsk/rdsk/g'`
echo "storing ${FS[1]} and ${BFS[1]} in the arrays" | /usr/bin/tee -a $LOGFILE
fi
#
# store the other /backup_ fs in the arrays
#
CTR=2
for i in `/usr/sbin/mount | /usr/bin/grep "^/backup_" | /usr/bin/awk '{print $1}'`
do
if [ $i != "$BROOT" ]
then
/usr/sbin/mount `echo $i | /usr/bin/sed -e 's/\/backup_/\//g'` > /dev/null 2>&1
if [ $? -ne 1 ]
then
FS[$CTR]=`echo $i | /usr/bin/sed -e 's/\/backup_/\//g'`
FSDEV[$CTR]=`/usr/sbin/mount | /usr/bin/grep "^${FS[$CTR]} " | /usr/bin/awk '{print $3}'`
FSRDEV[$CTR]=`echo ${FSDEV[$CTR]} | /usr/bin/sed -e 's/dsk/rdsk/g'`
BFS[$CTR]=$i
BFSDEV[$CTR]=`/usr/sbin/mount | /usr/bin/grep "^$i " | /usr/bin/awk '{print $3}'`
BFSRDEV[$CTR]=`echo ${BFSDEV[$CTR]} | /usr/bin/sed -e 's/dsk/rdsk/g'`
echo "storing ${FS[$CTR]} and ${BFS[$CTR]} in the arrays" | /usr/bin/tee -a $LOGFILE
CTR=$((CTR+1))
else
baderror "$i is not a backup filesystem. skipping ..."
fi
fi
done
#
# get the size of the arrays
#
NUMFS=`echo ${#FS[*]}`
#
# compare size of array with counter
#
if [ $NUMFS -ne $((CTR-1)) ]
then
baderror "size mismatch between arrays and counter"
exit 1
fi
#
# OK - next we assume the file systems were mounted readonly, and we will now
# attempt to unmount/remount them rw. we then retest that each is mounted
# before moving on
#
CTR=1
while [ $CTR -le $NUMFS ]
do
echo "remounting ${BFS[$CTR]} rw" | /usr/bin/tee -a $LOGFILE
if remount ${BFS[$CTR]} rw
then
echo "${BFS[$CTR]} successfully remounted rw" | /usr/bin/tee -a $LOGFILE
else
baderror "failed to remount ${BFS[$CTR]} with rw option"
exit 1
fi
CTR=$((CTR+1))
done
#
# from this point on, we need to set a trap to remount the file systems r/o
# if possible
#
trap trap_handler 0 1 15
#
# OK - time to clean up the old data in the backup filesystems
# and then do the dumps
#
CTR=1
while [ $CTR -le $NUMFS ]
do
echo "cleaning up ${BFS[$CTR]}" | /usr/bin/tee -a $LOGFILE
if cleanup ${BFS[$CTR]} ${BFSRDEV[$CTR]}
then
echo "${BFS[$CTR]} successfully cleaned" | /usr/bin/tee -a $LOGFILE
else
baderror "failed to clean ${BFS[$CTR]}"
exit 1
fi
/usr/bin/sleep 2
echo "copying ${FS[$CTR]} to ${BFS[$CTR]}" | /usr/bin/tee -a $LOGFILE
if dodump ${FS[$CTR]} ${FSDEV[$CTR]} ${FSRDEV[$CTR]} ${BFS[$CTR]} ${BFSDEV[$CTR]}
then
echo "${FS[$CTR]} successfully copied to ${BFS[$CTR]}" | /usr/bin/tee -a $LOGFILE
else
baderror "failed to copy ${FS[$CTR]} to ${BFS[$CTR]}"
exit 1
fi
CTR=$((CTR+1))
done
#
# OK - need to install the boot block on the backup
#
RAWDEV=/dev/rdsk/`basename ${BFSRDEV[1]}`
echo "installing bootblock for raw device $RAWDEV" | /usr/bin/tee -a $LOGFILE
/usr/sbin/installboot $BOOTBLK $RAWDEV > /dev/null 2>&1
if [ $? -ne 0 ]
then
baderror "failed to install bootblock on $RAWDEV"
exit 1
fi
#
# OK - need to adjust vfstab (or equivalent) entries for / in the
# backup devices so they point to themselves
#
CTR=1
#
# OK - need to edit system (kernel param) file so the meta device drivers
# are not loaded and Solstice DiskSuite is not started.
#
if /usr/bin/pkginfo -q SUNWmd
then
# edit the backup system (kernel param) file so that it ignores
# any meta device (Solstice DiskSuite) drivers
cp -p $BSYSTEM $BSYSTEM.orig
echo "editing file $BSYSTEM" | /usr/bin/tee -a $LOGFILE
if [ -f $BROOT/etc/rc2.d/S95SUNWmd.sync ]
then
mv $BROOT/etc/rc2.d/S95SUNWmd.sync $BROOT/etc/rc2.d/s95SUNWmd.sync
fi
if [ -f $BROOT/etc/rc3.d/S25mdlogd ]
then
mv $BROOT/etc/rc3.d/S25mdlogd $BROOT/etc/rc3.d/s25mdlogd
fi
#
# comment out other filesystems using meta devices
# in /backup_root/etc/vfstab file
#
for i in `/usr/bin/grep "^/dev/md" $BFSTAB | /usr/bin/awk '{print $1}'`
do
METADEV=`/usr/bin/grep "^$i[ | ]" $BFSTAB`
/usr/bin/cp -p $BFSTAB $BFSTAB.$DATE.sys_backup
/usr/bin/sed -e "\\%$i[ | ]%{
s%^%###bkup### %
a\###bkup###ABOVE ENTRY WAS DISABLED - META DEVICES ARE NOT EXISTING
}" $BFSTAB.$DATE.sys_backup > $BFSTAB
done
fi
#
# comment out all swap devices not in the backup disk
#
BSWAPDEV=`/usr/bin/basename ${BFSDEV[1]} | /usr/bin/cut -c1-6`
for i in `/usr/bin/grep "[ | ]swap" $BFSTAB | /usr/bin/grep "^/dev" | /usr/bin/awk '{print $1}' | /usr/bin/grep -v $BSWAPDEV`
do
/usr/bin/cp -p $BFSTAB $BFSTAB.$DATE.sys_backup
/usr/bin/sed -e "\\%$i[ | ]%{
s%^%###bkup### %
a\###bkup###ABOVE ENTRY WAS DISABLED - SWAP NOT IN BACKUP DISK
}" $BFSTAB.$DATE.sys_backup > $BFSTAB
done
#
# create mini-copy of abars home in backup root unless already there
#
if /usr/bin/grep "^/abars" $FSTAB > /dev/null 2>&1
then
if [ ! -s $BROOT/abars/restit ]
then
# the following is used to create/update an emergency mini-home
# directory for abars to be used in the event of an emergency.
#
# ABARSHOME specifies current/old/permanent ABARS home dir
# MINIHOME specifies new/temp/backup ABARS home dir
#
ABARSHOME=~abars
MINIHOME=$BROOT/abars
FILELIST=".auth_code rc.abars abarsd restit .profile"
if [ ! -d $ABARSHOME ]
then
baderror "Can not find $ABARSHOME directory (permanent home of ABARS)"
exit 1
fi
[ -d $MINIHOME ] || mkdir $MINIHOME
if [ ! -d $MINIHOME ]
then
baderror "Can not find/create $MINIHOME directory to copy ~ABARS to"
exit 1
fi
/usr/bin/chown abars $MINIHOME
/usr/bin/chmod 0755 $MINIHOME
/usr/bin/cd $ABARSHOME
for file in $FILELIST
do
/usr/bin/cp -p $file $MINIHOME
/usr/bin/chown abars $MINIHOME/$file
done
echo "`date`: files copied from $ABARSHOME to $MINIHOME: $FILELIST" | tee -a $MINIHOME/README-BACKDATE
fi
fi
fi
rm <path>/normal_shutdown.flag
# End of script
I did add in shutdown script as you mention me to do ..and all rest of the stuff.
Rockies, wow! I don't think I've ever seen such a complicated backup script - glad I don't have the job of maintaining it!
Your amendments look fine to me, but you'll need to change <path> to wherever you want to put the flag file (eg /temp/normal_shutdown.flag or whatever - if you don't have a /temp directory, create one under root). Of course the only way to test them is to try the scripts out, both in a scheduled shutdown scenario and in an unscheduled one. I meant to add in my previous post that if the amended scripts work, you can get rid of your backup script reference in the crontab - I assume you already know that? You also need to ensure that all the scripts are executable using chmod 755 or whatever. Let us know how you get on and good luck.
When implementing this got this error:-
when machine comesback up shows error with ..
/etc/rc3.d/S99sys_backup CTR=$ systax error on line 4
Do you have any idea why this error comes...
I have not change anything on sys_backup .only added
ls <path>/normal_shutdown.flag 2>&1 /dev/null
if [ $? != 0 ]
then
exit
else
----sys_backup script-----
it looks to me like you may have inadvertently done some damage to the first reference to the CTR variable. What I'd do in this instance would be to type out the whole line again so that you're sure there are no control characters in there. Also, don't forget that the <path> I included is merely an example that you should replace with your actual path (eg /temp).
If this doesn't work it may be worth your while calling the sys_backup script from the if..then..else..fi script, so that you replace the whole backup entry with a command to execute the backup itself, eg
/etc/rc3.d/S99sys_backup
Hope this makes sense! Let me know how you get on. Cheers.
ken,
In that particular case there should be some code to put in ..
that way sys_backup run only on that time duration !! not in any other case means emergency reboot !!
you know what i mean !!!
yes you're quite right. Disregard my last statement about putting the script in /etc/rc3.d.
There's no reason, however, why you couldn't use a script with the if..then..else..fi structure in /etc/rc3.d to call your sys_backup from wherever it is located if the condition for auto-reboot is met. HTH.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.