title: Shell script Permission denied mysqldump: Got errno 32 on write only in Cron
body: <p>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.</p>
  
  <p>All the files are chmdod 777</p>
  
  <p>This is the script</p>
  
  <pre><code>mysqldump -uuser-ppassword databasename | gzip &gt; shop1.gz
  mysqldump -uuser -password databasename | gzip &gt; shop2.gz
  mysqldump -uuser -ppassword databasename | gzip &gt; timesheets.gz
  mysqldump -uuser -ppassword databasename | gzip &gt; library.gz 
  tar -cvvf backup.tar *.gz
  
  filename="backup.tar"
  hostname="****"
  username="****"
  password="****"
  ftp -un $hostname &lt;&lt;EOF
  quote USER $username
  quote PASS $password
  passive
  cd db_backups
  binary
  put $filename
  quit
  EOF
  </code></pre>
  
  <p>I got the errors:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>THIS IS THE FINAL WORKING SCRIPT, JUST IN CASE WOULD BE USEFUL TO SOMEONE ELSE:</p>
  
  <pre><code>mysqldump -u -p mydb1 | gzip &gt; httpdocs/dump/mydb1.gz
  mysqldump -u -p mydb2 | gzip &gt; httpdocs/dump/mydb2.gz
  mysqldump -u -p mydb3 | gzip &gt; httpdocs/dump/mydb3.gz
  mysqldump -u -p mydb4 | gzip &gt; 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 &lt;&lt;EOF
  quote USER $username
  quote PASS $password
  passive
  cd db_backups
  binary
  prompt
  mput $filename
  quit
  EOF
  </code></pre>