Backup WordPress project files / DB using a bash script

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 the backup of WordPress project files & DB:
(If you want a backup script for Magento then please go here)

#@author		MagePsycho <>
#@version		0.1.0

#/************************ EDIT VARIABLES ************************/
#/************************ //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!"

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!"

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

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

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 an 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 different types of backup:
    • 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 the 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 the backup script?

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

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

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.

5 thoughts on “Backup WordPress project files / DB using a bash script”

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

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

  3. Hi Raj,

    Great script but there’s one issue I’m not sure how to overcome. We have passwords that contain “meaningful” characters such as # – and & to name a few. When we try to use your script with these passwords, mysqldump fails saying it can’t connect to the server. We’ve tried different escaping mechanisms but can’t seem to get anything that works. Your thoughts?

      • Thanks, we’ve tried that, but either we’re doing something wrong or mysqldump has issues with single quotes and passwords. For example, we’ve tried the following (pseudo code):


        Which, when echoed = -p’-1!.,34kso0′

        mysqldump -u $user $PASS -h $host $db

        fails with the somewhat obtuse error: mysqldump: Got error: 1046: No database selected when selecting the database

        We double-checked the name of the DB and ran the exact same command from the command line and everything works just fine. We’re stumped.

        Thanks again for the help!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.