LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   More bash help. (https://www.linuxquestions.org/questions/linux-newbie-8/more-bash-help-939071/)

lleb 04-10-2012 07:38 AM

More bash help.
 
Sorry to keep bugging you guys about this stuff. I now have a backup script that works for everything except one small part. This backup script can rsync and create a tarball to push out to a mount point with zero issues, but when i try to push the tarball via ftp, it craps out on me. I can run the command manually with no issues so it is not the command. I must be missing something in the order of the script that I just can not see. an extra group of eye might just see it right off the bat.

Code:

#!/bin/bash
#
###########################################################
# Created by Ray Brunkow April 8, 2012
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 or version 3 of the
# license, at your option.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##########################################################
# Change the NAS device's IP here
# Put the NAS device's IP in the /etc/hosts table and call it
# ftp.rx30backup.com so it should look like the following
# 192.168.4.200        ftp.rx30backup.com
############################################################

url=ftp.rx30123.com
license=`showlic | grep LICENSE | awk '{print $6}'`                               
DATE=`date +%Y-%b-%d-%a-%H.%M`
dow=`date +%a`
rx30sessions=0
csrunning=0
HOMEDIR="$HOME"
LOG=${HOMEDIR}/logs/${DATE}-Backup.log                         
username=`whoami`
counter=0
LDIR=`pwd`
echo "hdbackd logs for ${license}" >> ${LOG}
MOUNTDIR="/mnt/backup"
TARGET=/dev/sdb1                ### Change as required
tarfile="rx${license}-${DATE}"
FILENUM=`find $HOMEDIR | wc -l`
enddate1="`date +%Y-%b-%d-%a-%H.%M.%S`"

# Change the NAS device's directory to where you want the files sent.
# Change the username and password for the NAS device.
############################################################
#backdir=array1/share/rx30backup
backdir=junk
#username=junk
#password=junk
username=junk
password=junk

### This script is to be called with the following options:
# t for tar compressed and verified file to be pushed to any device.
# f for ftp will also use the tar function to generate the file.
# r for rsync, this will not use the tar compression.
# n for no kill mode.  this will not terminate any interfaces or rx30 screens.
# x this will be used for debug mode to troubleshoot the script to see
#  were it might be breaking.
# You may call this script with a combination of options.  Example:  back f n x will call the FTP
# backup, typically to a NAS device, it will NOT close any screens or interfaces, and will fully
# display the rest of the script as it makes calls to functions, creates the tarball,
# and connects to the NAS device for lftp transmission.

### Options
######################################

while getopts ":tfrnx" opt
do
        case $opt in
                f )        DO_FTP=true
                        DO_RSYNC=false
                        DO_TAR=true
                        ;;

                r )        DO_FTP=false
                        DO_RSYNC=true
                        DO_TAR=false
                        ;;

                t )        DO_FTP=false
                        DO_RSYNC=false
                        DO_TAR=true
                        ;;

                n )        NO_KILL=true
                        ;;

                x )        set -xv
                        ;;
        esac
done

shift $(( $OPTIND -1 ))

### FUNCTIONS
######################################

### lftp function
#####################################

lftp()

{
        ${DO_FTP} || return

        lftp -e "set ftp:passive-mode on && cd ${backdir} && put ${NEWTAR} && bye" -u ${username},${password} ${url}
        echo "FTP1 transmission complete to remote computer at ${url}"  >> ${LOG}
        return

}

### rsync function
#####################################

RSYNC()

{
        ${DO_RSYNC} || return

        mkdir -p /mnt/backup/${dow}
                rsync -aviS /usr/rx30/ /mnt/backup/${dow} >> ${LOG} 2>&1
}

### function to create the tar file
#####################################

TAR()

{
        ${DO_TAR} || return

        mkdir -p /mnt/backup/${dow}
                tar cvjf /tmp/${tarfile}.tar.bz2 ${HOMEDIR}  >> ${LOG}
calcBakSpace
checkTar

}

### Function to check if system can mount backup drive
#####################################

MountDrive ()

{

mount ${MOUNTDIR} >> ${LOG}
rc=$?
if [ $rc -ne 0 ]
then
        echo "[Backup] unable to mount (rx=${rc}) backup drive." >> ${LOG}
        exit 2
fi

AVAILBACKSIZE=`df -hk | grep /mnt/usb | awk -F" " '{print $4}'` ## available space on device
MAXBACKSIZE=`df -hk | grep ${MOUNTDIR} | awk -F" " '{print $2}'`  ## maximum post format size
HOMEDIRSIZE=`du -s ${HOMEDIR} | awk -F" " '{print $1}'`          ## size of rx30 dir

}

### Function to verify the tarball will fit on the backup device.
##################################

calcBakSpace()

{

MAXBACKSIZE=`df -hk | grep ${MOUNTDIR} | awk -F" " '{print $2}'`  ## maximum post format size
AVAILBACKSIZE=`df -hk | grep ${MOUNTDIR} | awk -F" " '{print $4}'` ## available space on device
BAKFILESIZE=`du -hk /tmp/${tarfile}.tar.bz2 | awk -F" " '{print $1}'`  ## compressed backup file size
OLDFILE=`ls -ltr ${MOUNTDIR} | grep -v total | grep -v lost+found | head -1 | awk -F" " '{print $9}'` ## oldest file on usb stick

MAXBACKSIZEH=`df -h | grep ${MOUNTDIR} | awk -F" " '{print $2}'`  ## more human readable format
AVAILBACKSIZEH=`df -h | grep ${MOUNTDIR} | awk -F" " '{print $4}'`
BAKFILESIZEH=`du -hs /tmp/${tarfile}.tar.bz2 | awk -F" " '{print $1}'`

## checks if tarball too large for backup device.

if [ ${BAKFILESIZE} -gt ${MAXBACKSIZE} ]
then
        echo "Total Space Available on USB ${MAXBAKSIZEH} Space Needed ${BAKFILESIZEH}" >> ${LOG}
        echo "Larger backup device needed" >> ${LOG}
        exit 1
        else

## checking if enough room on stick for backup file
## very small chance it could be wrong due to check being performed before encryption adding small increase to file size

                if [ ${BAKFILESIZE} -lt ${AVAILBACKSIZE} ]
                then
                echo "${BAKFILESIZEH} needed ${AVAILBACKSIZEH} available. Backup can continue" | tee -a ${LOG}
                sleep 1
                echo "Space Available ${AVAILBACKSIZEH} needed ${BAKFILESIZEH}. Removing ${OLDFILE}." | tee -a ${LOG}
                sleep 1
                rm ${MOUNTDIR}/${OLDFILE}
                fi
fi

}

### Check and verify tarball
#####################################

checkTar ()

{

tar tfj /tmp/${tarfile}.tar.bz2 &> /tmp/tar2; bzerr="$?"; echo $bzerr > /tmp/bz-check.list

echo "Verifying compressed data." | tee -a ${LOG}
VERSTAT=`cat /tmp/bz-check.list`

if [ ${VERSTAT} -eq 0 ]
then
        echo "Verification of compressed data was successfull." | tee -a ${LOG}
else
        echo "Compressed data appears to be corrupt.  Error ${VERSTAT}." | tee -a ${LOG}
        exit 1
fi

    echo "Encrypting ${tarfile}.tar.bz2" | tee -a ${LOG}
    openssl des3 -a -salt -in /tmp/${tarfile}.tar.bz2 -out /tmp/${tarfile}.tar.bz2.enc -pass pass:tdsrx30
    echo "${tarfile}.tar.bz2.enc Encrypted" | tee -a ${LOG}
        NEWTAR=/tmp/${tarfile}.tar.bz2.enc

# to decrypt: openssl des3 -a -d -salt -in 2011-12-20.tar.bz2.enc -out 2011-12-20.tar.bz2 -pass pass:tdsrx30

}

### Function to copy the tarball to the backup directory and verify
#####################################

copyFiles ()

{
        ${DO_TAR} || return

echo "Copying Files to backup device ${TARGET}." | tee -a ${LOG}
    \cp -a /tmp/${tarfile}.tar.bz2.enc ${MOUNTDIR}
    BAKFILESIZEENC=`du -hk /tmp/${tarfile}.tar.bz2.enc | awk -F" " '{print $1}'`
    REMBAKFILE=`du -hk ${MOUNTDIR}/${tarfile}.tar.bz2.enc | awk -F" " '{print $1}'`
        if [ ${REMBAKFILE} -eq ${BAKFILESIZEENC} ]
        then
                echo "Copying Backup file to backup device complete." | tee -a ${LOG}
        else
                echo "Copying Backup file to backup device failed.  Contact TDS." | tee -a ${LOG}
                kdialog --display=:0 --error "Copying Backup file to backup device failed.  Contact TDS."
                exit 1
        fi

}

### Function to perform final checksum between local and remote file.
#####################################

chkSum ()

{
        ${DO_TAR} || return

echo "Performing checksum on copied file" | tee -a ${LOG}
        md5sum /tmp/${tarfile}.tar.bz2.enc >/tmp/usbcksums_local
        md5sum ${MOUNTDIR}/${tarfile}.tar.bz2.enc >/tmp/usbcksums_remote
        LOCALSUM=`cat /tmp/usbcksums_local | awk -F" " '{print $1}'`
        REMOTESUM=`cat /tmp/usbcksums_remote | awk -F" " '{print $1}'`
echo "Checksum Results" >>${LOG}
echo "${LOCALSUM}  ${REMOTESUM}" >>${LOG}

if [ ${LOCALSUM} = ${REMOTESUM} ]
then
        echo "Verification was successfull ${MOUNTDIR}/${tarfile}.tar.bz2.enc and /tmp/${tarfile}.tar.bz2.enc are identical." | tee -a ${LOG}
else
        echo "There is a difference between the Backup File on the hard drive and backup device." | tee -a ${LOG}
        echo "Backup Complete" | tee -a ${LOG}
fi

}

### Function for terminating interfaces
#####################################

stopinter ()

{

        ${NO_KIL}L && return

        INTERFACES=(rx30faxs rx30poss rx30pkgs rx30drxs rx30rds lifeline rx30ntfs)
        echo "Stopping additional interfaces."  >> ${LOG}

for f in "${INTERFACES[@]}"
do
killall $f &>/dev/null
done
echo "Interfaces stopped." >> ${LOG}

}

## killing rx30 procs
########################################

killProcs ()

{

        ${NO_KILL} && return

>/tmp/proclist
for i in `ps -e | grep rx30 | grep -v grep | awk -F" " '{print $4}'`
do
    if [ $i != rx30.exe ]
    then
        echo $i >>/tmp/proclist
    fi
    killall $i
done
echo "Rx30 screens stopped." >> ${LOG}

}

### starting rx30 background and autostart
########################################

startProcs ()

{
        ${NO_KILL} && return

YOURTERM=$TERM
export TERM=scoansi
rx30 bg &
. rx30 autostart 2>/dev/null
for i in `cat /tmp/proclist`
do
    $i &
done
TERM=$YOURTERM
echo "rx30 bg &, . rx30 autostart are running." >> ${LOG}

}

### Restarting most interfaces less CSS, rx30.exe in BG mode, and autostart.
########################################

restart ()

{
        ${NO_KILL} && return

        LIST=(lifeline rx30drxs rx30rds rx30faxs rx30pkgs rx30poss rx30ntfs)
        echo "Restarting interfaces." >> ${LOG}

for f in "${LIST[@]}"
do
$HOME/$f & &>/dev/null
done
echo "Interfaces restarted." >> ${LOG}

}

### Function to restart CSS
######################################

restartcss ()

{
        ${NO_KILL} && return

echo "Restarting rx30css." >> ${LOG}

$HOME/rx30css &
if [ $? -gt 0 ];
then
        echo "Problem starting Central Site interface or NON Central Site license." >> ${LOG}
else
        echo "Restarted rx30css ${enddate1}." >> ${LOG}
fi

}

### Run the first time script is run to add sudo permissions to visudo.
######################################

firstTime ()

{

cat <<EOF
#!/bin/bash
    if grep formatDev.sh /etc/sudoers
    then
        echo "sudo entries exist" | tee -a ${LOG}
    else
        echo "Adding sudo entries" | tee -a ${LOG}
        sleep 1
        sed "/${username}/ s/$/, \/bin\/mount \/mnt\/backup, \/bin\/umount \/mnt\/backup, \/bin\/chmod, \/bin\/chown, \/bin\/cp, \/tmp\/formatDev.sh/" /etc/sudoers > /etc/sudoers.spk
        cp /etc/sudoers /etc/sudoers.`date +%F`; mv /etc/sudoers.spk /etc/sudoers
        chmod 440 /etc/sudoers
        echo "sudo add complete"
        sleep 2
    fi
EOF

}

### Function to copy system files to $HOME and set proper ownership and permissions.
######################################

copysys ()

{
        SYSFILES="/etc/hosts /etc/exports /etc/dhcpd.conf /etc/resolv.conf /etc/cups/printers.conf /etc/sysconfig/static-routes /etc/sysconfig/network-scripts-ifcfg-eth* /opt/ltsp/i386/etc/lts.conf /etc/fstab"
        echo "Copy system files to user directory." >> ${LOG}

for f in "${SYSFILES[@]}"
do
cp $f $HOME &>/dev/null
done
chown ${username}:group ${HOMEDIR}/
chmod -R 755 ${HOMEDIR}/

}

### Make sure Central Site is not running
##########################################

KILLCSS ()

{
        ${NO_KILL} && return

if [ -f $HOME/rx30css ];
  then
  counter=0
  while [ $csrunning -gt 0 ];
  do
    echo "Stopping Central Site Interface" >> ${LOG}
    killall rx30css
    sleep 5
    csrunning=`ps -ef | grep rx30css | grep ^${username} | grep -v grep | wc -l`
    counter=$(($counter+1))
    if [ $counter -gt 5 ];
    then
        echo "Unable to shutdown Central Site Interface" >> ${LOG}
        exit
    fi
  done
fi

}

#####################################################################################################
# Uncomment the following command if you are getting errors like the following:              #######
# usb 4-4: device not accepting address 21, error -71                                        #######
#####################################################################################################
#
# sudo /sbin/rmmod ehci_hcd    # You will need to put "/sbin/rmmod ehci_hcd" into visudo for the user


#####################################################################################################
# Start the LOG #####################################################################################
#####################################################################################################
if [ ! -d ${HOMEDIR}/logs ]
  then
  echo "Logfile Doesn't Exist!!!" >> ${LOG}
  echo "Creating Directory" >> ${LOG}
  mkdir ${HOMEDIR}/logs
