[nmglug] Bash Scripting Help -- Thanks!!!

Tim Emerick timothyemerick at yahoo.com
Thu Apr 7 00:15:20 PDT 2005


Hey gang.  Would like to thank those that passed on some tips on my backup
script.  This is probably the first "real" bash script (simple as it may be)
that I've ever really written.  Makes me appreciate linux over windows more
and more each day.  I thought I'd post my latest incarnation of the script
and see if anybody would like to give it a go over with a discerning eye.

The only thing I don't like is the timing feedback where it will echo "1
minutes" instead of "1 minute".  Anybody have any tips on that.  I'm a little
anal retentive if you couldn't already tell.

Thanks in advance.

Tim Emerick


#!/bin/sh
#
# Backup Script.
# Written by Tim Emerick on April 6, 2005
#
# Thanks to the NMLUG/NMGLUG members for the bash scripting help.
# Also would like to thank http://www.google.com/linux and the
# Advanced Bash Scripting Guide.
#
# This backs up the /shares directory keeping a variable amount
# of full and partial backup sets for easy file recover via samba share.
#
# Variable List:
#
# q             Total number of backups to keep.
# qoffset       Sorting is weird if using numbers less than 10 so this is an
offset.
# a             Misc for processing files.
# starttime     Time when script started.
# optime        Misc variable for duration of individual sections of code.

starttime=`date +%s`

##############################################################################
### Put the amount of backup days desired here
##############################################################################

q=30
qoffset=11
q=$((q-1+qoffset))

echo "Backup started `date`"
echo ""
echo -n "Rotating and cleaning old Backup Directories."
optime=`date +%s`

#################################################################################
# Loop through directories in reverse order.  If too old then delete else
rotate.
#################################################################################

for a in `ls -r /backup/`; do
  if [ "$a" -ge "$q" ]; then
    rm -fr /backup/$a
  else
    mv /backup/$a /backup/$((a+1))
  fi
  echo -n "."
done
mkdir /backup/$qoffset

#######################################################################
### Clean up some non-essential backup files from the prior backup set.
#######################################################################

for a in bcd ghost gmtools installs kodata nai nissan-efast nissan-winfast 
sambastuff etc var temp
do
  rm -fr /backup/$((qoffset+1))/$a
  echo -n "."
done

####################################################################################
# I like to keep some backups for 2 days instead of just 1.  Let's delete
those now.
####################################################################################

for a in pathways
do
  rm -fr /backup/$((qoffset+2))/$a
  echo -n "."
done

optime=$((`date +%s`-$optime))
echo ""
echo "Finished in $((optime/60)) minutes and $((optime%60)) seconds."

#######################################################
# Copy the /shares, /etc, /var  to the backup directory
#######################################################

echo -n "Backing up files in /shares /etc /var"
optime=`date +%s`

echo -n "."
cp -a /shares/* /backup/$qoffset/
echo -n "."
cp -a /etc/ /backup/$qoffset/etc/
echo -n "."
cp -a /var/ /backup/$qoffset/var/

optime=$((`date +%s`-$optime))
echo ""
echo "Finished in $((optime/60)) minutes and $((optime%60)) seconds."

#########################################
# Exit with some statistics
#########################################

optime=$((`date +%s`-$starttime))

echo ""
echo "Total Backup took $((optime/60)) minutes and $((optime%60)) seconds."
echo ""
echo "Total diskspace used by the SHARES folder:"
echo "------------------------------------------"
du -sh /shares/*

echo ""
echo "Total diskspace used by the BACKUP folder:"
echo "------------------------------------------"
du -sh /backup/*

echo ""
echo "Total hard drive usage:"
echo "------------------------------------------"
df -h

exit 0


And here's the output of running that script.

Backup started Thu Apr  7 00:43:40 MDT 2005

Rotating and cleaning old Backup Directories.........................
Finished in 2 minutes and 4 seconds.
Backing up files in /shares /etc /var...
Finished in 17 minutes and 27 seconds.

Total Backup took 19 minutes and 31 seconds.

Total diskspace used by the SHARES folder:
------------------------------------------
266M    /shares/accounting
29M     /shares/accounting-pr
613M    /shares/bcd
740M    /shares/bluebook
1.1G    /shares/cbs
9.6G    /shares/ghost
808M    /shares/gmtools
9.7G    /shares/installs
1.4G    /shares/kodata
266M    /shares/nai
501M    /shares/nissan-efast
623M    /shares/nissan-winfast
1.7G    /shares/pathways
17M     /shares/sambastuff
372M    /shares/temp

Total diskspace used by the BACKUP folder:
------------------------------------------
28G     /backup/11
3.7G    /backup/12
2.1G    /backup/13
2.1G    /backup/14
2.1G    /backup/15
2.1G    /backup/16
2.1G    /backup/17
2.1G    /backup/18
2.1G    /backup/19
2.1G    /backup/20
2.1G    /backup/21
2.1G    /backup/22

Total hard drive usage:
------------------------------------------
Filesystem            Size  Used Avail Use% Mounted on
/dev/hda1             6.5G  589M  5.6G  10% /
tmpfs                 126M     0  126M   0% /dev/shm
/dev/hda6             140G   52G   81G  40% /backup
/dev/md0               74G   28G   43G  40% /shares





		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - 250MB free storage. Do more. Manage less. 
http://info.mail.yahoo.com/mail_250




More information about the nmglug mailing list