A small bash backup script for WordPress

As most of you know I work as an Escalation Engineer at Combell.com. Recently I crafted a tiny backup script that makes backups of my own site. While this script is optimized for use on shared servers at Combell.com, it can easily be adapted to be used anywhere.

#!/bin/bash

#### Settings ####
NOW=$(date +"%Y-%m-%d-%H%M")
FULL_PATH=$(pwd)
BACKUP_FOLDER=$FULL_PATH/data/

#### Site-specific Info ####
SITE_PATH="www" #Could also be subsites/subsitename
DB_NAME=`cat $SITE_PATH/wp-config.php | grep DB_NAME | cut -d \' -f 4`
DB_USER=`cat $SITE_PATH/wp-config.php | grep DB_USER | cut -d \' -f 4`
DB_PASS=`cat $SITE_PATH/wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`
DB_HOST=`cat $SITE_PATH/wp-config.php | grep DB_HOST | cut -d \' -f 4`

#### Files backup ####

function files_backup {
    zip -r $SITE_PATH.$NOW.zip $SITE_PATH
    mv $SITE_PATH.$NOW.zip $BACKUP_FOLDER
}

#### Database Backup ####
function database_backup {
    mysqldump -h $DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME > $DB_NAME.$NOW.sql
    mv $DB_NAME.$NOW.sql data/$DB_NAME.$NOW.sql
}

#### Runner Class ####
files_backup
database_backup

This script can be run automatically on a Combell-server by adding a cron for this script. You can do this in /etc/crontab by adding for example this:

0 */6 * * * /bin/sh /data/sites/web/youraccountname/backup.sh

The cron above will create a backup every six hours. Do not forget to put this backup.sh script in the root of your account.

Hope this helps you guys.

PS: the FULL_PATH variable is already included since I’m planning to iterate on this script and add more features, even though it’s currently only used to declare the backup directory path.


Posted

in

by

Tags:

Comments

4 responses to “A small bash backup script for WordPress”

  1. Andros avatar

    Fantastic guidance, just what I needed to create my own version. Thanks for sharing your time. Happy DevOps!

    1. Brecht Ryckaert avatar

      Hi Andros

      Glad you found it useful. Thank you very much for your kind words. Have a great day!

  2. […] A small bash backup script for WordPress The simplest backup script I found […]

  3. Shine avatar

    Simple but useful script. I adjusted it to fit my own needs. Many thanks, Andros.

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.