Bash Script to Sync WordPress Blog to Live Server

By | September 22, 2016

It’s handy to have a test server that we can use to see what will happen when we upgrade WordPress, install new plugins, or mess around with the layout and design of our blog. But how can we copy the changes we make to our test WordPress site to our live server? In this post, you will learn how to write a bash script to automate the synchronization of a test WordPress site to a live server.

There are two main things to consider when keeping a WordPress blog synchronized with another server:

  • The WordPress files and directories
  • The WordPress database

The script is broken down into four steps, and it allows you to run all of them in a row or do one at a time.

Let’s take a look at the entire script and then break it down into steps.

The entire script

#!/bin/bash

#DATE will be appended to the exported database files.
DATE=`date +%Y-%m-%d`

# If CONTINUE is 1, the script will go back to the main menu after each step is completed
CONTINUE=1

echo "###############################################################"
echo "# This script synchronizes the contents of your test server's #"
echo "# html directory and WordPress database to a cloud-based  	#"
echo "# virtual server.					 						    #"
echo "###############################################################"
echo "																 "
echo "  What would you like to do?									 "
echo "																 "
options=("All steps" "Export local database" "Sync web files" "Sync databases" "Import databases" "Exit")
select opt in "${options[@]}"

do
	case $opt in
		"All steps")
			# Set CONTINE to zero so that each step falls through to the next one until the script exits
			CONTINUE=0
			;& # fall through

		"Export local database")
			echo " "
			echo "###############################################################"
			echo "#STEP 1: Backup WordPress Database to DB_Backups directory    #"
			echo "###############################################################"
			echo "Will create backup /var/www/yourwebsite.com/DB_Backup/wordpress_$DATE.sql"
			read -p "Press enter to proceed."
			# Replace 10.0.1.200 with the IP address of your local MySQL server
			echo "SSHing to 10.0.1.200 to mysqldump the databases..."
			# Here we use SSH to log into the server and issue the command mysqldump to export the WordPress database. Comment and statistics tables are excluded using the --ignore-table option.
			ssh -l username 10.0.1.200 "mysqldump -u root --ignore-table=wordpress.wp_comments --ignore-table=wordpress.wp_statistics_exclusions --ignore-table=wordpress.wp_statistics_historical --ignore-table=wordpress.wp_statistics_pages --ignore-table=wordpress.wp_statistics_search --ignore-table=wordpress.wp_statistics_useronline --ignore-table=wordpress.wp_statistics_visit --ignore-table=wordpress.wp_statistics_visitor wordpress > /var/www/yourwebsite.com.com/DB_Backups/wordpress_$DATE.sql;"
			echo "DATABASE BACKUPS COMPLETE"
			# If we aren't doing all the steps, return to the main options menu
			if [ $CONTINUE = 1 ]; then
				read -p "Press enter to continue."
				continue
            fi
			# Otherwise, fall through to the next step
			;&

		"Sync web files")
			echo " "
			echo "###############################################################"
			echo "#STEP 2: Rsync Web Files With Cloud Server                   #"
			echo "###############################################################"
			read -p "Rsync web files with Cloud Server? (DRY RUN)"
			# Do a dry run of rsync first to check what changes will be made. Store the results in output.txt
			# Replace 172.16.0.1 with the IP address of your cloud server
			rsync --dry-run --exclude-from=do_exclude.txt --delete -tOrlvu -e ssh /home/username/data/Documents/yourwebsite.com.com/html/ username@172.16.0.1:/var/www/yourwebsite.com/html/ > /home/username/e_data/Documents/yourwebsite.com/output.txt
			echo " "
			# Show the results of the dry run in gedit
			gedit output.txt
			read -p "Rsync web files with Cloud Server? (REAL DEAL)"
			# If we're happy with the changes, continue with the real rsync.
			rsync --exclude-from=do_exclude.txt --delete -tOrlvu -e ssh /home/username/data/Documents/yourwebsite.com.com/html/ username@172.16.0.1:/var/www/yourwebsite.com/html/
			echo "WEB FILES SYNC COMPLETE"
			if [ $CONTINUE = 1 ]; then
				read -p "Press enter to continue"
				continue
            fi
			;&

		"Sync databases")
			echo " "
			echo "###############################################################"
			echo "#STEP 3: Rsync Database Backups With Cloud Server            #"
			echo "###############################################################"
			read -p "STEP 3: Rsync local DB_Backups with Cloud Server? (DRY RUN)"
			rsync --dry-run --delete -rltv -e ssh /home/username/data/Documents/yourwebsite.com/DB_Backups/ username@172.16.0.1:/var/www/yourwebsite.com/DB_Backups/
			read -p "Rsync local DB_Backups with Cloud Server? (REAL DEAL)"
			rsync --delete -rltv -e ssh /home/username/e_data/Documents/yourwebsite.com/DB_Backups/ username@172.16.0.1:/var/www/yourwebsite.com/DB_Backups/
			echo "DATABASE SYNC COMPLETE"
			if [ $CONTINUE = 1 ]; then
				read -p "Press enter to continue"
				continue
            fi
			;&

		"Import databases")
			echo " "
			echo "###############################################################"
			echo "#STEP 4: Import Database Backups into Cloud Server           #"
			echo "###############################################################"
			read -p "STEP 4: Import Database Backups into Live Website?"
			# Import the exported database into the live server
			ssh -l username 172.16.0.1 "mysql -u username -pPassword -D wordpress < /var/www/yourwebsite.com/DB_Backups/wordpress_$DATE.sql;
			echo "DATABASE IMPORT COMPLETE"
			echo " "
			if [ $CONTINUE = 1 ]; then
				read -p "Press enter to continue"
				continue
            fi
			read -p "Script complete. Press enter to exit"
			break
			;;

		"Exit")
			break
			;;

		# If the user inputs an invalid option
		*) echo invalid option;;

	esac
