[NCLUG] I think

Bob Proulx bob at proulx.com
Fri Feb 21 09:12:49 MST 2014


Hi Kerry,

Kerry Miller wrote:
> I have written a fresh shell script:
> 
> !# /bin/bash

A typo.  That should be #! there.

> # file: BKusers.sh
> 
> cd /home

If there is a problem with /home, or any directry in the cd command,
then the cd command will print an error but then the script will
continue.  With /home it is unlikely that will fail.  But it might.
It is always good to do something like this so that if there is a
problem that the script will stop at that point.  The cd itself will
print an error message so none is required.  I usually keep it simple.

  cd /home || exit 1

> tar -cvzf /mnt/UserBackup/BK_KerrysStuff.tar.gz kerrym2/KerrysStuff
> ...
> echo "BKusers.sh Done"

All of that looked just fine to me.

> I think this one will accomplish what I want done but it seem a bit
> brute force.

I don't really see anything wrong with brute force here.  More
important is if it does what you want it to do and that you understand
it so that you can maintain it.

One thing I wonder about is that you are creating one backup at a
time.  While doing so the script is going to overwrite any previous
backup.  That makes me slightly nervous.  I would prefer if it created
a new backup leaving the previous ones available.  I would suggest
putting the backups in a dated directory.  Something like this:

#!/bin/sh
cd /home || exit 1
today=$(date +%F)  # Example: "2014-02-21"
mkdir -p /mnt/UserBackup/$today
tar -cvzf /mnt/UserBackup/$today/BK_KerrysStuff.tar.gz kerrym2/KerrysStuff
... and so on with the rest ...

That way each day goes into a different directory and backups from
previous days are not overwritten.  You can remove older backups to
prevent consuming all disk space as a separate intentional step.

The date +%F produces dates in a nice "computer sorting" way of year
month day like 2014-02-21.  The mkdir -p can run multiple times and
creates the directory if it does not exist.  If it is a second run in
the same day then the directory will exist already and mkdir will be
okay with it.

This creates one backup copy in a day and if you backup multiple times
in a single day then those will overwrite each other.  That seems like
a nice improvement over the previous without creating too fine of a
granularity.

If you would like a unique backup directory each time at more
granularity than one per day then change the date command to emit
hours minutes seconds too.

today=$(date "+%F_%T")  # Example: "2014-02-21_09:09:16"

I suggest avoiding spaces in the file name.  I like underscores so
suggest that but a dot would be fine too.  There is a time format
standard that uses a "T" there as in "2014-02-21T09:09:16" but I think
that is ugly and for this use case doesn't get you anything in return
so I didn't suggest it.  This will be a filename that you will be
seeing and so should look nice to a human looking at it.

For your purposes I would probably stick with just a date for the
filename.  But I wanted to say how it would be easy to expand to more
granularity if that were desired.

Bob


More information about the NCLUG mailing list