WordPress Backup to Amazon S3 Script

This is an updated version of my WordPress Backup Script. The new version basically does the same thing: backup up a wordpress blog (actually any site that consists of files and a MySQL database). The new thing about the script is that instead of only saving the backup locally, it also uploads it to Amazon S3.

# (C) 2008 Guy Rutenberg
# This is a script that creates backups of my blog.
#no trailing slash
# end of configuration - you probably don't need to touch anything bellow
DUMP_NAME=${DB_NAME}-$(date +%Y%m%d).sql.bz2
echo -n "dumping database... "
mysqldump --user=${DB_USER} --password=${DB_PASS} --host=${DB_HOST} ${DB_NAME} \
 | bzip2 -c > ${BACKUP_DIR}/${DUMP_NAME}
if [ "$?" -ne "0" ]; then
	echo "failed!"
	exit 1
echo "done"
TAR_NAME=${BLOG_DIR##*/}-$(date +%Y%m%d).tar.bz2
echo -n "Creating tarball... "
tar -cjf ${BACKUP_DIR}/${BLOG_DIR##*/}-$(date +%Y%m%d).tar.bz2 ${BLOG_DIR}
if [ "$?" -ne "0" ]; then
	echo "failed!"
	exit 1
echo "done"
echo -n "Uploading SQL dump to Amazon S3... "
if [ "$?" -ne "0" ]; then
	echo "failed!"
	exit 1
echo "done"
echo -n "Uploading tarball to Amazon S3... "
if [ "$?" -ne "0" ]; then
	echo "failed!"
	exit 1
echo "done"

As with the older Amazon S3 backup script, this one also required you to install the s3cmd package.

The first six variables:


Are the pretty self explanatory and have the same meaning as in the old script.

The script has one new variable: S3BUCKET. This should be set to the S3 bucket to which the backups should be saved. If you want to add some prefix to the backups files on S3 this is the place.

The script keeps the backups both in the local backup directory and the Amazon S3.

17 thoughts on “WordPress Backup to Amazon S3 Script

  1. Pingback: Cloudalist » Blog Archive » Back up Wordpress to S3

  2. Pablo (OtakuPahp)

    Hi, your script is marvelous, but you have a problem with the code here:

    s3cmd put ${BACKUP_DIR}/${TAR_NAME} ${S3BUCKET}${tar_NAME}

    The TAR_NAME var is set as tar_NAME

    Once fixed works great.

    Thanks for the script

  3. Eric D. Brown

    Guy – Great job on this script. I now have a full backup solution for all my blogs.

    I’ve been using automator on mac to login and downdload the files from my various blogs but never found anything to do everything (DB & Files).

    This did the trick. Thanks!

  4. Argancel

    You made me discover s3cmd. Thank you so much!

    About your script, I think that an enhancement would be to send an email which warns if the script has failed.

    Also, you can check out this script, which detects automatically the database parameters:

    And please install wordpress plugin subscribe-to-comments so that readers can get updates by email for the comment thread of articles they comment. I would have loved to follow by email comments from this article, but I can’t!

  5. Guy Post author


    I think similar functionality (error reporting) can be achieved by the MAILTO feature in cron. By giving an email in the crontab, the output of the script is emailed to you. In case of an error it will be shown there (and easily spotted as the output is pretty short).

    Automatically figuring the parameters is a nice thing indeed (although it will make the script WordPress specific).

    It’s indeed worth trying to support this kind of stuff in the next version of the script.

    You can subscribe to the comments via the RSS feed just above the first comment. At first look subscribe-to-comments looks pretty complicated plugin, featuring subscription manager and stuff, so I’m a bit hesitating, and will need to check the plugin on some other blog of mine before deciding to install it on this one, the biggest problem that I have with such plugins that many times they don’t clean after themselves when they get uninstalled, they leave database entries which are difficult to find.

  6. Francis

    Thanks for this script! I made a few modifications and it works great on my host. I had some difficulties installing the s3cmd package due to the hosting restrictions.

    What I did is use the s3cmd package for ruby which worked beautifully after I modified your script slightly to match the required parameters.

    All my wordpress sites are now backed up automatically and daily using the modified script. Wonderful!

    Thanks again!

  7. Jeff

    When I run the script, I get the following error:

    dumping database… ./backup-owj.sh: line 21: /recreation/backups/admin001_recreation-20091201.sql.bz2: No such file or directory

    mysqldump: Got errno 32 on write


    The only changes I made were the the parameters at the top. Do I need to specify separate credentials to write to the backups folder? Presently it set to 777 (and was 755).

  8. Jeff

    Oh, and I’m running the script locally on my Ubuntu machine. The target server where blog is hosted is on CentOS (if any of this matters).

  9. Pingback: Hacer respaldo automatizado de WordPress usando Amazon S3

  10. Muzo

    is it possible to add a function that deletes the previous backup from both s3 and the local folder after uploading to s3?

  11. Guy Post author

    Currently such functionality isn’t provided by the script. But if you know bash (or willing to learn) it shouldn’t be too complicated.

  12. Diego

    Hi Guy, i am having a problem because the script its trying to back up the cache folders from a wp-super-cache directory.

    How can i exclude that directory from the backup?

  13. Guy Post author

    You can exclude by modified the tar line and adding

    --exclude wp-cache

    to its arguments (replace wp-cache with the name of the folder your cache plugin saves it’s data to).

  14. Pingback: WordPressハッカーズ

Leave a Reply

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