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 <[email protected]>
#@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.