[nmglug] Bash Scripting help

Mars DeLapp jmdelapp at delapp.com
Wed Mar 30 17:08:39 PST 2005


Tim Emerick wrote:
> 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?

yes, see the advanced bash scripting guide
http://www.tldp.org/LDP/abs/html/
You might want to look at for, while and case.

> 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.

Store the start time and caclulate total time at the end.
Here is an example
-------------------- Start script --------------
#!/bin/bash
# crap script written 3/30/05 by JMD

starttime=`date +%s`
echo "start = $starttime"

#do something
updatedb

endtime=`date +%s`
echo "end = $endtime"
total=`expr $endtime - $starttime`

echo "it took $total seconds to run this script"
-------------------- End script --------------

I took a backup script and did an extreem makover. It does a full backup once a week and incremental backups daily. It 
has a current backup that is really just a simlink to the most current backup set.

One of the most important features for me was to break up the backup into multiple tasks. As an example: system, user 
home directories, project files, admin files, photos, email, etc. I did this by reading a directory 
(/etc/marsbackup/tasks/) and for each of the backup include (.bki) files, it creates a separate backup file that 
includes the directories listed in the .bki file.

You can get a copy here
http://tentrocks.com/marsbackup.sh.txt

Let me know if you find this usefull.

Mars

-- 
=============================================================
J. Marsden DeLapp, PE
President
DeLapp & Associates, Inc. dba DeLapp Engineering
Providing lighting and power planning, design and analysis services
for commercial, industrial and large residential facilities.
1300 Luisa St Ste 23
Santa Fe NM 87505
(505) 983-5557
http://DeLapp.com
=============================================================






More information about the nmglug mailing list