Save thesql backup to afp network drive fails as cron job

afpcronMySQLterminal

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.

Best Answer

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.