1

For some strange reason I am not able to save a generated mysql backup via mysqldump to an afp network drive as a cron job. If I call the shell script as root user (login via sudo su in terminal), it works just perfectly. So, the script is ok. It must have something to do with the cron job itself.

For doing so, I edited the root's crontab using crontab -e like so:

10 12 * * 1-5 sh /var/root/cronjobs/mysql_backup.workdaily.sh

This tells the crontab daemon to execute it every work day (monday to friday) at 12.10 PM.

With the same call from terminal it works (as described above).

This is the shell script that I am using for doing it:

#!/usr/bin/env sh
LOCALMOUNTPOINT="/Volumes/MyDrive"

# check if network drive is mounted
if mount | grep "on $LOCALMOUNTPOINT" > /dev/null; then
    echo "mounted"
else
    # not needed the below check, but much more safer in cron job execution:
    if ls -lha /Volumes | grep "$LOCALMOUNTPOINT" > /dev/null; then
        mount -t afp "afp://username:password@NETWORKDRIVE%28AFP%29._afpovertcp._tcp.local/Subfolder" /Volumes/MyDrive
    else
        mkdir /Volumes/MyDrive && mount -t afp "afp://username:password@NETWORKDRIVE%28AFP%29._afpovertcp._tcp.local/Subfolder" /Volumes/MyDrive
    fi
fi

mysqldump -u root -ppassword --all-databases | gzip -c > $(date "+/Volumes/MyDrive/Dumps/mysql/username/full__%FT%H_%M_%S.sql.gz")

The cron job creates a file on network drive with a file size of 20 Bytes. If I gunzip it, it is empty. Normally the database has about 12 MB. So, something is wrong here.

This script is executed via the cron job in lunch time, so no one uses the MacBook at this time (the screen is locked with a logged in user). Standby is fully disabled (only the screen standby is active after 10 minutes).

Does anyone know why this does not work? Any help or suggestions would be great.

  • 2
    The shebang is missing, also mysqldump may not be in PATH. If this doesn't help please catch any output created the crib job and add it to the question – nohillside Mar 31 '17 at 11:53
  • @patrix sorry, my bad. The shebang issue was just a copy & paste error. I edited the question. And yes, mysqldump is in PATH, because I added the mysql app to PATH variable. But it may can be caused by the sudo shell. So, maybe it is in the users one, but not in the root one's. And there is no error to show. Because the root user has not received ever any mail. Usually you would receive a mail, if a cron job exited with an error code. The strange thing is, there is a file, but without any content. – alpham8 Mar 31 '17 at 12:04
  • I concur with @patrix about mysqldump not being in PATH. Expanding on that, keep in mind that when cron is run, it has no PATH environment to begin with so try expanding out the full path for mysqldump. – Allan Mar 31 '17 at 12:14
  • Also, when testing, are you running under the same user as the cronjob does? To catch all output, consider redirecting stdout and stderr to a file in crontab – nohillside Mar 31 '17 at 12:34
  • Not related to your problem, but existence of directories is easily tested with if [[ -d "LOCALMOUNTPOINT" ]]; then instead of using ls and grep :-) – nohillside Mar 31 '17 at 13:06
3

Found the solution: The problem was, that the mysqldump command could not be find. I needed to add the absolute path to it /Applications/MySQLWorkbench.app/Contents/MacOS/mysqldump

Thanks to patrix and Allan for that tip.

You must log in to answer this question.

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