On May 6, 2013 4:57 AM, "Tanstaafl" <tanstaafl@libertytrek.org> wrote:
>
> On 2013-05-05 5:25 PM, Neil Bothwick <neil@digimed.co.uk> wrote:
>>
>> On Sun, 5 May 2013 16:06:45 -0400, Todd Goodman wrote:
>>
>>> mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd
>>> /usr/bin/pg_dumpall -U $PGUSER -o | \
>>>          gzip >$BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz
>>>
>>> You could have it check first and only do the mkdir if the directory
>>> didn't already exist:
>>>
>>> [[ -d $BACKUP_DIR/$PGyy/$PGmm/$PGdd ]] || \
>>>          mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd
>>
>>
>> You could, but it is redundant as mkdir already does that test when
>> invoked with -p.
>
>
> Many thanks for the short noob bash scripting tutorial guys. Obviously I'm very new to it.
>
> I did confirm that there was no need to test (thanks Neil, this will come in handy I'm sure), but I also decided to uncomplicate it and just dump all fo the backups in a single directory. I realized this isn't like my email backups where I will be keeping years of them. I'll probably only keep 30 or so.
>
> So, my final script looks like this:
>
>
> #!/bin/bash
> BACKUP_DIR="/home/user/mypg_backups"
> PGUSER="superuser"
> PGyy=`date '+%Y'`
> PGmm=`date '+%m'`
> PGdd=`date '+%d_'`
> PGtt=`date '+%H:%M'`
> /usr/bin/pg_dumpall -U $PGUSER -o -f $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd$PGtt.gz
>
> For some reason, if I put the underscore in the filename itself, ie:
>
> $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd_$PGtt.gz
>
> It omitted the $PGdd variable entirely.
>
> I had to add the underscore into the variable to get the output like I wanted. Weird...
>
> Anyway, this is working perfectly, thanks guys.
>

Totally unweird.

In bash, underscores can be part of a variable name.

$PGdd_$PGtt involves two variables: $PGdd_ (note the trailing underscore) and $PGtt

You should write it like this:

${PGdd}_$PGtt

The { } syntax is bash's way to indicate what exactly constitutes a variable name. IOW, the above construct has three parts: the variable $PGdd, an underscore, and the variable $PGtt

Whenever you want to 'run' a variable name (i.e., combine it with other characters without specifying a whitespace), you should always use braces { } around the variable name.

That said, since you're no longer creating a directory structure, why don't you just output everything using a single 'date' command? E.g. :

date +'mypg-%Y-%m-%d_%H:%M'

Rgds,
--