Skip to content


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. :)

Popularity: 92% [?]

Posted in Shell Scripts, WordPress.


15 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. 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?

  2. 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

  3. 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.

  4. 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.

Continuing the Discussion

  1. The Indiana Jones School of Management linked to this post on 5/24/2004

    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 linked to this post on 2/25/2005

    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 linked to this post on 3/4/2005

    [...] 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 linked to this post on 3/4/2005

    [...] tor

    Just Back Up WP

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

    This en [...]

  5. Weblog Tools Collection » WP Backup to Email Script linked to this post on 3/8/2005

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

  6. The Indiana Jones School of Management linked to this post on 3/8/2005

    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…

  7. Prashanth Rao’s Weblog » Blog Archive » Wordpress backup python script linked to this post on 4/4/2005

    [...] 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 [...]

  8. codefuture.net thought repository linked to this post on 3/11/2006

    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…

  9. Shout Head :: How to setup multi-blogging with wordpress 2.0 linked to this post on 6/18/2006

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

  10. Message in a bottle…. : Blog Archive : Script per fer una copia de seguretat diaria del WordPress linked to this post on 3/31/2007

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

  11. links for 2010-05-17 | Pratyush Kotturu - KE5YQZ linked to this post on 5/17/2010

    [...] Tutorial 22 hours agobackup wordpress db and files shell script – Bash – Snipplr 22 hours agoWordPress Backup Shell Script | GFMorris.org 22 hours agoA graduate school survival guide: "So long, and thanks for the Ph.D!" [...]



Some HTML is OK

or, reply to this post via trackback.