fi
#####################################################################################################

echo "Backup script started." >> ${LOG}

### Checking to see if first time to run backup script.
########################################

if [ ! -f ${HOMEDIR}/.backup ]
then
    firstTime >/tmp/firstTime.sh
    chmod +x /tmp/firstTime.sh
    sleep 1
        echo -e "Adding visudo rules during first time setup.  " > ${HOMEDIR}/.backup
        echo -e "Enter the root password \(generally \"rx30\"\) at prompt\r"
        su - -c  /tmp/firstTime.sh   
fi

#####################################################################################################

### Call function to kill all rx30 screens
######################################
### Not used in NO_KILL mode

killProcs

### Call function to stop Central Site
######################################
### Not used in NO_KILL mode

KILLCSS

### Call function to terminate interfaces
######################################
### Not used in NO_KILL mode

stopinter

### Call function to copy system files to /usr/rx30 directory
######################################

copysys

umount /mnt/backup        ## removing stale or duplicate mounts

MountDrive
TAR
RSYNC
lftp
copyFiles
chkSum

startProcs
restart
restartcss

### cleaning up tmp directory
######################################
echo "removing /tmp/${tarfile}.tar.bz2" | tee -a ${LOG}
rm /tmp/${tarfile}.tar.bz2
echo "removing /tmp/${tarfile}.tar.bz2.enc" | tee -a ${LOG}
rm /tmp/${tarfile}.tar.bz2.enc
umount /mnt/backup
rc=$?
if [ $rc -ne 0 ]
then
        sleep 2
        umount /mnt/backup
        rc2=$?
        if [ $rc2 -ne 0 ]
        then
                echo "Unable to unmount the backup device.  Please contact TDS." >> ${LOG}
                exit 2
        else
                echo "Unmounted backup device successfully." >> ${LOG}
        fi
fi
echo "Backup drive unmounted." >> ${LOG}
kdialog --msgbox "Backup Complete! \n If you are using a thumbdrive it is now safe to unplug it."

### Notes for e-mail
### yum -y install postfix
### cd /var/spool/postfix
### mkfifo pickup
### postsuper -d ALL        ## This is to clear the printer queue a MUST on FC5/7 systems.
### service postfix restart
### chkconfig postfix on        ###this will set postfix to start on bootup###
### uncomment the following line to send e-mail everytime the script runs
# mailx -s "backup logs ${license}" user@123.net user2@123.net < ${LOG}
# echo "mailx -s "backup logs ${license}"  user@123.net user2@123.net < ${LOG}" >> ${LOG}

exit;


lleb 04-10-2012 07:40 AM

below is the -xv output



the extra return in the lftp function was just added in hopes of breaking the cycle it creates:

Code:

[rx30@rx30 ~]$ back.sh -f -x
./back.sh: line 28: showlic: command not found
+ getopts :tfrnx opt

shift $(( $OPTIND -1 ))
+ shift 2

### FUNCTIONS
######################################

### lftp function
#####################################

lftp()

{
        ${DO_FTP} || return

        lftp -e "set ftp:passive-mode on && cd ${backdir} && put ${NEWTAR} && bye" -u ${username},${password} ${url}
        echo "FTP1 transmission complete to remote computer at ${url}"  >> ${LOG}
        return

}

### rsync function
#####################################

RSYNC()

{
        ${DO_RSYNC} || return

        mkdir -p /mnt/backup/${dow}
                rsync -aviS /usr/rx30/ /mnt/backup/${dow} >> ${LOG} 2>&1
}

### function to create the tar file
#####################################

TAR()

{
        ${DO_TAR} || return

        mkdir -p /mnt/backup/${dow}
                tar cvjf /tmp/${tarfile}.tar.bz2 ${HOMEDIR}  >> ${LOG}
calcBakSpace
checkTar

}

### Function to check if system can mount backup drive
#####################################

MountDrive ()

{

mount ${MOUNTDIR} >> ${LOG}
rc=$?
if [ $rc -ne 0 ]
then
        echo "[Backup] unable to mount (rx=${rc}) backup drive." >> ${LOG}
        exit 2
fi

AVAILBACKSIZE=`df -hk | grep /mnt/usb | awk -F" " '{print $4}'` ## available space on device
MAXBACKSIZE=`df -hk | grep ${MOUNTDIR} | awk -F" " '{print $2}'`  ## maximum post format size
HOMEDIRSIZE=`du -s ${HOMEDIR} | awk -F" " '{print $1}'`          ## size of rx30 dir

}

### Function to verify the tarball will fit on the backup device.
##################################

calcBakSpace()

{

MAXBACKSIZE=`df -hk | grep ${MOUNTDIR} | awk -F" " '{print $2}'`  ## maximum post format size
AVAILBACKSIZE=`df -hk | grep ${MOUNTDIR} | awk -F" " '{print $4}'` ## available space on device
BAKFILESIZE=`du -hk /tmp/${tarfile}.tar.bz2 | awk -F" " '{print $1}'`  ## compressed backup file size
OLDFILE=`ls -ltr ${MOUNTDIR} | grep -v total | grep -v lost+found | head -1 | awk -F" " '{print $9}'` ## oldest file on usb stick

MAXBACKSIZEH=`df -h | grep ${MOUNTDIR} | awk -F" " '{print $2}'`  ## more human readable format
AVAILBACKSIZEH=`df -h | grep ${MOUNTDIR} | awk -F" " '{print $4}'`
BAKFILESIZEH=`du -hs /tmp/${tarfile}.tar.bz2 | awk -F" " '{print $1}'`

## checks if tarball too large for backup device.

if [ ${BAKFILESIZE} -gt ${MAXBACKSIZE} ]
then
        echo "Total Space Available on USB ${MAXBAKSIZEH} Space Needed ${BAKFILESIZEH}" >> ${LOG}
        echo "Larger backup device needed" >> ${LOG}
        exit 1
        else

## checking if enough room on stick for backup file
## very small chance it could be wrong due to check being performed before encryption adding small increase to file size

                if [ ${BAKFILESIZE} -lt ${AVAILBACKSIZE} ]
                then
                echo "${BAKFILESIZEH} needed ${AVAILBACKSIZEH} available. Backup can continue" | tee -a ${LOG}
                sleep 1
                echo "Space Available ${AVAILBACKSIZEH} needed ${BAKFILESIZEH}. Removing ${OLDFILE}." | tee -a ${LOG}
                sleep 1
                rm ${MOUNTDIR}/${OLDFILE}
                fi
fi

}

### Check and verify tarball
#####################################

checkTar ()

{

tar tfj /tmp/${tarfile}.tar.bz2 &> /tmp/tar2; bzerr="$?"; echo $bzerr > /tmp/bz-check.list

echo "Verifying compressed data." | tee -a ${LOG}
VERSTAT=`cat /tmp/bz-check.list`

if [ ${VERSTAT} -eq 0 ]
then
        echo "Verification of compressed data was successfull." | tee -a ${LOG}
else
        echo "Compressed data appears to be corrupt.  Error ${VERSTAT}." | tee -a ${LOG}
        exit 1
fi

    echo "Encrypting ${tarfile}.tar.bz2" | tee -a ${LOG}
    openssl des3 -a -salt -in /tmp/${tarfile}.tar.bz2 -out /tmp/${tarfile}.tar.bz2.enc -pass pass:tdsrx30
    echo "${tarfile}.tar.bz2.enc Encrypted" | tee -a ${LOG}
        NEWTAR=/tmp/${tarfile}.tar.bz2.enc

# to decrypt: openssl des3 -a -d -salt -in 2011-12-20.tar.bz2.enc -out 2011-12-20.tar.bz2 -pass pass:tdsrx30

}

