1

Here is my question asked on StackExchange 2 days ago. https://stackoverflow.com/q/35839190/4772652 Someone suggested me to ask here. Could anyone give me a hand to solve this?

============

I had tried to launch a script of mysqldump for daily backup by launchd and also by Carbon Copy Cloner (CCC). The script works fine under command line. However, it comes with empty file under launchd or CCC.

While I manually ran the command sh mysqldump.sh, a <DATE>.sql.gz file will be created. And if the launchd/CCC called the script, a <DATE>.sql.gz file will also be created. However, the file size will by 2 0B.

Here is the content of my plist file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.MySQL.crontab</string>
        <key>Program</key>
        <string>/bin/mysqldump.sh</string>
        <key>RunAtLoad</key>
        <true/>
        <key>StandardErrorPath</key>
        <string>/tmp/AlTest1.err</string>
        <key>StandardOutPath</key>
        <string>/tmp/AlTest1.out</string>
        <key>StartCalendarInterval</key>
        <dict>
            <key>Weekday</key>
            <integer>5</integer>
            <key>Hour</key>
            <integer>6</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
    </dict>
</plist>

And the launchd status for this plist is - 0 com.MySQL.crontab

Here is the script for mysqldump, which is created by Schulz with slight modified

#!/bin/sh
# *************************************************************
# file: mysqldump.sh
# date: 2015-03-27
# author: (c) by Marko Schulz - <info@tuxnet24.de>
# description: Get a mysqldump of all mysql databases.
# *************************************************************

dbUsername="me"
dbBackup="/Backup/MySQL_backup"

date=$( date +%Y%m%d )

find $dbBackup/ -mtime +30 -type f -name '*.sql.gz' -exec rm -rf {} ';' >/dev/null 2>&1

mysqldump -u $dbUsername --all-databases | gzip > ${dbBackup}/${date}.sql.gz 2>&1

What could I do to solve this problem?

  • Where is mysqldump? The probable issue is that it is not on the path for launchd although you have the /bin/mysqldump.sh in Apple's controlled area so I might be wrong – Mark Mar 9 '16 at 11:05
  • Is the path to the script actually /bin/mysqldump as the script shows? – Mikey T.K. Mar 9 '16 at 16:33
1

Your shell script is missing the full path to mysqldump (assuming you've installed MySQL Community Edition by Oracle):

/usr/local/mysql/bin/mysqldump -u $dbUsername --all-databases | gzip > ${dbBackup}/${date}.sql.gz 2>&1

If you need a password define it in the shell script:

dbPassword="my_dbuser_password"

and change the above line to:

/usr/local/mysql/bin/mysqldump -u $dbUsername -p$dbPassword --all-databases | gzip > ${dbBackup}/${date}.sql.gz 2>&1

There is no space between -p and $dbP...!

I also advice to move the shell script to another path (e.g. /usr/local/bin/). You have to adjust your plist then and the line

    <string>/bin/mysqldump.sh</string>

becomes:

    <string>/usr/local/bin/mysqldump.sh</string>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .