Backup an FTP site using wget

The following script will first call a PHP script to dump all MySQL tables and then connect to an FTP server to download all files for backup. Finally, the folder will be compressed. Inspiration came from cyberciti.biz and vancelucas.com.

backup_ftp.sh
#!/bin/sh
BACKUPDIR=/storage/.backup
LOGFILE=backup.log
FTP_SERVER=ftp.example.org
FTP_USERNAME=mbirth
FTP_PASSWORD=password
 
if [ ! -d $BACKUPDIR ]; then
    exit 1
fi
cd $BACKUPDIR
 
# Dump MySQL database on server
wget -S -O - --progress=dot:mega http://www.example.org/dumpdb.php > $LOGFILE  2>&1
 
# Download all contents
wget -r -N -x -S -l inf -a $LOGFILE --user="$FTP_USERNAME" --password="$FTP_PASSWORD" ftp://$FTP_SERVER/
WGETCODE=$?
 
NOW=`date "+%Y-%m-%d_%H%M%S"`
TARFILE="${FTP_SERVER}_${NOW}.tar.bz2"
tar cvjf "$TARFILE" $FTP_SERVER
TARSIZE=`stat --printf "%s" "$TARFILE"`
echo "Backup of $FTP_SERVER done. Wget exited with $WGETCODE. Filename: $TARFILE ($TARSIZE Bytes)"

Problems

In this variant, the folder with all the files will be kept after compressing it. This way, wget only downloads changed files upon the next run. A big drawback is, that files you delete on the server will still be there on your harddisk. This problem - together with a possible solution - is also described here.

My solution for now is to delete the downloaded folder once in a while and thus forcing a complete re-download of everything.