title: Bash script to backup files to remote FTP. Deleting old files
body: <p>I'm writing a bash script to send backups to a remote ftp server. The backup files are generated with a WordPress plugin so half the work is done for me from the start. </p>
  
  <p>The script does several things.</p>
  
  <ol>
  <li>It looks in the local backup dir for any files older than x and deletes them</li>
  <li>It connects to FTP and puts the backup files in a dir with the current date as a name</li>
  <li>It deletes any backup dirs for backups older than x</li>
  </ol>
  
  <p>As I am not fluent in bash, this is a mishmash of a bunch of scripts I found around the net. </p>
  
  <p>Here is my script: </p>
  
  <pre><code>#! /bin/bash 
  
  BACKDIR=/var/www/wp-content/backups
  
  #----------------------FTP Settings--------------------#
  
  FTP=Y
  
  FTPHOST="host"
  FTPUSER="user"
  FTPPASS="pass"
  FTPDIR="/backups"
  LFTP=$(which lftp)      # Path to binary
  
  #-------------------Deletion Settings-------------------#
  
  DELETE=Y
  
  DAYS=3 # how many days of backups do you want to keep?
  
  TODAY=$(date --iso) # Today's date like YYYY-MM-DD
  RMDATE=$(date --iso -d  $DAYS' days ago') # TODAY minus X days - too old files
  
  #----------------------End of Settings------------------#
  
  if  [ -e $BACKDIR ]
  then
  
  if  [ $DELETE = "Y" ]
  then
      find $BACKDIR -iname '*.zip' -type f -mtime +$DAYS -delete
      echo "Old files deleted."
  fi
  
  if  [ $FTP = "Y" ]
  then
  
      echo "Initiating FTP connection..."
  
      cd $BACKDIR
  
      $LFTP &lt;&lt; EOF
      open ${FTPUSER}:${FTPPASS}@${FTPHOST}
      mkdir $FTPDIR
      cd $FTPDIR
      mkdir ${TODAY}
      cd ${TODAY}
      mput *.zip
      cd ..
      rm -rf ${RMDATE}
      bye
  
  EOF
  
  echo "Done putting files to FTP."
  
  fi
  
  else
      echo "No Backup directory."
      exit
  fi
  </code></pre>
  
  <p>There are 2 specific things I can't get done:</p>
  
  <ol>
  <li>The find command doesn't delete any of the old files in the local backup dir.</li>
  <li>I would like mput to only put the .zip files that were created today. </li>
  </ol>
  
  <p>Thanks in advance for the help.</p>