[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Help with script - doesn't work properly from cron
From: |
Erik Olof Wahlstrom |
Subject: |
Re: Help with script - doesn't work properly from cron |
Date: |
Mon, 20 Jul 2009 16:08:25 -0700 (PDT) |
Thanks everyone for your pointers - I ended up with this:
#!/bin/bash
BACKUP_DIR="/media/disk/AUTOMATED_BACKUPS/DB_DAILY"
CURRENT_DIR=$BACKUP_DIR/`date +%d`
DATABASES="$(mysql -uroot -pNewSecretPw -Bse 'show databases')"
echo 'Backing up databases: '$DATABASES
cd /
rm -rf "$CURRENT_DIR"
mkdir -p "$CURRENT_DIR"
cd "$CURRENT_DIR" || exit 1
for DB in $DATABASES
do
mysqldump -uroot -pNewSecretPw "$DB" | bzip2 > "${DB}_$(date
+%Y-%m-%d_%H:%M).sql.bz2"
done
exit 0
It worked on the first manual run - keeping my fingers crossed for tonight!
:-P
Bob Proulx wrote:
>
> Greg Wooledge wrote:
>> Erik Olof Wahlstrom wrote:
>> > /usr/bin/mysqldump -uroot -pHardAsMySql321 "$DB" | bzip2 >
>> > "$DB"_`date +%Y-%m-%d_%k.%M`".sql.bz2"
>>
>> # Long line, probably broken by your mailer. For clarity, I'd
>> # write it on two lines explicitily:
>>
>> mysqldump -uroot -pHardAsMySql321 "$DB" |
>> bzip2 > "${DB}_$(date +%Y-%m-%d_%k.%M).sql.bz2"
>
> You might want to set umask restrictive so that the backup file
> created by the redirection isn't world readable.
>
> umask 077
>
> I have never liked whitespace in filenames and %k may produce spaces
> in filenames. Better IMNHO to use %H here and avoid the space. Why
> use a dot instead of the more common colon in the time?
>
> mysqldump -uroot -pHardAsMySql321 "$DB" |
> bzip2 > "${DB}_$(date +%Y-%m-%d_%H:%M).sql.bz2"
>
>> You could also consider writing it this way:
>>
>> cd /
>> rm -rf "$CURRENT_DIR"
>> mkdir -p "$CURRENT_DIR"
>> cd "$CURRENT_DIR" || exit 1
>>
>> Then you don't even need to check whether it already exists.
>
> Why bother changing directory there at all? :-)
>
> mysqldump -uroot -pHardAsMySql321 "$DB" |
> bzip2 > "$CURRENT_DIR/${DB}_$(date +%Y-%m-%d_%H:%M).sql.bz2"
>
>> Bernd Eggink wrote:
>> > You could replace the whole if-then-else clause by
>> >
>> > mkdir -p $CURRENT_DIR
>> > cd $CURRENT_DIR
>> > rm -f *
>>
>> Another failure to check the results of "cd" before doing "rm *". This
>> can and will lead to disasters.
>
> Doing 'rm *' just makes me extremely nervous even when care is taken
> to ensure that it is going to work okay. I think it is safer to avoid
> it. Let me suggest the following:
>
>> > CURRENT_DIR=$BACKUP_DIR/`date +%d`
>> ...
>> cd "$CURRENT_DIR" || exit 1
>> ...
>> mysqldump -uroot -pHardAsMySql321 "$DB" |
>> bzip2 > "${DB}_$(date +%Y-%m-%d_%k.%M).sql.bz2"
>
> In this case the file name is going to be SOMETHING.sql.bz2. Instead
> of using 'rm *' it would make me much less nervous to qualify that
> file glob with 'rm -f *.sql.bz2' at least so that if this were to escape
> and be invoked elsewhere it would have limited damage potential.
>
> cd "$CURRENT_DIR" || exit 1
> rm -f *.sql.bz2
>
>> > Is there a better way to clear out last months files before making the
>> > current backups?
>
> Personally I have the following line in my /etc/cron.d/local-mysql
> crontab. The 'savelog' command with -c7 will save seven days of
> datestamped backups (sufficient for my case) without further code.
> That could easily be -c30 to save 30 days worth. The 'savelog'
> command pretty much handles all of your issues with datestamping and
> cycling files very easily.
>
> 30 3 * * * root umask 077 ; mysqldump
> --defaults-file=/etc/mysql/debian.cnf --all-databases | gzip >
> /var/backups/mysql.dump ; savelog -q -d -l -C -c7 /var/backups/mysql.dump
>
> Bob
>
>
>
>
--
View this message in context:
http://www.nabble.com/Help-with-script---doesn%27t-work-properly-from-cron-tp24542164p24579084.html
Sent from the Gnu - Bash mailing list archive at Nabble.com.