Backup WordPress project files / db using bash script

August 8, 2013  |  2 Comments  |  by Raj (MagePsycho)  |  Linux, Magento

WordPress is undoubtedly the most popular blogging platform on the Internet. And when it comes to the maintenance or regular backups, life is not easier unless you have some backup script which works great via SSH & cron daemons.

Here is the custom backup script used for taking backup of wordpress project files & db:
(If you want backup script for Magento then please go here)

#!/bin/bash
#@author		MagePsycho <magepsycho@gmail.com>
#@website		http://www.magepsycho.com
#@blog			http://www.blog.magepsycho.com
#@version		0.1.0

#/************************ EDIT VARIABLES ************************/
projectName=magepsycho_blog
backupDir=/home/magepsycho/_backups
#/************************ //EDIT VARIABLES **********************/

fileName=$projectName-$(date +"%Y-%m-%d")
host=$(grep DB_HOST "wp-config.php" |cut -d "'" -f 4)
username=$(grep DB_USER "wp-config.php" | cut -d "'" -f 4)
password=$(grep DB_PASSWORD "wp-config.php" | cut -d "'" -f 4)
dbName=$(grep DB_NAME "wp-config.php" |cut -d "'" -f 4)

printf "What kind of backup you would like?\n[ d ] DB backup only\n[ f ] Files backup only\n[ b ] Files backup with DB\n"
read backupType
if [[ $backupType = @(d|b) ]]; then
	echo "----------------------------------------------------"
	echo "Dumping MySQL..."
	mysqldump -h "$host" -u "$username" -p"$password" "$dbName" | gzip > $fileName.sql.gz
	echo "Done!"
fi

if [[ $backupType = @(f|b) ]]; then
	echo "----------------------------------------------------"
	echo "Archiving Files..."
	tar -zcf $fileName.tar.gz * .htaccess
	echo "Done!"
	echo "----------------------------------------------------"
	echo "Cleaning..."
	rm -f $fileName.sql.gz
	echo "Done!"
fi

if [[ $backupType = @(d|f|b) ]]; then
	echo "----------------------------------------------------"
	mkdir -p $backupDir;
	echo "Moving file to backup dir..."
	if [[ $backupType == d ]]; then
		mv $fileName.sql.gz $backupDir
	fi

	if [[ $backupType = @(f|b) ]]; then
		mv $fileName.tar.gz $backupDir
	fi
	echo "Done!"
else
	echo "Invalid selection!"
fi

Or you can download it from here: [WordPress Backup Script]
Notes: If you get the following error:

syntax error in conditional expression: unexpected token `(‘
line 20: syntax error near `@(d’
line 20: `if [[ $backupType = @(d|b) ]]; then’

then this means you are using older version of bash (< 4.0). And you need to patch the script by adding the following line after bash script declaration:

shopt -s extglob

What does this script do?

1. Gives options for backup type which are:
- DB backup only
- Files backup only
- Files backup with DB
2. Dumps the database by taking db info from wp-config.php
3. Makes a copy of project files and compresses it in .tar.gz format.
4. Deletes the dumped SQL file as it's already copied in compressed file.
5. Creates backup dir if not exists
6. Copies the compressed project file to the backup dir.

Besides, you can also setup cron job to run this backup script at regular intervals.

How to run backup script?

1. Edit _wpbackup.sh to configure variables 'projectName' & 'backupDir'
2. Upload the edited _wpbackup.sh to the root of your WordPress installation
3. Run following series of commands in terminal:

cd /path/to/wordpress/root
chmod +x _wpbackup.sh
ex -sc $'%s/\r$//e|x' _wpbackup.sh
sh _wpbackup.sh

4. You will get the compressed backup file at backup dir

Snapshots for different backup types

DB Backup Only

DB Backup Only


File Backup Only

File Backup Only

Files Backup with DB

Files Backup with DB

Thanks for reading & sharing.

Posted in Linux, Magento and tagged , , , . Bookmark the permalink.

About Raj (MagePsycho)

Raj, the guy behind MagePsycho is a Zend PHP5 Certified Engineer, Magento Certified Developer, Magento Moderator / Freelancer with specialization in web applications (CMS, E-Commerce, ERP etc.). Catch him on: Twitter: @magepsycho Skype: magentopycho
  • Shaun O’Hagan

    I don’t like relying on backups of source files, who knows what you are actually backing up. Could be a hacked mass of nasties. Best practice in the corporate world with Java enterprise is for deployment of releases via version controlled builds. Only now is PHP starting to get anywhere the level we need with things like capistrano which is great.

    Database is a different mater but I would look again at your build and release approach and avoid backing up of wordpress and magento core files.

    Regards, Shaun.

  • Luke Barker

    thanks, this looks good, and I Disagree with previous comment that it is not useful to haev something like this – WordPress and PHP are low overhead and you can go more streamlined deploy if budget/case demands it easy enough. You will most likely have a decent snapshot locally of the site too so the nasties idea shouldnt cause a problem – just do a diff if it is a hack that you are recovering from. Capistrano and similar have been around for several years in PHP also.