Solved Backup MySQL with .sh script and crontab

Hello Everyone,

My script fails in cron but runs OK manually, here is what I have done.

Created a very simple script :
Code:
#!/bin/sh
mysqldump -u User -pPassword dbname > /usr/home/username/backups/dbname.sql
tar -czf /usr/home/username/backups/dailybackup.tar.gz /usr/home/username/public_html/
Added to crontab using crontab -e as root :
Code:
1  3  *  *  * /usr/local/bin/bash /usr/local/etc/backup/bkup.sh
Tested at shell prompt by executing :
# /usr/local/bin/bash /usr/local/etc/backup/bkup.sh
and get a successful backup of database and public_html directory.

When cron runs both files date and time updates, so I know it ran! However, the SQL file size is ZERO! It should be in excess of 9Mb, the TAR file is as it should be just under 500Gb.

Any idea why crontab fails to grab a good MySQL dump would be appreciated.

Kulraj
 
Why are you using bash? Also, try including the full path of the commands in your script. Example: /usr/local/bin/mysqldump
 
For me works doing this:

First set you password and DBDescription to a encrypted file with this command bellow:
mysql_config_editor set --login-path=DBDescription --host=DB#.pair.com --user=DBUser --port=3306 --password

See if that works:
mysql_config_editor print --login-path=DBDescription

The output will be something like this:
Code:
[DBDescription]
user = root
password = *****
host = localhost
port = 3306

Now create a shell script:

Bash:
#!/bin/sh
#Set paths
MYDUMP=/usr/local/bin/mysqldump
BACKUPDIR=/usr/local/www/apache24/data
MYGZIP=/usr/bin/gzip
MYFIND=/usr/bin/find

#Dump
$MYDUMP --login-path=DBDescription DBNAME > $BACKUPDIR/mysql-DBNAME.`date '+%d-%B-%Y--%Hh'`.sql

#GZIP the file
$MYGZIP $BACKUPDIR/mysql-DBNAME.`date '+%d-%B-%Y--%Hh'`.sql

And finally create a job with cron.
Here I use:
# nano /etc/crontab
At the final line of the file add:
Bash:
00 04 * * * root /usr/local/www/apache24/data/mybackup.sh

Based on the information of the page: link
 
Back
Top