title: mysqldump & gzip commands to properly create a compressed file of a MySQL database using crontab
body: <p>I am having problems with getting a <code>crontab</code> to work. I want to automate a MySQL database backup.</p>
  
  <p>The setup:</p>
  
  <ul>
  <li>Debian GNU/Linux 7.3 (wheezy) </li>
  <li>MySQL Server version: 5.5.33-0+wheezy1(Debian) </li>
  <li>directories user, backup and backup2 have 755 permission</li>
  <li>The user names for MySQL db and Debian account are the same</li>
  </ul>
  
  <p>From the shell this command works</p>
  
  <pre><code>mysqldump -u user -p[user_password] [database_name] | gzip &gt; dumpfilename.sql.gz
  </code></pre>
  
  <p>When I place this in a crontab using crontab -e </p>
  
  <pre><code>* * /usr/bin/mysqldump -u user -pupasswd mydatabase | gzip&gt; /home/user/backup/mydatabase-backup-`date +\%m\%d_\%Y`.sql.gz &gt;/dev/null 2&gt;&amp;1
  </code></pre>
  
  <p>A file is created every minute in /home/user/backup directory, but has 0 bytes.</p>
  
  <p>However when I redirect this output to a second directory, backup2, I note that the proper mysqldumpfile duly compressed is created in it. I am unable to figure what is the mistake that I am making that results in a 0 byte file in the first directory and the expected output in the second directory.</p>
  
  <pre><code>* * /usr/bin/mysqldump -u user -pupasswd my-database | gzip&gt; /home/user/backup/mydatabase-backup-`date +\%m\%d_\%Y`.sql.gz &gt;/home/user/backup2/mydatabase-backup-`date +\%m\%d_\%Y`.sql.gz 2&gt;&amp;1
  </code></pre>
  
  <p>I would greatly appreciate an explanation.</p>
  
  <p>Thanks</p>