### Function to copy the tarball to the backup directory and verify
#####################################

copyFiles ()

{
        ${DO_TAR} || return

echo "Copying Files to backup device ${TARGET}." | tee -a ${LOG}
    \cp -a /tmp/${tarfile}.tar.bz2.enc ${MOUNTDIR}
    BAKFILESIZEENC=`du -hk /tmp/${tarfile}.tar.bz2.enc | awk -F" " '{print $1}'`
    REMBAKFILE=`du -hk ${MOUNTDIR}/${tarfile}.tar.bz2.enc | awk -F" " '{print $1}'`
        if [ ${REMBAKFILE} -eq ${BAKFILESIZEENC} ]
        then
                echo "Copying Backup file to backup device complete." | tee -a ${LOG}
        else
                echo "Copying Backup file to backup device failed.  Contact TDS." | tee -a ${LOG}
                kdialog --display=:0 --error "Copying Backup file to backup device failed.  Contact TDS."
                exit 1
        fi

}

### Function to perform final checksum between local and remote file.
#####################################

chkSum ()

{
        ${DO_TAR} || return

echo "Performing checksum on copied file" | tee -a ${LOG}
        md5sum /tmp/${tarfile}.tar.bz2.enc >/tmp/usbcksums_local
        md5sum ${MOUNTDIR}/${tarfile}.tar.bz2.enc >/tmp/usbcksums_remote
        LOCALSUM=`cat /tmp/usbcksums_local | awk -F" " '{print $1}'`
        REMOTESUM=`cat /tmp/usbcksums_remote | awk -F" " '{print $1}'`
echo "Checksum Results" >>${LOG}
echo "${LOCALSUM}  ${REMOTESUM}" >>${LOG}

if [ ${LOCALSUM} = ${REMOTESUM} ]
then
        echo "Verification was successfull ${MOUNTDIR}/${tarfile}.tar.bz2.enc and /tmp/${tarfile}.tar.bz2.enc are identical." | tee -a ${LOG}
else
        echo "There is a difference between the Backup File on the hard drive and backup device." | tee -a ${LOG}
        echo "Backup Complete" | tee -a ${LOG}
fi

}

### Function for terminating interfaces
#####################################

stopinter ()

{

        ${NO_KIL}L && return

        INTERFACES=(rx30faxs rx30poss rx30pkgs rx30drxs rx30rds lifeline rx30ntfs)
        echo "Stopping additional interfaces."  >> ${LOG}

for f in "${INTERFACES[@]}"
do
killall $f &>/dev/null
done
echo "Interfaces stopped." >> ${LOG}

}

## killing rx30 procs
########################################

killProcs ()

{

        ${NO_KILL} && return

>/tmp/proclist
for i in `ps -e | grep rx30 | grep -v grep | awk -F" " '{print $4}'`
do
    if [ $i != rx30.exe ]
    then
        echo $i >>/tmp/proclist
    fi
    killall $i
done
echo "Rx30 screens stopped." >> ${LOG}

}

### starting rx30 background and autostart
########################################

startProcs ()

{
        ${NO_KILL} && return

YOURTERM=$TERM
export TERM=scoansi
rx30 bg &
. rx30 autostart 2>/dev/null
for i in `cat /tmp/proclist`
do
    $i &
done
TERM=$YOURTERM
echo "rx30 bg &, . rx30 autostart are running." >> ${LOG}

}

### Restarting most interfaces less CSS, rx30.exe in BG mode, and autostart.
########################################

restart ()

{
        ${NO_KILL} && return

        LIST=(lifeline rx30drxs rx30rds rx30faxs rx30pkgs rx30poss rx30ntfs)
        echo "Restarting interfaces." >> ${LOG}

for f in "${LIST[@]}"
do
$HOME/$f & &>/dev/null
done
echo "Interfaces restarted." >> ${LOG}

}

### Function to restart CSS
######################################

restartcss ()

{
        ${NO_KILL} && return

echo "Restarting rx30css." >> ${LOG}

$HOME/rx30css &
if [ $? -gt 0 ];
then
        echo "Problem starting Central Site interface or NON Central Site license." >> ${LOG}
else
        echo "Restarted rx30css ${enddate1}." >> ${LOG}
fi

}

### Run the first time script is run to add sudo permissions to visudo.
######################################

firstTime ()

{

cat <<EOF
#!/bin/bash
    if grep formatDev.sh /etc/sudoers
    then
        echo "sudo entries exist" | tee -a ${LOG}
    else
        echo "Adding sudo entries" | tee -a ${LOG}
        sleep 1
        sed "/${username}/ s/$/, \/bin\/mount \/mnt\/backup, \/bin\/umount \/mnt\/backup, \/bin\/chmod, \/bin\/chown, \/bin\/cp, \/tmp\/formatDev.sh/" /etc/sudoers > /etc/sudoers.spk
        cp /etc/sudoers /etc/sudoers.`date +%F`; mv /etc/sudoers.spk /etc/sudoers
        chmod 440 /etc/sudoers
        echo "sudo add complete"
        sleep 2
    fi
EOF

}

### Function to copy system files to $HOME and set proper ownership and permissions.
######################################

copysys ()

{
        SYSFILES="/etc/hosts /etc/exports /etc/dhcpd.conf /etc/resolv.conf /etc/cups/printers.conf /etc/sysconfig/static-routes /etc/sysconfig/network-scripts-ifcfg-eth* /opt/ltsp/i386/etc/lts.conf /etc/fstab"
        echo "Copy system files to user directory." >> ${LOG}

for f in "${SYSFILES[@]}"
do
cp $f $HOME &>/dev/null
done
chown ${username}:group ${HOMEDIR}/
chmod -R 755 ${HOMEDIR}/

}

### Make sure Central Site is not running
##########################################

KILLCSS ()

{
        ${NO_KILL} && return

if [ -f $HOME/rx30css ];
  then
  counter=0
  while [ $csrunning -gt 0 ];
  do
    echo "Stopping Central Site Interface" >> ${LOG}
    killall rx30css
    sleep 5
    csrunning=`ps -ef | grep rx30css | grep ^${username} | grep -v grep | wc -l`
    counter=$(($counter+1))
    if [ $counter -gt 5 ];
    then
        echo "Unable to shutdown Central Site Interface" >> ${LOG}
        exit
    fi
  done
fi

}

#####################################################################################################
# Uncomment the following command if you are getting errors like the following:              #######
# usb 4-4: device not accepting address 21, error -71                                        #######
#####################################################################################################
#
# sudo /sbin/rmmod ehci_hcd    # You will need to put "/sbin/rmmod ehci_hcd" into visudo for the user


#####################################################################################################
# Start the LOG #####################################################################################
#####################################################################################################
if [ ! -d ${HOMEDIR}/logs ]
  then
  echo "Logfile Doesn't Exist!!!" >> ${LOG}
  echo "Creating Directory" >> ${LOG}
  mkdir ${HOMEDIR}/logs
fi
+ '[' '!' -d /usr/rx30/logs ']'
#####################################################################################################

echo "Backup script started." >> ${LOG}
+ echo 'Backup script started.'

### Checking to see if first time to run backup script.
########################################

if [ ! -f ${HOMEDIR}/.backup ]
then
    firstTime >/tmp/firstTime.sh
    chmod +x /tmp/firstTime.sh
    sleep 1
        echo -e "Adding visudo rules during first time setup.  " > ${HOMEDIR}/.backup
        echo -e "Enter the root password \(generally \"rx30\"\) at prompt\r"
        su - -c  /tmp/firstTime.sh
fi
+ '[' '!' -f /usr/rx30/.backup ']'

#####################################################################################################

### Call function to kill all rx30 screens
######################################
### Not used in NO_KILL mode

killProcs
+ killProcs
+ return

### Call function to stop Central Site
######################################
### Not used in NO_KILL mode

KILLCSS
+ KILLCSS
+ return

### Call function to terminate interfaces
######################################
### Not used in NO_KILL mode

stopinter
+ stopinter
+ L
./back.sh: line 288: L: command not found
+ INTERFACES=(rx30faxs rx30poss rx30pkgs rx30drxs rx30rds lifeline rx30ntfs)
+ echo 'Stopping additional interfaces.'
+ for f in '"${INTERFACES[@]}"'
+ killall rx30faxs
+ for f in '"${INTERFACES[@]}"'
+ killall rx30poss
+ for f in '"${INTERFACES[@]}"'
+ killall rx30pkgs
+ for f in '"${INTERFACES[@]}"'
+ killall rx30drxs
+ for f in '"${INTERFACES[@]}"'
+ killall rx30rds
+ for f in '"${INTERFACES[@]}"'
+ killall lifeline
+ for f in '"${INTERFACES[@]}"'
+ killall rx30ntfs
+ echo 'Interfaces stopped.'

### Call function to copy system files to /usr/rx30 directory
######################################

