WordPress Backup to FTP

Update: A newer version of the script is available.

This script allows you to easily backup your WordPress blog to an FTP server. It’s actually a modification of my WordPress Backup to Amazon S3 Script, but instead of saving the backup to Amazon S3 it uploads it to an FTP server. Another update is that now the SQL dump includes the database creation instructions so you don’t need to create it manually before restoring from the backup.

Although I’ve written it with WordPress in mind (to creates backups of my blog), it isn’t WordPress specific. It can be used to backup any website that consists of a MySQL database and files. I’ve successfully used it to backup MediaWiki installation.

#!/bin/bash
 
# (C) 2008 Guy Rutenberg
# This is a script that creates backups of my blog.
 
DB_NAME=
DB_USER=
DB_PASS=
DB_HOST=
 
BLOG_DIR=
BACKUP_DIR=

FTP_HOST=
FTP_USER=
FTP_PASS=
FTP_BACKUP_DIR=
 
# end of configuration - you probably don't need to touch anything bellow
BLOG_DIR=`dirname "$BLOG_DIR"`/`basename "$BLOG_DIR"`
BACKUP_DIR=`dirname "$BACKUP_DIR"`/`basename "$BACKUP_DIR"`

echo -n "dumping database... "
DUMP_NAME=${DB_NAME}-$(date +%Y%m%d).sql.bz2
mysqldump --user=${DB_USER} --password=${DB_PASS} --host=${DB_HOST} \
 --databases ${DB_NAME} \
 | bzip2 -c > ${BACKUP_DIR}/${DUMP_NAME}
if [ "$?" -ne "0" ]; then
	echo "failed!"
	exit 1
fi
echo "done"
 
