title: Bash FTP Backup Script
body: <p>I run the following script on my server:</p>
  
  <pre><code>#!/bin/bash
  # System + MySQL backup script
  # Full backup day - Fri (rest of the day do incremental backup)
  # Copyright (c) 2005-2006 nixCraft &lt;http://www.cyberciti.biz/fb/&gt;
  # This script is licensed under GNU GPL version 2.0 or above
  # Automatically generated by http://bash.cyberciti.biz/backup/wizard-ftp-script.php
  # ---------------------------------------------------------------------
  
  ### System Setup ###
  DIRS="/var/www /etc"
  BACKUP=/tmp/backup.$$
  NOW=$(date +"%d-%m-%Y")
  INCFILE="/root/tar-inc-backup.dat"
  DAY=$(date +"%a")
  FULLBACKUP="Fri"
  
  ### MySQL Setup ###
  MUSER="xxx"
  MPASS="xxx"
  MHOST="localhost"
  MYSQL="$(which mysql)"
  MYSQLDUMP="$(which mysqldump)"
  GZIP="$(which gzip)"
  
  ### FTP server Setup ###
  FTPD="/backup/incremental"
  FTPU="xxx"
  FTPP="xxx"
  FTPS="xxx"
  NCFTP="$(which ncftpput)"
  
  ### Other stuff ###
  EMAILID="xxx"
  
  ### Start Backup for file system ###
  [ ! -d $BACKUP ] &amp;&amp; mkdir -p $BACKUP || :
  
  ### See if we want to make a full backup ###
  if [ "$DAY" == "$FULLBACKUP" ]; then
    FTPD="/backup/full"
    FILE="fs-full-$NOW.tar.gz"
    tar -zcvf $BACKUP/$FILE $DIRS
  else
    i=$(date +"%Hh%Mm%Ss")
    FILE="fs-i-$NOW-$i.tar.gz"
    tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS
  fi
  
  ### Start MySQL Backup ###
  # Get all databases name
  DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
  for db in $DBS
  do
   FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
   $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 &gt; $FILE
  done
  
  ### Dump backup using FTP ###
  #Start FTP backup using ncftp
  ncftp -u"$FTPU" -p"$FTPP" $FTPS&lt;&lt;EOF
  mkdir $FTPD
  mkdir $FTPD/$NOW
  cd $FTPD/$NOW
  lcd $BACKUP
  mput *
  quit
  EOF
  
  ### Find out if ftp backup failed or not ###
  if [ "$?" == "0" ]; then
   rm -f $BACKUP/*
   T=/tmp/backup.pass
   echo "Date: $(date)"&gt;$T
   echo "Hostname: $(hostname)" &gt;&gt;$T
   echo "Backup passed" &gt;&gt;$T
   mail  -s "BACKUP PASSED" "$EMAILID" &lt;$T
   rm -f $T
  else
   T=/tmp/backup.fail
   echo "Date: $(date)"&gt;$T
   echo "Hostname: $(hostname)" &gt;&gt;$T
   echo "Backup failed" &gt;&gt;$T
   mail  -s "BACKUP FAILED" "$EMAILID" &lt;$T
   rm -f $T
  fi
  </code></pre>
  
  <p>Everything is working well, except that it always tells me everything went well, even tho the remote ftp server is full. Can someone help me changing this, so it gives me an error, when the remote server is full?
  Also, is there any way to remove old backups?</p>
  
  <p>Thanks,
  Daniel</p>