• I poured a bunch of time into looking for a backup solution to automate backing up and restoring my website. I didn’t find anything so I put one together.

    Here is a blog post about why this became important:
    http://www.infrasupport.com/business-continuity-disaster-recovery-apollo-13-week/

    The backmeup.sh script below makes sense if you’re hosting your own WordPress site or if your hosting service gives you an environment with shell access. I tested it on my own stuff – I backed up my website, built another virtual machine and installed WordPress from scratch, and then followed my directions to restore it. It all worked. The idea is, dump the database and copy all the content to a directory tree on some other system. Keep a rotation of 5 copies. From here, you can restore to a new system if the old system hiccups. Or use it for upgrades – do a fresh install on a new system and restore your website to it and test the upgrade while the old website version stays in production.

    I’ve backed up to both a CIFS and NFS server and it could probably be tailored for other scenarios.

    Enjoy. Now I won’t feel guilty when I ask for help. 🙂 If the code below is a hassle to paste in, just get in touch with me via the Contact form on my website and I’ll get you a copy.

    #/bin/sh
    # backmeup.sh - Backup my WordPress website.
    # Keeps a rotation of 5 copies on the backup target.
    # Assumes a reachable NFS or other server(s) set up as backup targets
    # and your local mountpoint directory or directories exist(s).
    #
    # $1 is the name of the NFS server to use as your backup target.
    #
    # This example uses two NFS servers named nfsa and nfsb.  Adapt to suit
    # your circumstances.
    #
    # Set up cron jobs to run every day but Sunday to alternate
    # NFS servers like this:
    #
    # Minute hour DOM Month DOW
    # 01 17 * * 1,3,5 /usr/share/wordpress/wp-content/backmeup.sh nfsa > /usr/share/wordpress/wp-content/backmeup.log 2>&1
    # 01 17 * * 2,4,6 /usr/share/wordpress/wp-content/backmeup.sh nfsb > /usr/share/wordpress/wp-content/backmeup.log 2>&1
    #
    # Backup at 1 minute past 5 PM every Mon, Wed, Fri to nfsa.
    # Backup at 1 minute past 5 PM every Tue, Thu, Sat to nfsb.
    #
    # Set up cron entries based on above as you see fit.
    #
    #	Greg Scott
    #	Infrasupport Corporation
    #	http://www.infrasupport.com
    #
    
    MOUNTPT="/mnt/$1"
    # Local mountpoint where the backup target is mounted.
    
    REMOTE="${1}:/data/$1"
    # Remote share to mount
    
    TGT="${MOUNTPT}/website"
    # Backup target
    
    echo "Mounting $REMOTE at $MOUNTPT"
    mount -t nfs $REMOTE $MOUNTPT
    # Change this mount to suit your situation.
    # You can also adapt to use a CIFS share if more convenient.
    
    ####################################
    # Everything below should work without edits.
    
    echo "Rotating old names and setting up new target."
    rm -Rf ${TGT}5
    rmdir ${TGT}5
    mv ${TGT}4 ${TGT}5
    mv ${TGT}3 ${TGT}4
    mv ${TGT}2 ${TGT}3
    mv ${TGT}1 ${TGT}2
    mv ${TGT} ${TGT}1
    mkdir -pv ${TGT}/wp-content
    mkdir -pv ${TGT}/etc/httpd/conf.d
    mkdir -pv ${TGT}/etc/wordpress
    
    echo "Starting backup"
    
    date
    
    echo "Running mysqldump."
    echo ""
    echo "To restore - first create the database and user like this:"
    echo "(Database named wordpress and user named wordpress.)"
    echo "# mysql -u root"
    echo "Welcome to the MariaDB monitor.  Commands end with ; or \g."
    echo ""
    echo "create database wordpress;"
    echo "grant all privileges on wordpress.* to wordpress@localhost identified by 'wordpress';"
    echo "flush privileges;"
    echo "exit"
    echo "#"
    echo ""
    echo "Next, populate the new database like this:"
    echo "# mysql -u root wordpress < wordpress.sql"
    echo "Where \"wordpress\" is the name of the database."
    mysqldump wordpress > ${TGT}/wordpress.sql
    echo ""
    
    echo "Copying wp-content"
    rsync -a /usr/share/wordpress/wp-content/* ${TGT}/wp-content
    
    echo "Copying conf.d files."
    rsync -a /etc/httpd/conf.d/* ${TGT}/etc/httpd/conf.d
    
    echo "Copying /etc/wordpress/"
    rsync -a /etc/wordpress/* ${TGT}/etc/wordpress
    
    date
    
    echo "Umounting ${MOUNTPT}"
    umount ${MOUNTPT}
  • The topic ‘Here's a working backup script’ is closed to new replies.