AIX Tip of the Week

Subject: Script to Automate System Backups Over the Network Using NIM

Audience: All

Date: December 5, 2006

A basic IT "best practice" is to do regular system backups**. The system backup should be validated by periodically restoring to a test machine by someone other than the creator of the backup.

The attached script automates system backups of AIX servers to a NIM server over the network. The script runs on the NIM server, either manually or as a cron job. The documentation is inside the script.

I highly recommend using NIM (network installation manager) for the following reasons:

  1. Convenience: ability to do remote installs, backups, restores, updates
  2. Productivity: ability to manage software by groups of servers
  3. Lower cost: eliminates need for a back device (tape, DVD) on each server

NIM is part of the base AIX operating system. It has a "learning curve" and you should read the documentation carefully. For more information, see http://publib.boulder.ibm.com/infocenter/pseries/v5r3/topic/com.ibm.aix.install/doc/insgdrf/nim_intro.htm

** A system back is different than a data backup (tar, cpio, etc). A system can be used for a "bare-metal" restore of the operating system. I recommend making regular system backups (monthly/quarterly), as well as, before and after making significant system changes. And don't forget, system backups only backup "rootvg", so you still need to do data backups in other volume groups.



#!/bin/ksh
#
# nim_mksysb [-h] [-r] [-n] [-l] [-m 'machines to get mksysb from']
#
# get mksysb from each client machine specified with -m.  If no
# machines specified, get mksysb from ALL machines. -r flag says
# remove oldest existing mksysb for the machines being backed up.
# use -n no_make flag with -r to remove a generation of mksysb,
# without creating a new one.
# Author: Steve Knudsen
# Modified by Bruce Spencer December 2006
#	Added subdirectory for each nim_machine
#	Added help (-h)
#	Added list (-l)
#	Added MAX_BACKUPS (save up to MAX_BACKUPS, then erase oldest) 


###  Your customizations begin here ###

# Parent directory for storing mksysb
# Backups will be stored as $MKSYSB_DIR/nim_machine_name/nim_machine_name_date
MKSYSB_DIR=/export/mksysb   

# Save up to MAX_BACKUPS per server, then remove oldest (space saving)
# The "-r" option will override and remove the oldest.
MAX_BACKUPS=100

###  End customizations ###



# Initialize
remove_old=
machine_list=
no_make=

# Get Command Line Arguments
while getopts hlnrm: option
   do
   case $option in
   h)	echo " " 
	echo "Purpose: automate system backup(s) using NIM"
     	echo " " 
      	echo "Syntax: `basename $0` [-h] [-r] [-n] [-l] [-m 'machines to get mksysb from']"
	echo "\t-h = help"
	echo "\t-r = remove the oldest mksysb image"
	echo "\t-l = list NIM machines and mksysb images"
	echo "\t-n = no backup (used for testing or to remove oldest mksysb)"
	echo "\t-m = NIM machine name to backup.  Default = backup all"
	echo ""
        exit;;

   l)   echo "\n## NIM machines ##"
	lsnim -c machines | awk ' !/^master/ { print $1 } '
	echo "\n## NIM mksysb Resources ##"
	lsnim -t mksysb | awk ' { print $1 }'
	echo " "
	exit;;

   m) machine_list="$OPTARG";;
   n) no_make=1;;
   r) remove_old=1;;
   esac

   done

# if machine_list is null at this point, set it to ALL clients
if [ -z "$machine_list" ]; then 
   machine_list=`lsnim -c machines | awk ' !/master/ { print $1 }'`
fi

# Backup machine(s)
echo "Machine list is $machine_list \n"

for m in $machine_list
   do

   echo "### Creating NIM mksysb Resource for $m ###" 
   date

   if [ ! -d $MKSYSB_DIR/$m -a -z "$no_make" ]; then
      echo "Creating new directory: $MKSYSB_DIR/$m"
      mkdir $MKSYSB_DIR/$m
   fi

   cd $MKSYSB_DIR/$m 2>/dev/null   
      
   n_backups=$(ls $m* |wc -l )
   if [ ! -z "$remove_old" || $n_backups -ge $MAX_BACKUPS ]; then
      oldest=$(ls -lt $m* | tail -1 | awk '{print $9}')
      if [ ! -z $oldest ]; then
        echo Removing oldest file and nim resource: $oldest
        nim -o remove $oldest
        /usr/bin/rm $oldest
      else 
	echo "Can not remove oldest file. No files to remove. $oldest"
      fi
   fi

   #  if no_make is null, go ahead and make the mksysb
   if [ -z "$no_make" ]; then   

      filename="$m"_`date +%Y%m%d%H%M`

      echo New file and nim resource is $filename
      echo Machine to backup is $m

      echo nim -o define -t mksysb -aserver=master -amk_image=yes \
      -alocation=$MKSYSB_DIR/$m/$filename \
      -asource=$m $filename

      time nim -o define -t mksysb -aserver=master -amk_image=yes \
      -alocation=$MKSYSB_DIR/$m/$filename \
      -asource=$m $filename

   else
      echo "Script invoked with no_make option. Backup of $m was not made."
   fi
   echo "-----"

   done




Bruce Spencer,
baspence@us.ibm.com

December 5, 2006