WordPress Backup Shell Script

For those seeking to backup their WordPress installations on a nightly basis, I have decided to publish the following shell script.

Below, any instance of a tilde [~] indicates /home/[username]/. Referencing to user root is key to this. You could change it to /home/[username]/ if you so chose.

#!/bin/csh -f

# Determine current date
setenv CURDATE date +%Y%m%d

# Backup all WP databases to compressed, dated file in home directory
mysqldump -q -e -hlocalhost -u[user] -p[pass] [database] | gzip - > ~/${CURDATE}_db-backup.sql.gz

# Backup WP files to compressed, dated file in home directory
cd ~/path/to/wp-files/
tar cf - . | gzip - > ~/${CURDATE}_files.tar.gz

Any data in [brackets] should be filled in, sans brackets.

This should sit in userroot, chmod’d to 755 to be executable.

You will get errors for all files in wp-uploads on the second TAR run for the backups. If you don’t want backups of your WP files but are more concerned about the backup of the database, remove everything from the last comment on down.

I personally get an email sent to me to verify that there were no errors in the running of the script. I also use FTP Voyager’s scheduler to do a Move Down on the backups to a local machine. When I can get a RAID box running at home, I’ll be dumping it to that, since I trust RAID more than a single-point-of-failure HD machine. :)

14 Responses to “WordPress Backup Shell Script”

  1. The Indiana Jones School of Management Says:

    Carving a Side of Beef
    Well, I’ve migrated to WP 1.2 here in the last couple of minutes. It’ll take far longer to write this entry than it did to make the update.

    The IJSM.org WP Upgrade Process

    My first process was to run my backup shell script so that I had a g…

  2. Blogging Pro Says:

    WordPress Backup Shell Script

    GFMorris.org has a WordPress backup shell script for those seeking to backup their WordPress installations on a nightly basis. This includes the files and MySQL database….

  3. Weblog Tools Collection » WP Backup Shell Script Says:

    [...] 05 WP Backup Shell Script Categories - WordPress Hack LinkyLoo — Mark WP Backup Shell Script After Photomatt’s recent calls for backup, I [...]

  4. skebrown » Blog Archive » Just Back Up WP Says:

    [...] tor

    Just Back Up WP

    Matt said Just Back Up, so here is a shell script to help you along.

    This en [...]

  5. VesperDEM Says:

    I’m not much of a shell scripter, but I have been having problems getting this script to work. The first problem I was having was with the SETENV statement. I fixed this by changing it to:
    set CURDATE = `date +%y%m%d`

    My current problem has to do with ‘mysqldump’. I keep getting this error message:
    mysqldump: Got error: 1045: Access denied for user: ‘davescha_wrdp1@localhost’ (Using password: YES) when trying to connect

    I have everything correct, yet the thing keeps telling me that I can’t access the DB to back it up. I have tried several variations of what was in the script. They all report the same error.

    I can access the DB fine from phpMyAdmin and WP.

    Any thoughts?

  6. Geof F. Morris Says:

    It seems to me that your host has disallowed you as a user to use mysqldump [assuming, of course, that you're being hosted ;)]. That you had to change the environment doesn’t hugely surprise me; thanks for posting the SETENV statement that works for you.

    Sorry that it’s not working out for you. :(

    GFM

  7. VesperDEM Says:

    Thank you, that was the question that I needed to help me figure out the mysqldump part. The user isn’t DB_USER, it’s ACCOUNT_USER! I kept putting in the username I used for the database. I thought I had actually tried that combo when I was trying to get it to work earlier. Oh well…

    I made the adjustment and now it’s working perfectly.

  8. Weblog Tools Collection » WP Backup to Email Script Says:

    [...] Email Script Categories - Cool Scripts WordPress Hack — Mark Ever since Geoff wrote the small script to backup a Wordpress installation automatically [...]

  9. The Indiana Jones School of Management Says:

    Email WP DB Backups to Yourself

    Mark Ghosh has extended my original WordPress backup shell script to email you the sqldump and then delete the temporary backups. Sweet. This comes recommended if you don’t have access to a computer [as I do] that stays on 24/7 and can do once-night…

  10. Prashanth Rao’s Weblog » Blog Archive » Wordpress backup python script Says:

    [...] p python script #!/usr/bin/env python # # Ported by Christian Wilcox from shell script found at: # http://gfmorris.org/archives/2004/04/29/wordpress-backup-shell-script/ # # Make sure to set appropriate values to vars dictionary import os import ti [...]

  11. codefuture.net thought repository Says:

    Wordpress backup python script

    #!/usr/bin/env python
    #
    # Ported by Christian Wilcox from shell script found at:
    # http://gfmorris.org/archives/2004/04/29/wordpress-backup-shell-script/
    #
    # Make sure to set appropriate values to vars dictionary
    import os
    import time
    vars = {
    &#8…

  12. Shout Head :: How to setup multi-blogging with wordpress 2.0 Says:

    [...] I use this shell script run as a cron job and save the files the same way. [...]

  13. Message in a bottle…. : Blog Archive : Script per fer una copia de seguretat diaria del WordPress Says:

    [...] gfmorris he trobat aquest script de shell per a fer una copia de seguretat d’un blog amb WordPress. [...]

  14. Abhishek Says:

    Hi ,

    I am new to shell scripting but i am stuck with the following issue , as i want to catch a python script error and if there is an error then echo eyx message:
    the shell script i wrote is
    #!/bin/sh

    case “xyz” in
    get_xml)
    cd xyz
    python2.5 hello_xml.pyc;
    if [[ $? -ne "1" ]]; then
    echo “ERROR”
    exit
    else
    echo “Done”
    fi
    ;;

    but issue is even the python script hello_xml.pyc throws an error the shell IF statement is not catching it , it always echo “done” only!!! in both case of failure and success.
    Can some one point me to correct direction.

Leave a Reply