Linux Shell Script example1 (NFS Backup)


#!/bin/sh
#
# expected image location is tool1.hkg.yahoo.com:/ad2/image/
#
nfs_server=tool1.hkg.yahoo.com
nfs_mount=ad2
image_dir=image
tmp_mnt=/mnt
bin_dir=/etc/bin
# verbose=1

# -----------------------> Do not modify after this line <---------------------
command=${0##*/}
usage () {
  echo "usage: $command [-fp] [-l local-disk-id] [-n nfs_server:nfs_mount-pt]"
  echo ""
  echo " -f  full backup                                       "
  echo " -p  partial backup, exclude /etc/bin/notar-xxx file   "
  echo " -v  be verbose                                        "
  echo " -n  nfs_server and nfs_mount-point                    "
  echo " -l  local image mount-point                           "
  echo " -h  this Help page                                    "
}

nfs_opt="${nfs_server}:/${nfs_mount}"
try_nfs=2
full_backup=2
while getopts "l:n:vpfh?" opt; do
  case "$opt" in
    l)
      try_nfs=0
      tmp_mnt=/$OPTARG
      ;;
    n)
      try_nfs=1
      nfs_opt=$OPTARG
      ;;
    v)  
      verbose=1
      set -xv
      ;;
    f)
      full_backup=1
      ;;
    p)
      full_backup=0
      ;;       
    h)
      usage
      exit
      ;;
    \?)
      usage
      exit
      ;;
  esac
done
if [ $try_nfs = 2 -o $full_backup = 2 ]; then
  echo " Error: Parameter not enough!!"
  exit
fi

##########################################################
###
###  Skip NFS Mount if doing local harddisk backup
###
##########################################################
if [ $try_nfs = 1 ]; then

### Check whether NFS Client Daemon up or bring it up
# if ps ax | grep nfsiod > /dev/null 2>&1; then
#    :
# else
#    nfsiod -n 2
# fi

### Check whether local mount point is occupied
if df | grep $tmp_mnt > /dev/null 2>&1; then
  if df | grep $nfs_opt > /dev/null 2>&1; then
### Mounted already
    :
  else
   echo "$tmp_mnt directory in use! Program exit!!"
   exit
  fi
else
### Mount NFS directory
  mount_nfs -R 1 $nfs_opt $tmp_mnt
  if df | grep $nfs_opt > /dev/null 2>&1; then
    :
  else
   echo "Cannot nfs mount $nfs_opt ! Program exit!!"  
   exit   
  fi
fi

fi
##########################################################
###
###  Skip NFS Mount if doing local harddisk backup
###
##########################################################

### Sync / Copy $bin directory, for the scripts and exclude file
if [ -d "$bin_dir" ]; then
    drop_dir="${tmp_mnt}/${image_dir}/bin/*"
    cp ${drop_dir} ${bin_dir}  > /dev/null 2>&1
fi

### Check dump 'image' directory, if not exist create it
drop_dir="${tmp_mnt}/${image_dir}"
if [ ! -d "$drop_dir" ]; then
    mkdir $drop_dir 2> /dev/null
fi

### Check dump file directory, if not exist create it
machinename=`uname -n`
if [ $full_backup = 1 ]; then
    drop_dir="${tmp_mnt}/${image_dir}/$machinename"
else 
    drop_dir="${tmp_mnt}/${image_dir}/Daily-$machinename"
fi
if [ ! -d "$drop_dir" ]; then
    mkdir $drop_dir 2> /dev/null
fi

### Backup existing dump file if both files are good.
# if [ -f "${drop_dir}/root.tgz" -a -f "${drop_dir}/home.tgz" ]; then
#     if [ -s "${drop_dir}/root.tgz" -a -s "${drop_dir}/home.tgz" ]; then
#             rm -f "${drop_dir}/root.tgz.*"
#             rm -f "${drop_dir}/home.tgz.*"
#             mv "${drop_dir}/root.tgz" "${drop_dir}/root.tgz.1"
#             mv "${drop_dir}/home.tgz" "${drop_dir}/home.tgz.1"
#     else 
#             rm -f "${drop_dir}/root.tgz"
#             rm -f "${drop_dir}/home.tgz"
#     fi
# else
#        rm -f "${drop_dir}/root.tgz"
#        rm -f "${drop_dir}/home.tgz"
# fi
#
# Not space to store .1 image. Give up
rm -f "${drop_dir}/root.tgz"
rm -f "${drop_dir}/home.tgz"

### Lookup the Exclusion pattern file in image directory ####
exclude=""
if [ $full_backup = 0 ]; then
   exclude_file="${bin_dir}/notar-$machinename"
   if [ -f "${exclude_file}" -a -s "${exclude_file}" ]; then
       exclude=" -X $exclude_file"
   fi
fi

### Create the dump tarball
#
echo "## $machinename : Root-Dump Starts ##" >> ${tmp_mnt}/${image_dir}/daily.log
date >> ${tmp_mnt}/${image_dir}/daily.log
cd /
tar -zcl --totals ${exclude} -f ${drop_dir}/root.tgz . >>${tmp_mnt}/${image_dir}/daily.log 2>&1
#
echo "## $machinename : Home-Dump Starts ##" >> ${tmp_mnt}/${image_dir}/daily.log
date >> ${tmp_mnt}/${image_dir}/daily.log
cd /
tar -zcl --totals ${exclude} -f ${drop_dir}/home.tgz home/* >>${tmp_mnt}/${image_dir}/daily.log 2>&1
#
#if [ -d /ad2 ]; then
#  echo "## $machinename : /ad2-Dump Starts ##" >> ${tmp_mnt}/${image_dir}/daily.log
#  date >> ${tmp_mnt}/${image_dir}/daily.log
#  cd /
#  tar -zcl --totals --exclude *.tgz* -f ${drop_dir}/ad2.tgz ad2/* >>${tmp_mnt}/${image_dir}/daily.log 2>&1
#fi
#
echo "## $machinename : Dump Done   #######" >> ${tmp_mnt}/${image_dir}/daily.log
date >> ${tmp_mnt}/${image_dir}/daily.log
echo ""

##########################################################
###
###  Skip NFS Un-Mount if doing local harddisk backup
###
##########################################################
if [ $try_nfs = 1 ]; then

### Umount the nfs & Shutdown NFS Client Daemon 
 umount $tmp_mnt
 #
# a=`${bin_dir}/rkill -q nfsiod`
# retval=$?
# if [ "$retval" = 1 ]; then
#      echo "Cannot shutdown the NFS_Client daemon?????"  
# fi

fi
##########################################################
###
###  Skip NFS Un-Mount if doing local harddisk backup
###
##########################################################

Labels: cheatsheet, linux.