mysqldump via cron

ncw
ncw used Ask the Experts™
on
Getting the following error in an email from the scheduled task cron system on a dedicated server:
sh: /mysqldump: No such file or directory

The cron runs a php file with code that executes a mysqldump command using exec. The code works fine when accessed via a url and also ok via an SSH terminal (Putty) but not via cron. The command we're using (with password and path removed) is below.

I've tried adding /usr/bin/ infront of mysqldump, but no difference:

Any ideas please? The control panel is Plesk.

mysqldump -hlocalhost -u db_user -p********** db_somename | gzip >
/var/www/<path>/backup_tue.sql.gz

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2004

Commented:
Have you verified the location of mysqldump?
LinuxGuruLinux Server Administrator

Commented:
Hello,

Try to locate the mysqldump using the following command.

locate mysqldump
ncw

Author

Commented:
Entering locate mysqldump in Putty at root level returns command not found. But if I browse to /usr/bin/ in WinSCP then I can see the mysqldump file (65Kb)., and if I enter the above command with /usr/bin/mysqldump then it works.

Same command in Php file works via url but not via cron.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Top Expert 2004

Commented:
>>> Entering locate mysqldump in Putty at root level returns command not found.

That implies your environment variable 'PATH' is not set properly.  Check to make sure /usr/bin is included.

>>> Same command in Php file works via url but not via cron.

Make sure the user running the cron job has appropriate access to /usr/bin.  That access could be restricted by file system rights or by PHP's operating environment.  Running PHP from the command line will create a different environment than running it from a web page.  Try a cron job that writes phpinfo() to a local file and examine the configuration.
ncw

Author

Commented:
the locate command is not recognised, however whereis confirms mysqldump location as /usr/bin/

The cron now seems to be finding mysqldump but is now giving the following error:
Permission denied
mysqldump: Got errno 32 on write

So as you infer above there are some permissions issues when running cron. How can I give the cron environment the same permissions as the web url environment, or is that a security issue?
LinuxGuruLinux Server Administrator
Commented:
Hello,

Please make sure that the script has permissions to read/write in the location where the backup is saved,
ncw

Author

Commented:
The existing file is 664 with owner as the account username
The directory is 777 with owner as apache

How do I make sure that the script has permissions to read/write in the location where the backup is saved?


Top Expert 2004
Commented:
You need to verify which user is running the cron job, and verify that user has write permission to the directory.  You say the directory is 777, so any user should be able to write to it.  The existing file, though, is read-only for anybody but the owner/group.  Instead of making the file 666 (which adds write to 'everyone'), consider adding the cron user to the file's group owner.
ncw

Author

Commented:
How would I add the cron user to the file's group owner?
Top Expert 2004

Commented:
usermod -aG <file_group_owner> <username>

You should make sure you want this to happen.  If you think there might be security issues, revisit the user owning the cron job, or the user owning the file.
ncw

Author

Commented:
The cron will now create new backup files ok with owner name as the account name, but the existing backup files have the owner set as 'apache' (the existing files were created by running the php script via a url). For the moment I've renamed the existing files and hope the new files will now be overwritten correctly on a rolling basis as the ownership will be the account name. Not sure how the existing files came to have an ownership of 'apache'.

 
Top Expert 2004

Commented:
>>> Not sure how the existing files came to have an ownership of 'apache'.

That is the user that originally created them.  That means either your cron job was running as apache, or the cron job called a script to run under the web service account.

Either way, you could chown those files if it becomes a problem.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial