Wordpress site backup with shell script

Home » Blog » How to perform complete WordPress site backup with shell script and cron job ?

How to perform complete WordPress site backup with shell script and cron job ?

A complete WordPress site backup is not always easy to perform when some of the folders to save are not in standard WordPress directories.

Here is a simple solution to achieve the task without any plugin.

I'm not a coder and all the lines below are the result of crawling over the net.
I've only adapted the code and changed some parameters for my needs.

Enable first SSH/ Shell access on your cpanel in order to be able to execute script.

wordpress site backup - Shell Access

If your SSH access is disabled, click on "Manage SSH access" to enable.

wordpress site backup - Manage Shell Access

In order to identify your WordPress path, go to cpanel and click on "File Manager".
You will see your root path in front of the the Home icon (WordPress path = root path/public_html/).
Don't forget to create your backup path by FTP if it does'nt exist.

wordpress site backup - File manager

Copy/ paste the content of the following script in notepad++, replace text in red color with your own informations and save it to a unix script .sh file (Backup.sh for example).
Then, tranfer this file by FTP to your root directory.

#!/bin/sh

#
# Customize these variables. You have to create backup path by ftp if it doesn't exist
#
EMAIL=xxx.yyy@yahoo.com
WORDPRESS_PATH="/home/website/public_html"
BACKUP_PATH="/home/website/wp_backups/website-backup"

#
# valid path test - Exit if wrong path
#[ ! -d $WORDPRESS_PATH ] && echo "Not a valid directory : $WORDPRESS_PATH" && exit 1[ ! -f $WORDPRESS_PATH/wp-config.php ] && echo "Cannot find wordpress config file 'wp-config.php'" && exit 1[ ! -d $BACKUP_PATH ] && mkdir -p $BACKUP_PATH

#
# Script variables
#
NOW=`date +%Y-%m-%d_%Hh%Mm%S`
WP_PARENT=`dirname $WORDPRESS_PATH`

#Create Backup sub-directory with date and time
mkdir $BACKUP_PATH/Backup-$NOW

# Entire public_html directory backup and compression
cd $WP_PARENT
tar zcf $BACKUP_PATH/Backup-$NOW/Global-Backup-$NOW.tar.gz public_html
cd $OLDPWD

# read wordpress wp-config.php file to extract DB Name, Username, Password and use it in variables
WP_CONFIG="$WORDPRESS_PATH/wp-config.php"
PROPS=$(sed -e "/define('DB_(NAME|USER|PASSWORD|HOST)/!d"
-e "s/[^']*'DB_(NAME|USER|PASSWORD|HOST)'[^']*'([^']*)'.*$/DB_1='2';/g" ${WP_CONFIG}) && eval $PROPS;

#
# Database backup and compression
#
mysqldump -p$DB_PASSWORD -u$DB_USER $DB_NAME > $BACKUP_PATH/Backup-$NOW/backup_$DB_NAME-$NOW.sql
gzip -v $BACKUP_PATH/Backup-$NOW/backup_$DB_NAME-$NOW.sql

#send an email when finished !
echo "Backup completed" | /usr/bin/mail -s "Wordpress and Mysql backups completed " $EMAIL

Change the file permissions of Backup.sh to 700 in order to be able to execute it and to perform your WordPress site backup.

wordpress site backup - Rights

To schedule script execution, go to your cpanel and click on Cron jobs icon.

wordpress site backup - Cron Jobs

Here are the parameters to perform one backup by day at midnight.
Enter the execution command /bin/sh /root path/your script file name and press Add New Cron Job button to valid.

wordpress site backup - Cron Job planification

Wordpress site backup is done !

wordpress site backup result

Complete wordpress site backup takes about 18 min (5.5 Go).
Don't forget to transfer the resulting files on your computer after backing up because most of host providers doesn't allow to keep backup files on their servers (you can automate this transfert with Syncbackfree, a free software that enable you to perform scheduled transferts and backups between ftp and your computer).

By |2016-10-19T08:56:26+00:00April 25th, 2016|Blog|5 Comments

About the Author:

5 Comments

  1. George Sheldon April 25, 2016 at 13:35 - Reply

    Great information! Thanks for sharing this information!

    • chrisroll April 25, 2016 at 13:43 - Reply

      You're welcome, George !

  2. ÿþJ May 31, 2016 at 04:01 - Reply

    Hi there, just became aware of your blog through Google, and found that it's truly informative. I am gonna watch out for brussels. I¡¯ll be grateful if you continue this in future. Numerous people will be benefited from your writing. Cheers!

    • chrisroll May 31, 2016 at 14:33 - Reply

      Thanks a lot 🙂

  3. minecraft September 5, 2018 at 07:41 - Reply

    This excellent post definitely has all of
    the information and facts I wanted concerning this subject and
    didn't know who to ask.

Leave A Comment