[nmglug] Bash Scripting help

Tim Emerick timothyemerick at yahoo.com
Wed Mar 30 12:47:38 PST 2005


I've been working on a bash backup script that runs as a cron job.  To say
it's inelegant is an understatement at best.  I could use some advice on
structure and output.

1. Rotating backup sets is tedious if I wanted to add or delete the number of
sets to retain.  Is there a way to loop using a variable?

2. When running as a cron job I get an email after the job has run showing
output.  The commands that were run are preceded with a '+'.  I would like to
selectively replace them with something more descriptive using echo.

3. I'm using date to give me a start and stop time but I would also like a
total time that the script took to run.

Below is the actual script and the email from cron.

Thanks for your insight.

Tim
~~~~~~~~~~~ Start Script ~~~~~~~~~~~~~~~~~~~~~~~~
#!/bin/sh -x
#
# Backup Procedure.
#
# This backs up the /shares directory keeping a 10 day
# live backup set for easy file recover via samba share.
#
# Some superflous files are deleted from backups older than 2 days.

# Show what time we started
echo Backup started `date`

rm -fr /backup/10
mv /backup/09 /backup/10
mv /backup/08 /backup/09
mv /backup/07 /backup/08
mv /backup/06 /backup/07
mv /backup/05 /backup/06
mv /backup/04 /backup/05
mv /backup/03 /backup/04
mv /backup/02 /backup/03
mv /backup/01 /backup/02
mkdir /backup/01

# Clean up some non-essential backup files from the prior backup set.
rm -fr /backup/02/bcd/
rm -fr /backup/02/ghost/
rm -fr /backup/02/gmtools
rm -fr /backup/02/installs/
rm -fr /backup/02/kodata/
rm -fr /backup/02/nai/
rm -fr /backup/02/nissan-efast/
rm -fr /backup/02/nissan-winfast/
rm -fr /backup/02/sambastuff/
rm -fr /backup/02/etc/
rm -fr /backup/02/var/
rm -fr /backup/02/temp/

# I like to keep some backups for 2 days.  Let's delete those now.
rm -fr /backup/03/pathways/

# CoPy the shares folder to the backup directory
cp -a /shares/* /backup/01/

# also copy the /etc and /var directory
cp -a /etc/ /backup/01/etc/
cp -a /var/ /backup/01/var/

# Exit with some statistics
du -sh /shares/*
du -sh /backup/*
df -h

echo
echo Backup Finished `date`
~~~~~~~~~End Script~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~Start cron/at email ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
++ date
+ echo Backup started Wed Mar 30 12:42:00 MST 2005
Backup started Wed Mar 30 12:42:00 MST 2005
+ rm -fr /backup/10
+ mv /backup/09 /backup/10
+ mv /backup/08 /backup/09
+ mv /backup/07 /backup/08
+ mv /backup/06 /backup/07
+ mv /backup/05 /backup/06
+ mv /backup/04 /backup/05
+ mv /backup/03 /backup/04
+ mv /backup/02 /backup/03
+ mv /backup/01 /backup/02
+ mkdir /backup/01
+ rm -fr /backup/02/bcd/
+ rm -fr /backup/02/ghost/
+ rm -fr /backup/02/gmtools
+ rm -fr /backup/02/installs/
+ rm -fr /backup/02/kodata/
+ rm -fr /backup/02/nai/
+ rm -fr /backup/02/nissan-efast/
+ rm -fr /backup/02/nissan-winfast/
+ rm -fr /backup/02/sambastuff/
+ rm -fr /backup/02/etc/
+ rm -fr /backup/02/var/
+ rm -fr /backup/02/temp/
+ rm -fr /backup/03/pathways/
+ cp -a /shares/accounting /shares/accounting-pr /shares/bcd /shares/bluebook
/shares/cbs /shares/ghost /shares/gmtools /shares/installs /shares/kodata
/shares/nai /shares/nissan-efast /shares/nissan-winfast /shares/pathways
/shares/sambastuff /shares/temp /backup/01/
+ cp -a /etc/ /backup/01/etc/
+ cp -a /var/ /backup/01/var/
+ du -sh /shares/accounting /shares/accounting-pr /shares/bcd
/shares/bluebook /shares/cbs /shares/ghost /shares/gmtools /shares/installs
/shares/kodata /shares/nai /shares/nissan-efast /shares/nissan-winfast
/shares/pathways /shares/sambastuff /shares/temp
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
182M	/shares/nai
501M	/shares/nissan-efast
623M	/shares/nissan-winfast
1.7G	/shares/pathways
14M	/shares/sambastuff
148M	/shares/temp
+ du -sh /backup/1 /backup/10 /backup/2 /backup/3 /backup/4 /backup/5
/backup/6 /backup/7 /backup/8 /backup/9
28G	/backup/01
2.1G	/backup/10
3.7G	/backup/02
2.1G	/backup/03
2.1G	/backup/04
2.1G	/backup/05
2.1G	/backup/06
2.1G	/backup/07
2.1G	/backup/08
2.1G	/backup/09
+ df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hda1              6728280    665812   5720688  11% /
tmpfs                   128472         0    128472   0% /dev/shm
/dev/hda6            146371480  49574860  89361340  36% /backup
/dev/md0              76922848  28424988  44590332  39% /shares
+ echo

++ date
+ echo Backup Finished Wed Mar 30 13:02:44 MST 2005
Backup Finished Wed Mar 30 13:02:44 MST 2005
~~~~~~~~~~~~~~~Finish cron/at email~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


		
__________________________________ 
Do you Yahoo!? 
Read only the mail you want - Yahoo! Mail SpamGuard. 
http://promotions.yahoo.com/new_mail 




More information about the nmglug mailing list