echo -n "Creating tarball... "
TAR_NAME=${BLOG_DIR##*/}-$(date +%Y%m%d).tar.bz2
tar -cjf ${BACKUP_DIR}/${BLOG_DIR##*/}-$(date +%Y%m%d).tar.bz2 ${BLOG_DIR}
if [ "$?" -ne "0" ]; then
	echo "failed!"
	exit 2
fi
echo "done"
 
echo -n "Uploading SQL dump and tarball to FTP... "
lftp -u ${FTP_USER},${FTP_PASS} ${FTP_HOST} <<EOF
cd "${FTP_BACKUP_DIR}"
put "${BACKUP_DIR}/${DUMP_NAME}"
put "${BACKUP_DIR}/${TAR_NAME}"

EOF
if [ "$?" -ne "0" ]; then
	echo "failed!"
	exit 3
fi
echo "done"

The configuration is done using the variables in the beginning of the script (until the “end of configuration” line). They are pretty self explanatory, and you should adjust them for your site before using the script. If there are variables you don’t understand what they do, see the post about the previous script, and don’t hesitate to comment.

32 thoughts on “WordPress Backup to FTP”

  1. גיא שלום וברכה,
    האם חייבים גישת SSH לשרת כדי להריץ את התסריט?
    Hello Guy,
    Should i must have a SSH access to the server to run this script?

  2. Hi Ariel,

    SSH access could make things a lot easier for you. But if you lack SSH access to your serve, there might still be some possible workarounds.

    The simplest one would be to create some CGI script that calls the backup script, and then using a cronjob and wget from your computer, regularly execute the script to create backups.

    If you have server side cron jobs, you can just upload the script and use the server’s cronjobs.

    It’s hard for me to elaborate and be more specific as I don’t know what kind of access you do have to the server and what sever-side tools/software are at your disposal.

    Anyhow, I hope I’ve been of help, and don’t hesitate to comment if you’ve further questions.

  3. Hey Guy, thank you so much. This works great. One question. How do I make it delete the tarballs after FTP is successful OR some thing to clean up files older than X days?

  4. Hi Lynette, I usually prefer to keep a copy of my backups on the server as an extra precaution. But if you want them to be removed after a successful FTP upload, just append the following to the script:

    rm -f "${BACKUP_DIR}/${DUMP_NAME}"
    rm -f "${BACKUP_DIR}/${TAR_NAME}
    
  5. Hello Guy.

    This is just what I have been looking for. Thanks for providing this cool script. I hope to use it to back up a few friends websites that host on Godaddy servers. I created my file backup.sh am getting errors when I run the script from cron though, here is what I get:

    /home/content/t/h/e/thesite/html/backups/backup_tool.sh: line 2: : command not found
    /home/content/t/h/e/thesite/html/backups/backup_tool.sh: line 5: : command not found
    /home/content/t/h/e/thesite/html/backups/backup_tool.sh: line 10: : command not found
    /home/content/t/h/e/thesite/html/backups/backup_tool.sh: line 13: : command not found
    /home/content/t/h/e/thesite/html/backups/backup_tool.sh: line 18: : command not found
    /home/content/t/h/e/thesite/html/backups/backup_tool.sh: line 22: : command not found
    dumping database… /home/content/t/h/e/thesite/html/backups/backup_tool.sh: line 27: bzip2: command not found
    mysqldump: Got errno 32 on write
    failed!

    I would love to use this, is there anything I can edit to make it work? Is GoDaddy missing bzip2? That is what I gather…

    Thanks for sharing your hard work and any help you can give me would be great.

  6. Hi Christopher,

    From your comment it looks to me like you are running the script on a very limited shell (it doesn’t support empty lines 😉 ). I used to have a shared hosting account in GoDaddy couple of years ago and I don’t recall having a real shell access there.

    You must have an access to a decent shell in order to use the script, so GoDaddy started offering one, I’m afraid the script won’t run on there server.

  7. Guy, I just wanted to drop in here to say thanks. This has been very helpful and one of the most reliable method I’ve used.

  8. Hello! I would love to use this script, and this is a noobie question… But how I should use that script? What language is it written with? Should I save it to a .PHP file, run it from Ubuntu command line, save it as a WORDPRESSPLUGIN, or what??

    Thanks a lot for the answer in advance!
    – Jupstejuho

  9. Hello again! I am very sorry to bother you, but I still don’t understand how to run this script. I tried copypasting the text to ubuntu command line (I am using Ubuntu 8.1) but it closed after two seconds. Then I tried saving it to a script.sh file and I dragged it to command line, but it said “access denied”. Could you please be sooo kind to give me step-by-step instructions on how to run this script properly? I would be really happy if you just could!

    Thanks A LOT in advance,
    Jupstejuho

  10. After saving the script to a file (e.g. script.sh) you need to allow executing it. For example in the command line:

    chmod +x script.sh
    

    Afterwards you should be able to execute the script.

  11. Hello Guy,

    thanks for this script, should be a standard in WP….

    Unfortunately the script creates the following message on a Gentoo server after creating the tarball:

    file changed as we read it
    failed!

    As I am a WP user and not a coder, this leaves me puzzled….any hints much appreciated!

    Regards,
    Andy

  12. Hi,

    The error you’re describing doesn’t sound specific to Gentoo. It happened because the files in the wordpress dir where changed while you made the backup. My guess is that you use some kind of cache plugin that accidentally changed it files right during the upgrade or that you’ve uploaded a file during it.

    Does it always happen? What cache plugin do you use?

  13. im confused on how to do the path on these care to elaborate, if my file is in /public_html/site.com directory?

  14. @Jason, I can’t recommend in person any other command line ftp client. But I believe you can easily adjust the script to any client that is installed on your host.

  15. sorry, also dumb question here… as far as running the script goes. where should the .sh file be located? in the root of your site? also, by running the script by typing ./script.sh will that trigger the backup or should the full path to that script be included as well (ie. ./var/www/vhosts/mysite.com/httpdocs/script.sh)? any help is much appreciated and sorry for the noobiness!

  16. figured it out! 2 questions though: what is the script doing when it reads “Removing leading ‘/’ from member names”? and is there any way to automate this script to run daily or hourly? thank you so much for posting this!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.