Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Thanks for reading this.

I have created a bash script to backup my databases. The script will create a directory for the day then create a backup file for each database within that directory.

It runs as expected if I call it from the command line.

I want it to run when the machine starts up so I have created a cron job to run it.

When the cron job runs it creates the directory but it doesn't create the files.

I have checked the syslog and I can see the cron job being processed but there is no output that indicates an error. There is a message which appears to suggest that an email would have been sent if postfix or some such were installed. I don't really want to install postfix unless I really have to.

Is there any other log I could be looking at?

Here's the cron job (it runs as root):

@reboot /usr/bin/db.backup.sh

here's the bash script:

#!/bin/bash

DB_BACKUP="/files/database_backups/`date +%Y-%m-%d`"
DB_USER="root"
DB_PASSWD="a"
HN=`hostname | awk -F. '{print $1}'`

# Create the backup directory
mkdir -p $DB_BACKUP

# Remove backups older than 10 days
find /files/database_backups/ -maxdepth 1 -type d -mtime +10 -exec rm -rf {} \;

# Backup each database on the system
for db in $(mysql --user=$DB_USER --password=$DB_PASSWD -e 'show databases' -s --skip-column-names|grep -viE '(staging|performance_schema|information_schema)');
do mysqldump --max-allowed-packet=1073741824 --user=$DB_USER --password=$DB_PASSWD --events --opt --single-transaction $db | gzip > "$DB_BACKUP/$db-$(date +%Y-%m-%d).sql.gz";
chown -R www-data:www-data $DB_BACKUP
done
share|improve this question
    
I notice that someone (Tim?) has edited my question. How and why? Can anyone edit other people's questions? Did I write something wrong/offensive/stupid? I'm not being stroppy here, I just want to understand the etiquette of the forum so that I get it right in future. –  Simon Jul 30 at 12:42
    
as your job depends of MySQL service being up and running, I recommend to see this answer and use upstart instead of cron. TLDR, with upstart you can set your job to start after MySQL service being up. –  Stef K Jul 30 at 14:02
    
Thanks Stef K ... I will look at this, every bit of understanding helps and it is useful to have options. The answer/comments below have fixed the problem. –  Simon Jul 30 at 14:24

1 Answer 1

up vote 1 down vote accepted

Scripts run under cron doesn't have the same environment as when you run it from the command line.

This can be because the PATH defined under cron doesn't include the directory where mysql and mysqldump are located.

Or you use the full path when referencing these commands in your script, like /usr/bin/mysql or you create a new PATH variable at the beginning of your script :

export PATH=/bin:/usr/bin

Or pointing to any other directory where mysql and mysqldump are installed if you did not install MySQL using the default Ubuntu package.

To keep the possible error messages that would have been produced when the script is run from cron, if you don't get a mail locally, you can simply add output redirection to your crontab definition like this :

@reboot /usr/bin/db.backup.sh >/tmp/db-backup.log 2>&1

Any message, information and error, will be later found into /tmp/db-backup.log.

Consider using '>>' instead of '>' in the above line if you want to append the information and not overwrite the log file at each execution.

Update: if the cron job is run before the MySQL server is up

If the script is run in the startup process of the machine, before MySQL server is up, you can implement a small check at the beginning of the script to wait for the MySQL socket file to be present :

while [ ! -e /var/run/mysqld/mysqld.sock]; do
  # if the file doesn't exist, will loop
do

Attention : if the MySQL never comes up for some reason, the script will loop forever.

share|improve this answer
1  
Thanks Benoit, that has certainly moved me along, I've now got an error message.I added export PATH=/bin:/usr/bin to the script but I got the same error message as I did without it: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) - I am googling away now trying to figure that one out! –  Simon Jul 30 at 13:13
2  
@Simon: with the error message you gave and if the script is still running fine when you launch it manually at the command line, I've the impression that the cronjob is run BEFORE the MySQL server is started. If you still want to run this script at the startup of the machine, you may prefer to launch it from the /etc/rc.local startup script, which is the last script executed during the boot process. –  Benoit Jul 30 at 13:21
    
Thank you Benoit, that (the rc.local solution) worked. –  Simon Jul 30 at 14:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.