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.

Please forgive my ignorance. I have searched, but perhaps I’m not sure really what to search for in order to answer my question myself, so I’m turning to this forum for a little guidance.

I’ve stood up an Ubuntu server 12.04.3. This server is primarily running MySQL. I am attempting to set up a job that will perform a daily dump.

I am logged in as my own user, I have created an executable file called /home/xyz/MySQL_Dumps/RunMySQLDump

-rwxrwxr-x  1    0 1000       1012 Jan 15 15:26 RunMySQLDump

crontab –l shows:

12 * * * * /home/xyz/MySQL_Dumps/RunMySQLDump

Contents of RunMySQLDump:

MyTimeNow=$(date +"%Y%m%e_%H%M")

mysqldump  --host=localhost --user=<user> --password=<password>--max_allowed_packet=1G --skip-opt --port=3306 --default-character-set=utf8 --single-transaction=TRUE --dump-date=TRUE --comments --flush-privileges --force --all-databases --result-file=/home/xyz/MySQL_Dumps/$MyTimeNow-X.dump

mysqldump  --host=localhost --user=<user> --password=<password> --max_allowed_packet=1G --skip-opt --port=3306 --default-character-set=utf8 --single-transaction=TRUE --dump-date=TRUE --comments --flush-privileges --force --all-databases --result-file=/home/xyz/MySQL_Dumps/$MyTimeNow-Y.dump

mysqldump  --host=localhost --user=<user> --password=<password> --max_allowed_packet=1G --skip-opt --port=3306 --default-character-set=utf8 --single-transaction=TRUE --dump-date=TRUE --comments --flush-privileges --force --all-databases --result-file=/home/xyz/MySQL_Dumps/$MyTimeNow-Z.dump

tar -cvf $MyTimeNow-all.tar $MyTimeNow*.dump
gzip *.tar
rm $MyTimeNow*.dump

So, here’s the problem I’m running up against. When I log into the server as my own user “xyz” and I manually run the command ./RunMySQLDump, everything works fine:

/home/xyz/MySQL_Dumps

-rw-rw-r--  1 1000 1000 1042848433 Jan 15 14:03 20140114_0310-all.tar.gz
-rw-rw-r--  1 1000 1000 1065212917 Jan 15 15:18 20140115_1512-all.tar.gz
-rw-rw-r--  1 1000 1000 1065718672 Jan 15 16:28 20140115_1622-all.tar.gz
-rw-rw-r--  1 1000 1000 1077965846 Jan 16 11:42 20140116_1136-all.tar.gz

However, when I let cron run the job, this is what I get: /home/xyz (note: NOT in /home/xyz/MySQL_Dumps AND no data!)

-rw-rw-r--  1 xyz xyz     67 Jan 16 16:12 20140116_1612-all.tar.gz

Can someone please point me in the right direction?

Thanks immensely!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

When you run with ./RunMySQLDump the working directory of the script is /home/xyz/MySQL_Dumps/, that is why it works.

When cron starts the script it's working directory is /home/xyz/ (your home directory) as it is in a user crontab.

The first lines in your script use absolute paths, the last lines use relative paths. That is why it fails.

Just change the working directory in the script before you start using relative paths or better at the beginning of the script:

cd /home/xyz/MySQL_Dumps/

Or use absolute paths everywhere in the script.

share|improve this answer
    
I wound up placing absolute paths. Thanks a lot! –  user236652 Jan 20 at 23:02

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.