copysys
+ copysys
+ SYSFILES='/etc/hosts /etc/exports /etc/dhcpd.conf /etc/resolv.conf /etc/cups/printers.conf /etc/sysconfig/static-routes /etc/sysconfig/network-scripts-ifcfg-eth* /opt/ltsp/i386/etc/lts.conf /etc/fstab'
+ echo 'Copy system files to user directory.'
+ for f in '"${SYSFILES[@]}"'
+ cp /etc/hosts /etc/exports /etc/dhcpd.conf /etc/resolv.conf /etc/cups/printers.conf /etc/sysconfig/static-routes '/etc/sysconfig/network-scripts-ifcfg-eth*' /opt/ltsp/i386/etc/lts.conf /etc/fstab /usr/rx30
+ chown temp1:group /usr/rx30/
chown: invalid user: `temp1:group'
+ chmod -R 755 /usr/rx30/

umount /mnt/backup      ## removing stale or duplicate mounts
+ umount /mnt/backup

MountDrive
+ MountDrive
+ mount /mnt/backup
+ rc=0
+ '[' 0 -ne 0 ']'
df -hk | grep /mnt/usb | awk -F" " '{print $4}'
++ df -hk
++ awk '-F ' '{print $4}'
++ grep /mnt/usb
+ AVAILBACKSIZE=
df -hk | grep ${MOUNTDIR} | awk -F" " '{print $2}'
++ df -hk
++ awk '-F ' '{print $2}'
++ grep /mnt/backup
+ MAXBACKSIZE=7788448
du -s ${HOMEDIR} | awk -F" " '{print $1}'
++ du -s /usr/rx30
++ awk '-F ' '{print $1}'
+ HOMEDIRSIZE=22956
TAR
+ TAR
+ true
+ mkdir -p /mnt/backup/Tue
+ tar cvjf /tmp/rx-2012-Apr-10-Tue-08.27.tar.bz2 /usr/rx30
tar: Removing leading `/' from member names
tar: /usr/rx30/logs/2012-Apr-10-Tue-08.27-Backup.log: file changed as we read it
+ calcBakSpace
df -hk | grep ${MOUNTDIR} | awk -F" " '{print $2}'
++ awk '-F ' '{print $2}'
++ grep /mnt/backup
++ df -hk
+ MAXBACKSIZE=7788448
df -hk | grep ${MOUNTDIR} | awk -F" " '{print $4}'
++ df -hk
++ awk '-F ' '{print $4}'
++ grep /mnt/backup
+ AVAILBACKSIZE=7235208
du -hk /tmp/${tarfile}.tar.bz2 | awk -F" " '{print $1}'
++ du -hk /tmp/rx-2012-Apr-10-Tue-08.27.tar.bz2
++ awk '-F ' '{print $1}'
+ BAKFILESIZE=616
ls -ltr ${MOUNTDIR} | grep -v total | grep -v lost+found | head -1 | awk -F" " '{print $9}'
++ ls -ltr /mnt/backup
++ head -1
++ grep -v lost+found
++ grep -v total
++ awk '-F ' '{print $9}'
+ OLDFILE=Mon
df -h | grep ${MOUNTDIR} | awk -F" " '{print $2}'
++ df -h
++ grep /mnt/backup
++ awk '-F ' '{print $2}'
+ MAXBACKSIZEH=7.5G
df -h | grep ${MOUNTDIR} | awk -F" " '{print $4}'
++ df -h
++ grep /mnt/backup
++ awk '-F ' '{print $4}'
+ AVAILBACKSIZEH=7.0G
du -hs /tmp/${tarfile}.tar.bz2 | awk -F" " '{print $1}'
++ awk '-F ' '{print $1}'
++ du -hs /tmp/rx-2012-Apr-10-Tue-08.27.tar.bz2
+ BAKFILESIZEH=616K
+ '[' 616 -gt 7788448 ']'
+ '[' 616 -lt 7235208 ']'
+ echo '616K needed 7.0G available. Backup can continue'
+ tee -a /usr/rx30/logs/2012-Apr-10-Tue-08.27-Backup.log
616K needed 7.0G available. Backup can continue
+ sleep 1
+ echo 'Space Available 7.0G needed 616K. Removing Mon.'
+ tee -a /usr/rx30/logs/2012-Apr-10-Tue-08.27-Backup.log
Space Available 7.0G needed 616K. Removing Mon.
+ sleep 1
+ rm /mnt/backup/Mon
rm: cannot remove `/mnt/backup/Mon': Is a directory
+ checkTar
+ tar tfj /tmp/rx-2012-Apr-10-Tue-08.27.tar.bz2
+ bzerr=0
+ echo 0
+ echo 'Verifying compressed data.'
+ tee -a /usr/rx30/logs/2012-Apr-10-Tue-08.27-Backup.log
Verifying compressed data.
cat /tmp/bz-check.list
++ cat /tmp/bz-check.list
+ VERSTAT=0
+ '[' 0 -eq 0 ']'
+ echo 'Verification of compressed data was successfull.'
+ tee -a /usr/rx30/logs/2012-Apr-10-Tue-08.27-Backup.log
Verification of compressed data was successfull.
+ echo 'Encrypting rx-2012-Apr-10-Tue-08.27.tar.bz2'
+ tee -a /usr/rx30/logs/2012-Apr-10-Tue-08.27-Backup.log
Encrypting rx-2012-Apr-10-Tue-08.27.tar.bz2
+ openssl des3 -a -salt -in /tmp/rx-2012-Apr-10-Tue-08.27.tar.bz2 -out /tmp/rx-2012-Apr-10-Tue-08.27.tar.bz2.enc -pass pass:tdsrx30
+ echo 'rx-2012-Apr-10-Tue-08.27.tar.bz2.enc Encrypted'
+ tee -a /usr/rx30/logs/2012-Apr-10-Tue-08.27-Backup.log
rx-2012-Apr-10-Tue-08.27.tar.bz2.enc Encrypted
+ NEWTAR=/tmp/rx-2012-Apr-10-Tue-08.27.tar.bz2.enc
RSYNC
+ RSYNC
+ false
+ return
lftp
+ lftp
+ true
+ lftp -e 'set ftp:passive-mode on && cd rayb && put /tmp/rx-2012-Apr-10-Tue-08.27.tar.bz2.enc && bye' -u junk,junk ftp.rx30123.com
+ true
+ lftp -e 'set ftp:passive-mode on && cd rayb && put /tmp/rx-2012-Apr-10-Tue-08.27.tar.bz2.enc && bye' -u junk,junk ftp.rx30123.com
+ true
+ lftp -e 'set ftp:passive-mode on && cd rayb && put /tmp/rx-2012-Apr-10-Tue-08.27.tar.bz2.enc && bye' -u junk,junk ftp.rx30123.com
+ true
+ lftp -e 'set ftp:passive-mode on && cd rayb && put /tmp/rx-2012-Apr-10-Tue-08.27.tar.bz2.enc && bye' -u junk,junk ftp.rx30123.com

above is the output with the -xv option. you can clearly see that for what ever reason the lftp is not working properly.

Code:

[rx30@rx30 ~]$ lftp -e 'set ftp:passive-mode on && cd rayb && put /tmp/rx-2012-
Apr-10-Tue-08.27.tar.bz2.enc && bye' -u junk,junk ftp.rx30123.com
cd ok, cwd=/rayb
845390 bytes transferred

so what did i mess up in the script that is causing the lfpt to run in a loop instead of doing its job of putting the file up on the ftp server?

pan64 04-10-2012 07:53 AM

the lftp command and the lftp function have the same name.

colucix 04-10-2012 07:55 AM

You named the function as the lftp command, so that every time you call lftp inside the function it calls itself recursively in an endless loop. Rename the function and the trick is done.

jimtony 04-10-2012 08:03 AM

You should change your thread's title,giving more information in it.So others can help you.

lleb 04-10-2012 09:27 AM

thanks, i also found an other error. i was using the variable username in two different locations with two different =<stuff> changed the 2nd one from username => USER, i will also replace the name of the lftp function. ty

lleb 04-10-2012 09:29 AM

those 2 fixes did the trick. many thanks a.. making as solved.

David the H. 04-10-2012 11:11 AM

Another way to avoid recursion is to use the command built-in.

Code:

foo() {
        command foo <options>
}


In addition:

1)
QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


2)
It's recommended to use ((..)) for numerical tests, and [[..]] for string tests and other complex expressions. Don't use the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression


3)
Clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands, and separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be a year or so down the line).

Many people also think that it's more readable to place the "do/then" keywords on the same line as the "for/while/until/if" keywords, as it more clearly separates the outside block from the inside block.

Code:

for var in fee fai foo fum ; do

        if [[ "$var" == "foo" ]]; then
                echo "Found 'foo'."
        fi

done

Environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.

Finally, this is just my opinion, but using the full ${variable} pattern everywhere does nothing but make the code look more cluttered and harder to read. I suggest leaving the brackets off when you don't need them.


4)
You have a large number of lines like this:

Code:

MAXBACKSIZE=`df -hk | grep ${MOUNTDIR} | awk -F" " '{print $2}'`
To start with, $(..) is highly recommended over `..`.

Second, Useless Use Of Grep. Awk can do all of the pattern matching itself. Just import the bash variable value into an awk variable.

Third, since the same command is repeated so often, set up a function for it.

Code:

getsize() {
        # $1 is the pattern to match, $2 is the column number to print

        df -hk | awk -v "pattern=$1" -v "colnum=$2" '( $0 ~ pattern ) { print $colunum }'

}

maxbacksize=$( getsize "$mountdir" 2 )


lleb 04-11-2012 12:29 PM

Thank you David the H. I will read over that, but here are some adjustments I have made to the script. It is now functioning as expected.

Code:

#!/bin/bash
#
###########################################################
# Created by Ray Brunkow April 8, 2012
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 or version 3 of the
# license, at your option.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##########################################################
# Change the NAS device's IP here
# Put the NAS device's IP in the /etc/hosts table and call it
# ftp.rx30backup.com so it should look like the following
# 192.168.4.200        ftp.rx30backup.com
############################################################

#url=ftp.rx30backup.com
url=ftp.rx30.com
license=`showlic | grep LICENSE | awk '{print $6}'`                               
DATE=`date +%Y-%b-%d-%a-%H.%M`
dow=`date +%a`
rx30sessions=0
csrunning=0
HOMEDIR="$HOME"
LOG=${HOMEDIR}/logs/${DATE}-Backup.log                         
username=`whoami`
counter=0
LDIR=`pwd`
echo "hdbackd logs for ${license}" >> ${LOG}
MOUNTDIR="/mnt/backup"
TARGET=/dev/sdb1                ### Change as required
tarfile="rx${license}-${DATE}"
FILENUM=`find $HOMEDIR | wc -l`
enddate1="`date +%Y-%b-%d-%a-%H.%M.%S`"

# Change the NAS device's directory to where you want the files sent.
# Change the username and password for the NAS device.
############################################################
#backdir=array1/share/rx30backup
backdir=rayb
#password=greengrass
USER=temp1
password=cleanm#0p2

### This script is to be called with the following options:
# t for tar compressed and verified file to be pushed to any device.
# f for ftp will also use the tar function to generate the file.
# r for rsync, this will not use the tar compression.
# n for no kill mode.  this will not terminate any interfaces or rx30 screens.
# x this will be used for debug mode to troubleshoot the script to see
#  were it might be breaking.
# You may call this script with a combination of options.  Example:  back f n x will call the FTP
# backup, typically to a NAS device, it will NOT close any screens or interfaces, and will fully
# display the rest of the script as it makes calls to functions, creates the tarball,
# and connects to the NAS device for lftp transmission.

### Options
######################################

while getopts ":tfrnx" opt
do
        case $opt in
                f )        DO_FTP=true
                        DO_RSYNC=false
                        DO_TAR=true
                        ;;

                r )        DO_FTP=false
                        DO_RSYNC=true
                        DO_TAR=false
                        ;;

                t )        DO_FTP=false
                        DO_RSYNC=false
                        DO_TAR=true
                        ;;

                n )        NO_KILL=true
                        ;;

                x )        set -xv
                        ;;
        esac
done

shift $(( $OPTIND -1 ))

### FUNCTIONS
######################################

### lftp function
#####################################

LFTP()

{
        #${DO_FTP} || return

        lftp -e "set ftp:passive-mode on && cd ${backdir} && put ${NEWTAR} && bye" -u ${USER},${password} ${url}
        echo "FTP1 transmission complete to remote computer at ${url}"  >> ${LOG}
        return

}

### rsync function
#####################################

RSYNC()

{
        #${DO_RSYNC} || return

        mkdir -p /mnt/backup/${dow}
                rsync -aviS /usr/rx30/ /mnt/backup/${dow} >> ${LOG} 2>&1
}

### function to create the tar file
#####################################

TAR()

{
        #${DO_TAR} || return

        mkdir -p /mnt/backup/${dow}
                tar cvjf /tmp/${tarfile}.tar.bz2 ${HOMEDIR}  >> ${LOG}
calcBakSpace
checkTar

}

### Function to check if system can mount backup drive
#####################################

MountDrive ()

{

mount ${MOUNTDIR} >> ${LOG}
rc=$?
if [ $rc -ne 0 ]
then
        echo "[Backup] unable to mount (rx=${rc}) backup drive." >> ${LOG}
        exit 2
fi

AVAILBACKSIZE=`df -hk | grep /mnt/usb | awk -F" " '{print $4}'` ## available space on device
MAXBACKSIZE=`df -hk | grep ${MOUNTDIR} | awk -F" " '{print $2}'`  ## maximum post format size
HOMEDIRSIZE=`du -s ${HOMEDIR} | awk -F" " '{print $1}'`          ## size of rx30 dir

}

### Function to verify the tarball will fit on the backup device.
##################################

calcBakSpace()

{

MAXBACKSIZE=`df -hk | grep ${MOUNTDIR} | awk -F" " '{print $2}'`  ## maximum post format size
AVAILBACKSIZE=`df -hk | grep ${MOUNTDIR} | awk -F" " '{print $4}'` ## available space on device
BAKFILESIZE=`du -hk /tmp/${tarfile}.tar.bz2 | awk -F" " '{print $1}'`  ## compressed backup file size
OLDFILE=`ls -ltr ${MOUNTDIR} | grep -v total | grep -v lost+found | head -1 | awk -F" " '{print $9}'` ## oldest file on usb stick

MAXBACKSIZEH=`df -h | grep ${MOUNTDIR} | awk -F" " '{print $2}'`  ## more human readable format
AVAILBACKSIZEH=`df -h | grep ${MOUNTDIR} | awk -F" " '{print $4}'`
BAKFILESIZEH=`du -hs /tmp/${tarfile}.tar.bz2 | awk -F" " '{print $1}'`

## checks if tarball too large for backup device.

if [ ${BAKFILESIZE} -gt ${MAXBACKSIZE} ]
then
        echo "Total Space Available on USB ${MAXBAKSIZEH} Space Needed ${BAKFILESIZEH}" >> ${LOG}
        echo "Larger backup device needed" >> ${LOG}
        exit 1
        else

## checking if enough room on stick for backup file
## very small chance it could be wrong due to check being performed before encryption adding small increase to file size

                if [ ${BAKFILESIZE} -lt ${AVAILBACKSIZE} ]
                then
                echo "${BAKFILESIZEH} needed ${AVAILBACKSIZEH} available. Backup can continue" | tee -a ${LOG}
                sleep 1
                echo "Space Available ${AVAILBACKSIZEH} needed ${BAKFILESIZEH}. Removing ${OLDFILE}." | tee -a ${LOG}
                sleep 1
                rm ${MOUNTDIR}/${OLDFILE}
                fi
fi

}

### Check and verify tarball
#####################################

checkTar ()

{

tar tfj /tmp/${tarfile}.tar.bz2 &> /tmp/tar2; bzerr="$?"; echo $bzerr > /tmp/bz-check.list

echo "Verifying compressed data." | tee -a ${LOG}
VERSTAT=`cat /tmp/bz-check.list`

if [ ${VERSTAT} -eq 0 ]
then
        echo "Verification of compressed data was successfull." | tee -a ${LOG}
else
        echo "Compressed data appears to be corrupt.  Error ${VERSTAT}." | tee -a ${LOG}
        exit 1
fi

    echo "Encrypting ${tarfile}.tar.bz2" | tee -a ${LOG}
    openssl des3 -a -salt -in /tmp/${tarfile}.tar.bz2 -out /tmp/${tarfile}.tar.bz2.enc -pass pass:tdsrx30
    echo "${tarfile}.tar.bz2.enc Encrypted" | tee -a ${LOG}
        NEWTAR=/tmp/${tarfile}.tar.bz2.enc

# to decrypt: openssl des3 -a -d -salt -in 2011-12-20.tar.bz2.enc -out 2011-12-20.tar.bz2 -pass pass:tdsrx30

}

### Function to copy the tarball to the backup directory and verify
#####################################

copyFiles ()

{
        #${DO_TAR} || return

echo "Copying Files to backup device ${TARGET}." | tee -a ${LOG}
    \cp -a /tmp/${tarfile}.tar.bz2.enc ${MOUNTDIR}
    BAKFILESIZEENC=`du -hk /tmp/${tarfile}.tar.bz2.enc | awk -F" " '{print $1}'`
    REMBAKFILE=`du -hk ${MOUNTDIR}/${tarfile}.tar.bz2.enc | awk -F" " '{print $1}'`
        if [ ${REMBAKFILE} -eq ${BAKFILESIZEENC} ]
        then
                echo "Copying Backup file to backup device complete." | tee -a ${LOG}
        else
                echo "Copying Backup file to backup device failed.  Contact TDS." | tee -a ${LOG}
                kdialog --display=:0 --error "Copying Backup file to backup device failed.  Contact TDS."
                exit 1
        fi

}

### Function to perform final checksum between local and remote file.
#####################################

chkSum ()

{
        #${DO_TAR} || return

echo "Performing checksum on copied file" | tee -a ${LOG}
        md5sum /tmp/${tarfile}.tar.bz2.enc >/tmp/usbcksums_local
        md5sum ${MOUNTDIR}/${tarfile}.tar.bz2.enc >/tmp/usbcksums_remote
        LOCALSUM=`cat /tmp/usbcksums_local | awk -F" " '{print $1}'`
        REMOTESUM=`cat /tmp/usbcksums_remote | awk -F" " '{print $1}'`
echo "Checksum Results" >>${LOG}
echo "${LOCALSUM}  ${REMOTESUM}" >>${LOG}

if [ ${LOCALSUM} = ${REMOTESUM} ]
then
        echo "Verification was successfull ${MOUNTDIR}/${tarfile}.tar.bz2.enc and /tmp/${tarfile}.tar.bz2.enc are identical." | tee -a ${LOG}
else
        echo "There is a difference between the Backup File on the hard drive and backup device." | tee -a ${LOG}
        echo "Backup Complete" | tee -a ${LOG}
fi

}

### Function for terminating interfaces
#####################################

stopinter ()

{

        #${NO_KILL} && return

        INTERFACES=(rx30faxs rx30poss rx30pkgs rx30drxs rx30rds lifeline rx30ntfs)
        echo "Stopping additional interfaces."  >> ${LOG}

for f in "${INTERFACES[@]}"
do
killall $f &>/dev/null
done
echo "Interfaces stopped." >> ${LOG}

}

## killing rx30 procs
########################################

killProcs ()

{

        #${NO_KILL} && return

>/tmp/proclist
for i in `ps -e | grep rx30 | grep -v grep | awk -F" " '{print $4}'`
do
    if [ $i != rx30.exe ]
    then
        echo $i >>/tmp/proclist
    fi
    killall $i
echo "Rx30 screens stopped." >> ${LOG}
done

}

### starting rx30 background and autostart
########################################

startProcs ()

{
        #${NO_KILL} && return

YOURTERM=$TERM
export TERM=scoansi
rx30 bg &
. rx30 autostart 2>/dev/null
for i in `cat /tmp/proclist`
do
    $i &
done
TERM=$YOURTERM
echo "rx30 bg &, . rx30 autostart are running." >> ${LOG}

}

### Restarting most interfaces less CSS, rx30.exe in BG mode, and autostart.
########################################

restart ()

{
        #${NO_KILL} && return

        LIST=(lifeline rx30drxs rx30rds rx30faxs rx30pkgs rx30poss rx30ntfs)
        echo "Restarting interfaces." >> ${LOG}

for f in "${LIST[@]}"
do
$HOME/$f & &>/dev/null
done
echo "Interfaces restarted." >> ${LOG}

}

### Function to restart CSS
######################################

restartcss ()

{
        #${NO_KILL} && return

echo "Restarting rx30css." >> ${LOG}

$HOME/rx30css &
if [ $? -gt 0 ];
then
        echo "Problem starting Central Site interface or NON Central Site license." >> ${LOG}
else
        echo "Restarted rx30css ${enddate1}." >> ${LOG}
fi

}

### Run the first time script is run to add sudo permissions to visudo.
######################################

firstTime ()

{

cat <<EOF
#!/bin/bash
    if grep formatDev.sh /etc/sudoers
    then
        echo "sudo entries exist" | tee -a ${LOG}
    else
        echo "Adding sudo entries" | tee -a ${LOG}
        sleep 1
        sed "/${username}/ s/$/, \/bin\/mount \/mnt\/backup, \/bin\/umount \/mnt\/backup, \/bin\/chmod, \/bin\/chown, \/bin\/cp, \/tmp\/formatDev.sh/" /etc/sudoers > /etc/sudoers.spk
        cp /etc/sudoers /etc/sudoers.`date +%F`; mv /etc/sudoers.spk /etc/sudoers
        chmod 440 /etc/sudoers
        echo "sudo add complete"
        sleep 2
    fi
EOF

}

### Function to copy system files to $HOME and set proper ownership and permissions.
######################################

copysys ()

{
        SYSFILES="/etc/hosts /etc/exports /etc/dhcpd.conf /etc/resolv.conf /etc/cups/printers.conf /etc/sysconfig/static-routes /etc/sysconfig/network-scripts-ifcfg-eth* /opt/ltsp/i386/etc/lts.conf /etc/fstab"
        echo "Copy system files to user directory." >> ${LOG}

for f in "${SYSFILES[@]}"
do
cp $f $HOME &>/dev/null
done
chown ${username}:group ${HOMEDIR}/
chmod -R 755 ${HOMEDIR}/

}

### Make sure Central Site is not running
##########################################

KILLCSS ()

{
        #${NO_KILL} && return

if [ -f $HOME/rx30css ];
  then
  counter=0
  while [ $csrunning -gt 0 ];
  do
    echo "Stopping Central Site Interface" >> ${LOG}
    killall rx30css
    sleep 5
    csrunning=`ps -ef | grep rx30css | grep ^${username} | grep -v grep | wc -l`
    counter=$(($counter+1))
    if [ $counter -gt 5 ];
    then
        echo "Unable to shutdown Central Site Interface" >> ${LOG}
        exit
    fi
  done
fi

}

#####################################################################################################
# Uncomment the following command if you are getting errors like the following:              #######
# usb 4-4: device not accepting address 21, error -71                                        #######
#####################################################################################################
#
# sudo /sbin/rmmod ehci_hcd    # You will need to put "/sbin/rmmod ehci_hcd" into visudo for the user


#####################################################################################################
# Start the LOG #####################################################################################
#####################################################################################################
if [ ! -d ${HOMEDIR}/logs ]
  then
  echo "Logfile Doesn't Exist!!!" >> ${LOG}
  echo "Creating Directory" >> ${LOG}
  mkdir ${HOMEDIR}/logs
fi
#####################################################################################################

echo "Backup script started." >> ${LOG}

### Checking to see if first time to run backup script.
########################################

if [ ! -f ${HOMEDIR}/.backup ]
then
    firstTime >/tmp/firstTime.sh
    chmod +x /tmp/firstTime.sh
    sleep 1
        echo -e "Adding visudo rules during first time setup.  " > ${HOMEDIR}/.backup
        echo -e "Enter the root password \(generally \"rx30\"\) at prompt\r"
        su - -c  /tmp/firstTime.sh   
fi

#####################################################################################################

### Call function to kill all rx30 screens
######################################
### Not used in NO_KILL mode
if [ ${NO_KILL} != true ]
then
        killProcs
fi
### Call function to stop Central Site
######################################
### Not used in NO_KILL mode
if [ ${NO_KILL} != true ]
then
        KILLCSS
fi
### Call function to terminate interfaces
######################################
### Not used in NO_KILL mode
if [ ${NO_KILL} != true ]
then
        stopinter
fi
### Call function to copy system files to /usr/rx30 directory
######################################

copysys

umount /mnt/backup        ## removing stale or duplicate mounts

MountDrive

if [ ${DO_TAR} != true ]
then
        TAR
fi

if [ ${DO_RSYNC} != true ]
then
        RSYNC
fi

if [ ${DO_FTP} != true ]
then
        LFTP
fi

if [ ${DO_TAR} != true ]
then
        copyFiles
fi

if [ ${DO_TAR} != true ]
then
        chkSum
fi

if [ ${DO_TAR} != true ]
then
        startProcs
fi

if [ ${DO_TAR} != true ]
then
        restart
fi

if [ ${DO_TAR} != true ]
then
        restartcss
fi

### cleaning up tmp directory
######################################
if [ ${DO_TAR} != true ]
then
        echo "removing /tmp/${tarfile}.tar.bz2" | tee -a ${LOG}
        rm /tmp/${tarfile}.tar.bz2
        echo "removing /tmp/${tarfile}.tar.bz2.enc" | tee -a ${LOG}
        rm /tmp/${tarfile}.tar.bz2.enc
        umount /mnt/backup
        rc=$?
                if [ $rc -ne 0 ]
                then
                        sleep 2
                        umount /mnt/backup
                        rc2=$?
                                if [ $rc2 -ne 0 ]
                                then
                                        echo "Unable to unmount the backup device.  Please contact TDS." >> ${LOG}
                                        exit 2
                                else
                                        echo "Unmounted backup device successfully." >> ${LOG}
                                        kdialog --msgbox "Backup Complete! \n If you are using a thumbdrive it is now safe to unplug it."
                        fi
                fi
fi


### Notes for e-mail
### yum -y install postfix
### cd /var/spool/postfix
### mkfifo pickup
### postsuper -d ALL        ## This is to clear the printer queue a MUST on FC5/7 systems.
### service postfix restart
### chkconfig postfix on        ###this will set postfix to start on bootup###
### uncomment the following line to send e-mail everytime the script runs
# mailx -s "backup logs ${license}" user@123.net user2@123.net < ${LOG}
# echo "mailx -s "backup logs ${license}"  user@123.net user2@123.net < ${LOG}" >> ${LOG}

exit;

I will also look into what you linked to see about improving the script further.


All times are GMT -5. The time now is 01:58 AM.