title: Bash scripting, checking for errors, logging
body: <p>Here's one for the bash-fu wizards. No, actually, I'm just kidding, you'll all probably know this except for me..</p>
  
  <p>I'm trying to create a backup shell script. The idea is fairly simple: find files in a certain folder, older than 7 days, tar/gzip them to another directory, and remove them. The problem is, I'm not sure if I'll have enough permissions to create a tar/gzip file in the target dir. Is there any (proper) way to check if the file has been created successfully, and if so, delete the files. Otherwise, skip that part and don't destroy customers' data. I hear they are not very fond of that.</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>01: #!/bin/bash
  02: 
  03: ROOTDIR="/data/www"
  04: 
  05: TAR="${ROOTDIR}/log/svg_out_xml/export_out_ack_$(date +%Y-%m-%d).tar"
  06: cd ${ROOTDIR}/exchange/export/out_ack/
  07: find . -mtime +7 -type f -print0 | xargs -0 tar -cf "${TAR}"
  08: gzip ${TAR}
  09: find . -mtime +7 -type f -print0 | xargs -0 rm -f
  </code></pre>
  
  <p>Basically, I'd need to check if everything went fine on lines 7 and 8, and if so execute 9.</p>
  
  <p>Additionally, I'd like to make a log file of these operations so I know everything went fine (this is a nightly cron job).</p>