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.


 
snippets/sh/backup-ftp-site.txt · Last modified: 2010-01-15 14:35.46 by mbirth
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki Contents powered by Club-Mate Contents powered by BassDrive.com Labelled with ICRA