#!/bin/bash
#
#
#################################
# SET GLOBAL VARIABLES FIRST #
#################################
NOW=$(date +%F-%H%M%S)
LOGFILE=/tmp/dirvish-workstation-logfile.$NOW
#################################
# Declare the functions #
#################################
function errorMail() {
echo -e "$BODY" | mail -s "$SUBJECT" -r "$SENDER" $RECIPIENTS
#NOTE: this function does not EXIT the script!
}
#The doTheBackup function is what does the actual work AFTER the list of workstations
# has be read and parsed, the banks located, and the vault located
function doTheBackup() {
#calling a function with two parameters means passing $1 and $2 into the script
# from the options where the function was called. Going to set some in-function
# variables for easier reading
# $1 was the current search bank's name
# $2 is the current workstation
# $3 the target shares to be backed up
CURRENTBANK=/banks/$1
WORKSTATION=$2
if [ ! -f $CURRENTBANK/$2-$1/dirvish/default.hist ] ; then
INITIALIZE="--init"
echo "I didn't see default.hist so I'm setting INITIALIZE"
else
echo "This vault doesn't need to be initialized."
fi
TARGETS=$3
# if the total number of arguments ($#) is greater than (-gt) 3, add each
# part onto the $JOB value.
if [ "$#" -gt 3 ] ; then
for PART in $(seq 4 "$#") ; do
eval PART=\$${PART}
echo "Adding $(PART) to $TARGETS"
TARGETS="$TARGETS ${PART}"
done
fi
#mount each target share onto its respective mountpoint
echo starting the mounts
for MOUNTIT in $TARGETS ; do
#verify that all the mount points will exists by making them (again if necessary)
mkdir -p /mnt/$WORKSTATION/$MOUNTIT > /dev/null
#Mount the workstations share onto the mountpoint
mount -t smbfs -o credentials=~root/.credentials \
//$WORKSTATION/$MOUNTIT\$ /mnt/$WORKSTATION/$MOUNTIT
df | if ! grep -q "/mnt/$WORKSTATION/$MOUNTIT" ; then
echo "There was a problem mounting /mnt/$WORKSTATION/$MOUNTIT\$" >> $LOGFILE
# -OR-
#error out if target not reachable
#send errorMail
#exit
fi
done
#run the dirvish command for that mountpoint
dirvish $INITIALIZE --vault $WORKSTATION-$SB
#email recipients with backup report
#not implemented yet
#umount the workstation shares
for MOUNTIT in $TARGETS ; do
umount /mnt/$WORKSTATION/$MOUNTIT
done
}
#################################
# BEGIN THE SCRIPT #
#################################
if [ ! -f /etc/dirvish/workstation.conf ] ; then
BODY="No workstation.conf file found. Please check and try again"
SUBJECT="Missing variable values. Unable to start workstation backup"
errorMail
exit
fi
#parce the workstation backup config file to read in the following variables
#ADMINNOTIFY
#SENDER
#HITLIST
#PRIMARYBANK
#SECONDARYBANK
. /etc/dirvish/workstation.conf
#let's send the start time to the logfile
echo "Workstation backup started at $NOW" > $LOGFILE
#check for the existance of the workstation list control file that contains
# the list of machine names to be backed up.
# abort if not found.
if [ ! -f $HITLIST ] ; then
SUBJECT="workstation list not found"
BODY="The list of workstations to backup wasn't found, please correct this issue and try again."
echo "$SUBJECT. $BODY"
for RECIPIENTS in $ADMINNOTIFY; do
errorMail $SUBJECT $BODY $RECIPIENTS
done
exit
fi
# Get the banks mounted so that we can backup to them
# NOTE: the fstab will have to include these paths so that
# we can mount them by path name
#if no workstation banks get mounted.. why bother with the workstation backup?
for MOUNTBANK in $PRIMARYBANK $SECONDARYBANK ; do
mount /banks/$MOUNTBANK
#now check to see if the server sees if the current $MOUNTBANK got mounted
#NOTE: we can't pipe the results of df to an if-grep subshell because we won't be able
# to keep track of the varables once we are done with the subshell, so let's
# send df's output to string
DFSTRING="$(df)"
if ! echo $DFSTRING | grep -q "/banks/$MOUNTBANK" ; then
echo "There was a problem mounting bank $MOUNTBANK" >> $LOGFILE
else
MOUNTCHECK=success
fi
done
#check to make sure we have at least ONE mounted workstation bank
#by check to see that MOUNTCHECK does exists because it has been set to something (succes in our case)
if [ ! $MOUNTCHECK ] ; then
SUBJECT="Dirvish workstation bank Mount error(TESTING)"
BODY="No workstation banks were mounted. There is no point in doing the workstation backup. Please correct the mount issue for banks: $PRIMARYBANK and $SECONDARYBANK and try again"
echo "$SUBJECT. $BODY"
for RECIPIENTS in $ADMINNOTIFY; do
errorMail $SUBJECT $BODY $RECIPIENTS
done
echo "Exiting due to error: $SUBJECT"
exit
fi
#prep the HITLIST for Unix processing
fromdos < $HITLIST > /tmp/hitlist.$NOW
HITLIST=/tmp/hitlist.$NOW
echo "I'm going to read the arrays now."
#Now lets read in the file of workstations to backup using file descriptor number 5
# so that we can build an array of machine names
exec 5<$HITLIST
while read -a LINE <&5; do
#If $COUNT is unset, then....
if [ ! $COUNT ] ; then
#check to see if the line is empty
if [ -z $LINE ] ; then
#echo "Count isn't set and shouldn't be set yet - this is an empty line"
continue
fi
#COUNT hasn't been set and the current line isn't empty so let's set it now..
COUNT=0
#echo "Setting count to 0 because COUNT wasn't set and I see data in LINE"
else
#echo "I see that count has already been set so let's test to see if we need to increment COUNT."
#if count DOES exist, then let's increment it, but only if the
# value of the current line isn't empty
if [ ! -z $LINE ] ; then
((COUNT++))
#echo "I see data on LINE. I'm incrementing -- I'm now on $COUNT"
else
#skip on to the next line
#echo "There was no data on LINE. continuing with next line."
continue
fi
fi
#a debug line
#echo "processing # $COUNT"
#The data on each line will consist of:
#workstation_name share1_to_backup share2_to_backup (etc)
#so we read in each LINE as an array using the -a option,
#because we didn't want to store the line a s string such as
#"workstation_name share1_to_backup share2_to_backup" but
#rather we want to read it in as an array so that we can
#reference specific portions of the line. This means that
#instead of using $LINE to reference everything on the line
#in the file, we have to use ${LINE[} to reference all(@)
#elements in the ${ARRAY}. If you just ask for $LINE when
#it is really a ${LINE]}, then the system will only return
#the FIRST element in the ${LINE[} array.
#Storing the first element of the LINE ARRAY into the current $COUNT
# of the WRKSTN array <-- this will be the workstation name to be backed-up
WRKSTN[$COUNT]=${LINE[0]}
#Storing ALL of the elements of the LINE array starting with element 1, (the second
#element in the LINE array. These elements will be the TARGET shares to backup.
WRKSTNTARGETS[$COUNT]=${LINE]:1}
#TODO: what if there are no targets??? Should this be handled here or in the backup
# workstation loop?
#PROPOSED: if [ -z ${WRKSTNTARGETS[} ] ; then
# ${WRKSTNTARGETS[0]}=null
# fi
done
- close file
exec 5>&-
echo "done loading the arrays"
- If there are not workstations to be backed up (by virtue of the array being empty)
- then inform admin
]} = 0 ] ; then
echo "the workstation array count is at 0. exiting with nothing to backup."
exit
else
echo "I see workstations to backup"
fi
#debug exit while testing the array count test...
#Because we are having to use multple syncronized arrays to accomplish our task
#We can't just do a "for CWS in ${WRKSTN[} ; do " command. We will need to count
- the number of elements in the WRKSTN array and start a loop (starting a 0 because bash
- starts its arrays at 0) and ending at TotalNumberOfElements MINUS ONE! <-- once again
- because bash starts its arrays at 0
- CurrentWorkStation = CWS
- Calculate the number of elements in the WRKSTN array and start a for loop
- to run through the workstations.
]} -1)) ) ; do
CWS=${WRKSTN[$CURRENTELEMENT]}
#search the each bank directoriy for the current workstation's vault
#SearchBank->SB
for SB in $PRIMARYBANK $SECONDARYBANK ; do
echo processing bank $SB
#Check to see if the current workstation already has a vault directory
if [ ! -d /banks/$SB/$CWS-$SB ] ; then
echo "Didn't find /banks/$SB/$CWS-$SB"
#if we DON'T find the vault, then we need to create one..but
# only on the bank and not on the mountpoint so let's check to see if
# this is a mountpoint first
if [ -f /banks/$SB/mountpointonly ] ; then
#The mount the banks part of the script will deal with notifying
# the admin once...so we don't have to do it here.
# Just continue on to the next bank or workstation because
# there is no sense continuing on with this do loop
echo "Bank not mounted. Skipping"
continue
fi
#The bank is mounted and no directory exists so let's make the directory
mkdir -p /banks/$SB/$CWS-$SB/dirvish
#copy in a default.conf file
cp /etc/dirvish/config.files/defaultworkstation /banks/$SB/$CWS-$SB/dirvish/default.conf
#now adjust the vault file to set the path.
sed -i -e "s#setcustompath#/mnt/$CWS#g" /banks/$SB/$CWS-$SB/dirvish/default.conf
#If we have created the vault automatically, we'll have to signal that the vault
#needs to be initialized
#NOT NEEDED! Just look for the default.hist file that will exist once the vault is initialized
#touch /banks/$SB/$CWS-$SB/needinit
fi
# NOTE: Since we are going to have to do the dirvish backup TWICE for each workstation
# let's function'alize the following processes so that we can use only set of code.
# This will necessitate variablizing things in such a way to make them usable in both the
# onsite and offsite banks.
#call the doTheBacukp function to start the backup process
#for the current workstation
doTheBackup $SB $CWS "${WRKSTNTARGETS[$CURRENTELEMENT]}"
done
done
#cleanup
rm $HITLIST
exit
