Automated creation of mksysb and copy to NFS Server
Posted on AIX, Scripting, Automation
I have a bunch of systems that we are working on, however we don’t have the tape library connected yet. It makes me very nervous to not have any backups so I put this together. It creates a mksysb on the local system and copies it to the NFS export. In my case I created this on my NIM server so if I needed to create a SPOT and reinstall or cut it to tape it’s already available there. It’s pretty much ready to use so all you only need to do a few things:
- Update the variables
- Copy it to each system
- Schedule with cron
#!/usr/bin/ksh
HOSTNAME=`uname -a | awk '{print $2}'`
DATE=$(date +%m%d%Y)
FILENAME=$HOSTNAME.$DATE
RETAIN_LOCAL_BACKUPS=1
RETAIN_NFS_BACKUPS=7
BACKUPDIR="/mksysb/nfs_mksysb"
NFSSERVER="NFSServer"
# Check to make sure the directory we need to mount is created
if [ ! -e "/mksysb/nfs_mksysb" ]; then
mkdir -p /mksysb/nfs_mksysb
fi
# Determine if the NFS share is mounted unless it's the server serviing the NFS Mount
if [ ! "$NFSSERVER" == "$HOSTNAME" ]; then
mount | grep nfs_mksysb || mount "$NFSSERVER:/mksysb/nfs_mksysb" "$BACKUPDIR"
mount | grep nfs_mksysb && MOUNTED = 1
fi
# Ensure the directory for the system is created
if [ ! -e "$BACKUPDIR/$HOSTNAME" ]; then
mkdir -p "$BACKUPDIR/$HOSTNAME"
fi
# Everything is mounted and ready to go - lets create our backup
/usr/bin/mksysb -e -i "/mksysb/$FILENAME"
# MKSYSB is completed so lets copy it over to the NFS share
cp "/mksysb/$FILENAME" "$BACKUPDIR/$HOSTNAME/"
# Clean up local directories
find /mksysb \( ! -name mksysb -prune \) -name "$HOSTNAME.*" -mtime +$RETAIN_LOCAL_BACKUPS -exec rm {} \;
# Clean up NFS share
find "${BACKUPDIR}/${HOSTNAME}" -name "${HOSTNAME}.*" -mtime +${RETAIN_NFS_BACKUPS} -exec rm {} \;
# We are finished so lets unmount the share
[ ! -z "$MOUNTED" ] && umount /mksysb/nfs_mksysb