Calling functions

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search
← Writing functionsHomePass arguments into a function →

To call or invoke the function, type the name of the function:

functioName

For example, define and write a function called yday() to display yesterday's date:

yday(){  date --date='1 day ago'; }

To invoke the function yday():

yday

In the end your program should like as follows:

#!/bin/bash
# write the function
yday(){
 date --date='1 day ago'
}
# invoke the function
yday

Putting It All Together

  • Create a shell script called nas_backup.sh.
  • Function such as mount_nas and other get called several times (code reuse).
  • The use functions makes script easy to modify and read.
  • All functions and variables are created at the start of a script.
  • You must declare the variable before any commands attempt to use them.
  • This script also demonstrate the use of here documents, sending an alert email, command substitution, invoke the command via variables, logging a message to a syslog, and much more.
#!/bin/bash
# A shell script to backup MySQL database and directories to a nas server.
# Written by Vivek Gite <vivek@gite.in>
# Last updated on, Feb-2-2007
###############################
#  	     Variables        #
###############################
 
### SETUP BIN PATHS ###
MKDIR=/bin/mkdir
CP=/bin/cp
GTAR=/bin/tar
RSYNC=/usr/bin/rsync
MOUNT=/bin/mount
UMOUNT=/bin/umount
GREP=/bin/grep
AWK=/bin/awk
SED=/bin/sed
CUT=/bin/cut
MYSQL=/usr/bin/mysql
MYSQLADMIN=/usr/bin/mysqladmin
MYSQLDUMP=/usr/bin/mysqldump
GZIP=/bin/gzip
LOGGER=/usr/bin/logger
MAILCMD=/bin/mail
DU=/usr/bin/du
RM=/bin/rm
 
### SETUP NAS LOGIN ###
NASUSER=vivek
NASPASSWORD=MyPassWord
NASSERVER=nas10.nixcraft.net.in
NASMNT=/nas10
 
### ADMIN Notification Email Ids ###
WARN_ADMIN_EMAIL_IDS="user@example.com,user@example.net,vivek@gite.in"
 
### SETUP MYSQL LOGIN/Password ###
MUSER='root'
MPASS='mySqlLoginPassword'
MHOST="127.0.0.1"
 
### SETUP MYSQL BACKUP PATHS ###
MBAKPATH=${NASMNT}/mysql
 
### SETUP TAR BALL BACKUP PATHS ###
TBAKPATH=${NASMNT}/tarballs
 
### Setup file system dirs to backup ###
TAR_SRC_DIRS='/etc /var/named/chroot /root /home /var/www/html /usr/local/mailboxes'
 
### Date format dd-mm-yyyy ###
NOW=$(date +"%d-%m-%Y")
 
### Time format hh_mm_ssAM|PM ###
TIME_FORMAT='%H_%M_%S%P'
 
###############################
#  User Defined Functions    #
###############################
 
#
# Purpose: Send warning email.
#
tar_warn_email(){
        $LOGGER "$(basename $0) GNU/tar: *** Failed at $(date) ***"
        $MAILCMD -s "GNU/TAR Backup Failed" "${WARN_ADMIN_EMAIL_IDS}"<<EOF
GNU/Tar backup failed @ $(date) for $(hostname)
EOF
}
 
#
# Purpose: Backup file system directories.
# 
backup_tar(){
        $LOGGER "$(basename $0) GNU/tar: Started at $(date)"
        # call function to mount nas device 
        mount_nas
        [ ! -d ${TBAKPATH}/$NOW/ ] && $MKDIR -p ${TBAKPATH}/$NOW/
        local path="${TBAKPATH}/$NOW/fs-$(date +"${TIME_FORMAT}").tar.gz"
        $GTAR --exclude "*/proc/*" --exclude "*/dev/*" --exclude '*/cache/*' -zcvf $path $TAR_SRC_DIRS
        [ $? -ne 0 ] && tar_warn_email
        # call function to unmount nas device
        umount_nas
        $LOGGER "$(basename $0) GNU/tar: Ended at $(date)"
}
#
# Purpose: Mount backup nas device.
# 
mount_nas(){
        [ ! -d $NASMNT ] && $MKDIR -p $NASMNT
        $MOUNT | $GREP $NASMNT >/dev/null
        [ $? -eq 0 ] || $MOUNT -t cifs //$NASSERVER/$NASUSER -o username=$NASUSER,password=$NASPASSWORD $NASMNT
}
#
# Purpose: Unmount backup nas device.
# 
umount_nas(){
        $MOUNT | $GREP $NASMNT >/dev/null
        [ $? -eq 0 ] && $UMOUNT $NASMNT 
}
#
# Purpose: Backup mysql database.
# 
mysql(){
        $LOGGER "$(basename $0) mysql: Started at $(date)"
        local DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
        local db=""
        local linkname=""
        [ ! -d $MBAKPATH/$NOW ] && $MKDIR -p $MBAKPATH/$NOW
        for db in $DBS
        do
                [ "$db" == "sgopenxadserver"  ] && continue 
                local tTime=$(date +"${TIME_FORMAT}")
                local FILE="${MBAKPATH}/$NOW/${db}.${tTime}.gz"
                $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
                #create latest file link
                linkname="${MBAKPATH}/$NOW/${db}.latest"
                [ -L $linkname ] && /bin/rm $linkname
                /bin/ln -s $FILE $linkname
        done
        $LOGGER "$(basename $0) mysql: Ended at $(date)"
}
 
#
# Purpose: Wrapper function to call other functions.
#
backup_mysql(){
        mount_nas         # call function to mount nas device
        mysql
        umount_nas        # call function to unmount nas device
}
 
###################################
# Main Script Logic Starts Here   #
###################################
case "$1" in
        mysql)
                backup_mysql
                ;;
        fsbak)
                backup_tar
                ;;
        mount)
                mount_nas
                ;;
        umount)
                umount_nas
                ;;
        *)
                echo "Usage: $0 {mysql|fsbak|mount|umount}"
                echo ""
                echo "Use this shell script to backup mysql database and directories to backup nas server."
esac

You can run this script as follows to mak e a mysql database backup:

./nas_backup.sh mysql

To make a file system backup, enter:

./nas_backup.sh fsbak
← Writing functionsHomePass arguments into a function →