Luca Luca - 4 months ago 125
SQL Question

Shell script Permission denied mysqldump: Got errno 32 on write only in Cron

I have a simple sh file to backup some MySql databases and FTP them to an external server.
The shell script works fine if called from a command line, but doesn't work when scheduled as cron job.

All the files are chmdod 777

This is the script

mysqldump -uuser-ppassword databasename | gzip > shop1.gz
mysqldump -uuser -password databasename | gzip > shop2.gz
mysqldump -uuser -ppassword databasename | gzip > timesheets.gz
mysqldump -uuser -ppassword databasename | gzip > library.gz
tar -cvvf backup.tar *.gz

filename="backup.tar"
hostname="****"
username="****"
password="****"
ftp -un $hostname <<EOF
quote USER $username
quote PASS $password
passive
cd db_backups
binary
put $filename
quit
EOF


I got the errors:

httpdocs/dump/db_bck.sh: line 1: shop1.gz: Permission denied
mysqldump: Got errno 32 on write
httpdocs/dump/db_bck.sh: line 2: shop2.gz: Permission denied
mysqldump: Got errno 32 on write
httpdocs/dump/db_bck.sh: line 3: timesheets.gz: Permission denied
mysqldump: Got errno 32 on write
httpdocs/dump/db_bck.sh: line 4: library.gz: Permission denied
mysqldump: Got errno 32 on write
tar: backup.tar: Cannot open: Permission denied
tar: Error is not recoverable: exiting now
ftp: u: unknown option


THIS IS THE FINAL WORKING SCRIPT, JUST IN CASE WOULD BE USEFUL TO SOMEONE ELSE:

mysqldump -u -p mydb1 | gzip > httpdocs/dump/mydb1.gz
mysqldump -u -p mydb2 | gzip > httpdocs/dump/mydb2.gz
mysqldump -u -p mydb3 | gzip > httpdocs/dump/mydb3.gz
mysqldump -u -p mydb4 | gzip > httpdocs/dump/mydb4.gz


cd /var/www/vhosts/mydomain.com/httpdocs/dump
filename="*.gz"
hostname="ftphostname"
username="ftpusernam"
password="ftppassword"
/usr/kerberos/bin/ftp -un $hostname <<EOF
quote USER $username
quote PASS $password
passive
cd db_backups
binary
prompt
mput $filename
quit
EOF

Answer

Ensure the following (preliminary checks)

  1. Make sure you have enough free disk space.

  2. Increase max_allowed_packet for mysqldump

  3. Permission of the user running the cron (check everything chown, chgrp, chmod)

  4. Make sure gzip is is actually working

  5. Ensure you have have write permissions for the filename="backup.tar"

  6. Try to use full path information like (/var/lib/mysql/backups/shop2.gz)

  7. Lastly use touch to create the file first, and then run the script using cron