done

Step 1: Export local test database

In the first step, we export a copy of our test server’s WordPress database. We use SSH to log in to the server and issue the mysqldump command to export the database to a directory.

		"Export local database")
			echo " "
			echo "###############################################################"
			echo "#STEP 1: Backup WordPress Database to DB_Backups directory    #"
			echo "###############################################################"
			echo "Will create backup /var/www/yourwebsite.com/DB_Backup/wordpress_$DATE.sql"
			read -p "Press enter to proceed."
			# Replace 10.0.1.200 with the IP address of your local MySQL server
			echo "SSHing to 10.0.1.200 to mysqldump the databases..."
			# Here we use SSH to log into the server and issue the command mysqldump to export the WordPress database. Comment and statistics tables are excluded using the --ignore-table option.
			ssh -l username 10.0.1.200 "mysqldump -u root --ignore-table=wordpress.wp_comments --ignore-table=wordpress.wp_statistics_exclusions --ignore-table=wordpress.wp_statistics_historical --ignore-table=wordpress.wp_statistics_pages --ignore-table=wordpress.wp_statistics_search --ignore-table=wordpress.wp_statistics_useronline --ignore-table=wordpress.wp_statistics_visit --ignore-table=wordpress.wp_statistics_visitor wordpress > /var/www/yourwebsite.com.com/DB_Backups/wordpress_$DATE.sql;"
			echo "DATABASE BACKUPS COMPLETE"
			# If we aren't doing all the steps, return to the main options menu
			if [ $CONTINUE = 1 ]; then
				read -p "Press enter to continue."
				continue
            fi
			# Otherwise, fall through to the next step
			;&

Step 2: Synchronize files and directories

In step two, we use the command rsync to synchronize any changes to the website’s files with the live site. We start with a “dry run” of rsync, which shows up what changes will be made without actually doing anything. If we’re happy with the results, we can proceed with the real sync by simply removing the –dry-run option. Note the –exclude-from option in the rsync command. Here we can exclude files that we don’t want to upload to the production server. For example, wp-config.php, which contains server specific settings.

Check out my post on how to learn more about rsync and its options.

		"Sync web files")
			echo " "
			echo "###############################################################"
			echo "#STEP 2: Rsync Web Files With Cloud Server                   #"
			echo "###############################################################"
			read -p "Rsync web files with Cloud Server? (DRY RUN)"
			# Do a dry run of rsync first to check what changes will be made. Store the results in output.txt
			# Replace 172.16.0.1 with the IP address of your cloud server
			rsync --dry-run --exclude-from=do_exclude.txt --delete -tOrlvu -e ssh /home/username/data/Documents/yourwebsite.com.com/html/ username@172.16.0.1:/var/www/yourwebsite.com/html/ > /home/username/e_data/Documents/yourwebsite.com/output.txt
			echo " "
			# Show the results of the dry run in gedit
			gedit output.txt
			read -p "Rsync web files with Cloud Server? (REAL DEAL)"
			# If we're happy with the changes, continue with the real rsync.
			rsync --exclude-from=do_exclude.txt --delete -tOrlvu -e ssh /home/username/data/Documents/yourwebsite.com.com/html/ username@172.16.0.1:/var/www/yourwebsite.com/html/
			echo "WEB FILES SYNC COMPLETE"
			if [ $CONTINUE = 1 ]; then
				read -p "Press enter to continue"
				continue
            fi
			;&

Step 3: Synchronize databases

In step three, we take the database file we exported in step one and copy it to the live server using rsync again.

		"Sync databases")
			echo " "
			echo "###############################################################"
			echo "#STEP 3: Rsync Database Backups With Cloud Server            #"
			echo "###############################################################"
			read -p "STEP 3: Rsync local DB_Backups with Cloud Server? (DRY RUN)"
			rsync --dry-run --delete -rltv -e ssh /home/username/data/Documents/yourwebsite.com/DB_Backups/ username@172.16.0.1:/var/www/yourwebsite.com/DB_Backups/
			read -p "Rsync local DB_Backups with Cloud Server? (REAL DEAL)"
			rsync --delete -rltv -e ssh /home/username/e_data/Documents/yourwebsite.com/DB_Backups/ username@172.16.0.1:/var/www/yourwebsite.com/DB_Backups/
			echo "DATABASE SYNC COMPLETE"
			if [ $CONTINUE = 1 ]; then
				read -p "Press enter to continue"
				continue
            fi
			;&

Step 4: Import database into production server

In the final step, we import the database file which we exported from the test server.

		"Import databases")
			echo " "
			echo "###############################################################"
			echo "#STEP 4: Import Database Backups into Cloud Server           #"
			echo "###############################################################"
			read -p "STEP 4: Import Database Backups into Live Website?"
			# Import the exported database into the live server
			ssh -l username 172.16.0.1 "mysql -u username -pPassword -D wordpress < /var/www/yourwebsite.com/DB_Backups/wordpress_$DATE.sql;
			echo "DATABASE IMPORT COMPLETE"
			echo " "
			if [ $CONTINUE = 1 ]; then
				read -p "Press enter to continue"
				continue
            fi
			read -p "Script complete. Press enter to exit"
			break
			;;

After completing all four steps, the live server will be in sync with the test server.

Leave a Reply

Your email address will not be published. Required fields are marked *

*