4

I'm trying to create a cron that daily backups my MySQL slave. The backup.sh content:

#!/bin/bash
#
# Backup mysql from slave
#
#
sudo mysql -u root -p'xxxxx' -e 'STOP SLAVE SQL_THREAD;'
sudo mysqldump -u root -p'xxxxx' ng_player | gzip > database_`date +\%Y-\%m-\%d`.sql.gz 
sudo mysqladmin -u root -p'xxxxx'  start-slave

I made it executable by sudo chmod +x /home/dev/backup.sh and entered in to crontab by:

sudo crontab -e

0 12 * * * /home/dev/backup.sh

but it doesn't work, if I only run in the command line it works but not in crontab.

FIXED: I used the script from this link: mysqldump doesn't work in crontab

Community
  • 1
  • 1
lior
  • 1,127
  • 3
  • 24
  • 43

4 Answers4

1

Break the problem in half. First try sending only email from the cron job to see if you are getting it to even run. Put this above in a file and have your cron job point to it:

#!/bin/bash

/bin/mail -s "test subject" "yourname@yourdomain" < /dev/null

The good thing about using this tester is that it is very simple and more likely to give you some results. It does not depend on your current working directory, which can sometimes be not what you expect it to be.

Elliptical view
  • 3,338
  • 1
  • 31
  • 28
  • i used '42 17 * * * /bin/mail -s "test subject" "mymail@gmail.com" < "sometext"' and it didnt work – lior Dec 25 '13 at 15:44
  • Well, that wasn't quite what I was thinking, but interesting. I was thinking put this in a simple three line file and then point cron to that file. – Elliptical view Dec 25 '13 at 16:41
  • (Sorry, for it not working before, my unix is a little rusty.) When I first had this problem I spent days getting it to work. Hope this helps you. – Elliptical view Dec 26 '13 at 00:26
  • I edited my answer. Please look at it again. I know I haven't solved your problem full, but rather just given you a little tool to help you help yourself. Merry xmas. – Elliptical view Dec 26 '13 at 00:33
0

Try use full link to mysql bin directory in .sh file

example :

sudo /var/lib/mysql -u root -p'xxxxx' -e 'STOP SLAVE SQL_THREAD;'
PHAO THU
  • 125
  • 3
  • 13
0

I had this same problem.

I figured out that you can't use the command sudo in a non-interactive script. The sudo command would create a field where you would type in the password to your account (root).

If you are logged into a command prompt like ssh sudo works without typing in any passwords, but when another program runs sudo it would ask for password.

Try this instead su command doesn't require any logins and it does the same thing.

su --session-command="mysql -u root -p'xxxxx' -e 'STOP SLAVE SQL_THREAD;'" root
su --session-command="mysqldump -u root -p'xxxxx' ng_player | gzip > database_`date +\%Y-\%m-\%d`.sql.gz" root
su --session-command="mysqladmin -u root -p'xxxxx'  start-slave" root

Replace root with your linux username.

EDIT: Look at this thread for a different answer. https://askubuntu.com/questions/173924/how-to-run-cron-job-using-sudo-command

Community
  • 1
  • 1
SSpoke
  • 5,656
  • 10
  • 72
  • 124
  • It doesnt work i get: su: unrecognized option '--session-command=mysql -u root -p'XXX' -e 'STOP SLAVE SQL_THREAD;'' Usage: su [options] [LOGIN] – lior Dec 25 '13 at 15:55
  • Oh it works for CentOS linux you probably have a different one. This will work for sure but the script requires your `root` password `echo "root_password_goes_here" | sudo -S mysql -u root -p'xxxxx' -e 'STOP SLAVE SQL_THREAD;'` This thread has more answers http://askubuntu.com/questions/173924/how-to-run-cron-job-using-sudo-command – SSpoke Dec 25 '13 at 22:35
0

Let's start with the silly stuff in the script.

The only command which you don't run via 'sudo' is the, spookily enough, only command which I would expect you might need to run via sudo (depending on the permissions of the target file).

Prefixing the commands in a script with sudo without a named user (i.e. running as root) serves no useful function if you are invoking the script as root.

On a typical installation, the mysql, mysqladmin and gzip programs are typically executable by any user - the authentication and authorization of the commands to the DBMS are authenticated by the DBMS using the authentication credentials passed as arguments - hence I would not expect that any of the operations here, except possibly writing to the output file (depending on its permissions).

You don't specify a path for the backup file - maybe it's writing it somewhere other than you expect?

(similarly, you should check if any of the executables are in a location which is not in the $PATH for the crontab execution environment).

but it doesn't work

....is not an error message.

The output of any command run via cron is mailed to the owner of the crontab - go read your mail.

symcbean
  • 47,736
  • 6
  • 59
  • 94