PDA

View Full Version : Your Ideas on a bash script please


techmatt
17th December 2006, 07:12 PM
I have written this bash scipt so I only have to create one cron job. I have it doing so basic backups and cleanups. I tryed to make it easaly portable to my other computers as well. I am planing on add more things to it but this is the basic setup. My scripting skills are still very basic, and I was wondering if some more skilled scripters/programers could see anything I could do better or optimise the script.
#!/bin/bash#
#
#
#Written by Matt (rug)
#Special Thanks to giulix for the error handleing tips
#
#
#This is were to set if root will be backed up
rootAcc="yes" #yes or no only
#
#Setting Varitables
bkdevice="/dev/hdd1" #The device Backups will be on
bkdir="/mnt" #were to mount the Backup Device
log="/home/fcsifile/Desktop/SystemJob.log" #log location
bkuser="/mnt/" #users are backup location
bkroot="/mnt/" #root is backed up to
#
bkimportant1="/var"
bkimpottant2="/etc"
day=$(date +%a)
nday=$(date +%d)
#
#Starting Log
echo >> $log "================================================== =========="
echo >> $log "--------------$(date +%c)---------------"
echo >> $log "================================================== =========="
#
#After and command you would want to see errors on please put "|| (err="what

program does" ; Error)" or
# "|| (err="what program does" ; ErrorExit)" to stop script
#
#Sends any Error to the log file
function Error {
echo "Failed on $err Please Alert Matt"
echo >> $log "Failed on $err Please Alert Matt"
return
}
#
#Cloeses log and exits program on error
function ErrorExit {
echo "Failed on $err Please Alert Matt"
echo >> $log "Failed on $err Please Alert Matt"
echo >> $log "Failed on $err Please Alert Matt"
echo >> $log "==========================End Log=========================="
echo >> $log " "

exit 1
}
#
#Checking for root privelages
ROOT_UID=0 # Root has $UID 0.
if [ "$UID" -eq "$ROOT_UID" ]
then
echo "Checking user: User is logged in as root. [OK]"
else
echo "Checking user: Failed"
echo "You Must Log in as Root for this Script to Function Correctly"
echo >> $log "ERROR: Checking Root Failed"
echo >> $log "==========================End Log=========================="
echo >> $log " "
exit 0
fi
#
#Mount backup drive
mount $bkdevice $bkdir || (err="Mounting $bkdevice on to $bkdir" ; ErrorExit)
#
#Every day task can go here
#
#
#
#Daily Setup (Insert Any Script You Want On The Day Listed)
#
#Monday
if [ "$day" == "Mon" ]
then
echo >> $log "starting Yum Update"
yum -y update || (err="Update" ; Error)
#
#Tuesday
elif [ "$day" == "Tue" ]
then
echo >> $log "Nothing else scheduled on Tuesday"
#
#Wednesday
elif [ "$day" == "Wed" ]
then
echo >> $log "Running Users Backup to $bkuser"
cd $bkdir
for i in `ls -1 /home`
do
n=$tm
tm=$(($n + 1))
tar -cpz -f Home-$i-$day.tar /home/$i || (err="$bkuser" ; Error)
rm -fv Home-lost+found-$day.tar
done
cd /
#
#Thursday
elif [ "$day" == "Thu" ]
then
echo >> $log "Nothing else scheduled on Thursday"
#
#Friday
elif [ "$day" == "Fri" ]
then

echo >> $log "Running Cleanup"
echo >> $log "$(df -h) Before Cleanup"
echo "Finding Trash Folders"
for trash in `find / -type d -name .Trash`
do
cd "$trash"
rm -vrf "$trash"/*
in=$tr
tr=$(($in + 1))
cd /
done
echo "$tr Trash Folders Found"
#finds and deletes tmp files
echo "Finding Temp Folders"
for tmp in `find / -type d -name tmp`
do
cd "$tmp"
rm -vrf "$tmp"/*
i=$tm
tm=$(($i + 1))
cd /
done
echo "$tm Temp Folders Found"
echo "Cleaning Up Yum Activity"
yum clean all
#Just makes it look like yum out put is part of the script
echo "$tm Temp Folders Emptied"
echo "$tr Trash Folders Emptied"
echo >> $log "$(df -h) After Cleanup"
#
#Saturday
elif [ "$day" == "Sat" ]
then
if [ "$rootAcc" == "yes" ]
then
echo >> $log "Starting Root Backup to $bkroot"
cd $bkroot
tar -cpz -f Root-$day.tar /root || (err="$bkroot" ; Error)
cd /
elif [ "$rootAcc" == "Yes" ]
then
echo >> $log "Starting Root Backup to $bkroot"
cd $bkroot
tar -cpz -f Root-$day.tar /root || (err="$bkroot" ; Error)
cd /
elif [ "$rootAcc" == "No" ]
then
echo >> $log "Root Backup has been disabled"
elif [ "$rootAcc" == "no" ]
then
echo >> $log "Root Backup has been disabled"
else
echo >> $log "Error on Root Backup configuration"
fi
#
#Sunday
elif [ "$day" == "Sun" ]
then
echo >> $log "Nothing else scheduled on Sunday"
#
elif [ $nday -eq 1 ]
then
echo >> $log "Running Media Backup to $bkmedia"
cd $bkmedia
tar -cpz -f var.tar $bkimportant1 || (err="$bkimportant1" ; Error)
tar -cpz -f etc.tar $bkimportant2 || (err="$bkimportant2" ; Error)
#
else
echo >> $log "Error Scripted failed. Date not valid"
#
fi
#
#Unmount backup device
cd ~
umount $bkdir || (err="$bkdir" ; ErrorExit)
#
#Closing Log
echo >> $log "==========================End Log=========================="
echo >> $log " "

#
exit 0
Thank you
Matt

jim
17th December 2006, 07:34 PM
In your check for root instead of exiting the script prompt or roots password. here is a function to ask for roots password. add GOT_root under the line #Checking for root privelagesin your script and comment out all your lines

CMDLN_ARGS="$@" # Command line arguments for this script
export CMDLN_ARGS

# Run this script as root if not already.
GOT_root () {

if [ ! $( id -u ) -eq 0 ]; then
echo "Please enter root's password."
exec su -c "${0} ${CMDLN_ARGS}" # Call this prog as root
exit ${?} # sice we're 'execing' above, we wont reach this exit
# unless something goes wrong.
fi

}

# Call this function early on - now or before your script's main loop starts.
GOT_root

techmatt
17th December 2006, 07:39 PM

Cool I have been wondering how to do that
Thanks

techmatt
18th December 2006, 05:16 PM
Since Jim is the only on that responded then I am left to assume that I did a good job on my first real atempt to produce bash scripting.
I'm so proud of my self :p

by the way thanks agin Jim it works perfectly