Re: Help with script - doesn't work properly from cron

2009-07-18 Thread Bernd Eggink

Erik Olof Wahlstrom schrieb:

Hello - I am having a problem with a backup script that I have put together -
when I run it as root from the terminal, it works as expected (with one
caveat); however, when cron runs it, the daily backup folder is created but
no files are deposited into that folder...

Here is the script:

#!/bin/bash
BACKUP_DIR="/media/disk/AUTOMATED_BACKUPS/DB_DAILY"

CURRENT_DIR=$BACKUP_DIR/`date +%d`

DATABASES="$(/usr/bin/mysql -uUsername -pPassword -Bse 'show databases')"
echo 'Backing up databases: '$DATABASES

if [ -e "$CURRENT_DIR" ]
then
cd $CURRENT_DIR
/bin/rm *
else
/bin/mkdir $CURRENT_DIR
cd $CURRENT_DIR
fi

for DB in $DATABASES
do
/usr/bin/mysqldump -uroot -pHardAsMySql321 "$DB" | bzip2 >
"$DB"_`date +%Y-%m-%d_%k.%M`".sql.bz2"
done

exit 0

Can any skilled eyes see why this doesn't work when it is run from the roots
crontab?


The reason may be that crontab commands are run with "/bin/sh -c 
command" (Bourne shell mode). Try changing the crontab entry to 
"/bin/bash command".



Additionally, when I run the script as root in the terminal, I get the
following output:

Backing up databases: information_schema db1 db2 db3
/bin/rm: cannot remove `*': No such file or directory

Is there a better way to clear out last months files before making the
current backups?


You could replace the whole if-then-else clause by

mkdir -p $CURRENT_DIR
cd $CURRENT_DIR
rm -f *

provided the options are supported on your system.

Regards,
Bernd

--
Bernd Eggink
http://sudrala.de




Re: printf -v doesn't allow array assignment

2009-07-18 Thread Stephane CHAZELAS
2009-07-17, 22:02(-04), Chris F.A. Johnson:
>
> $ printf -v q[2] "%s" "$RANDOM"
> bash: printf: `q[2]': not a valid identifier
>
>
>  I know I can work around it with a temporary variable, but it
>  would be nice if it could be done in a single step.
[...]

Just do a "touch q2" before and then it should stop complaining
;-)

Having said that, it's true that if "read 'q[2]'" is allowed, why
not printf -v 'q[2]'.

-- 
Stéphane


bash 4: source does not search current dir

2009-07-18 Thread Denys Vlasenko
Hi,

I assume . command does not search current directory anymore
in bash 4 for a reason. Perhaps it is specified by relevant
standard, although I did not find it.

This incompatible change affects me a lot. I imagine
I am not alone with this.

To forestall questions from users like me, consider
adding information about it to this file:

http://tiswww.case.edu/php/chet/bash/COMPAT

Thanks.
--
vda




Re: bash 4: source does not search current dir

2009-07-18 Thread Eric Blake
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

According to Denys Vlasenko on 7/18/2009 11:48 AM:
> Hi,
> 
> I assume . command does not search current directory anymore
> in bash 4 for a reason. Perhaps it is specified by relevant
> standard, although I did not find it.

It is specified by POSIX:
http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#dot

You can add . to your PATH (probably smarter at the end, not the
beginning) if you want to find scripts without anchoring them with a
leading './'.

- --
Don't work too hard, make some time for fun as well!

Eric Blake e...@byu.net
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkpiJpQACgkQ84KuGfSFAYDQzQCfcKRLHuLJ8qGvXST63r4haGA+
QkQAoLj+VbtyXr44VAp8dOOgYZbvXKWu
=+GfJ
-END PGP SIGNATURE-