Shell Script to Sync Local with Remote WordPress Sites

Often I sync local WordPress sites with remote ones. There’s various — freemium — plugins that do this, but instead of adding bloat & spammy third-party plugin ads I wanted a simple, one-command solution. Shell to the rescue! Here’s a simple script that syncs your local database & uploads directory with your remote — and stores backups!

Tired of manually downloading the database, importing to your local, replacing URLs, syncing uploads & creating backups? Me too! Check out this simple shell script to sync local with remote WordPress sites.

  1. Downloads a copy of the database from the remote server using mysqldump.
  2. Drops & recreates the local database.
  3. Imports the downloaded database from the remote server to the newly created local database.
  4. Replaces the URLs in the database using WP-CLI.
  5. Syncs the local uploads directory with the remote uploads directory.
  6. Creates a zipped backup of the uploads directory.

sync.sh

#!/bin/bash

local_db_name=LOCAL_DATABASE_NAME
local_db_host=LOCALHOST
local_db_pass=LOCAL_DATABASE_PASSWORD
local_url=LOCAL_URL
local_uploads=LOCAL_UPLOADS_DIR

remote_ssh_user=SSH_USERNAME
remote_ssh_host=SSH_HOST
remote_db_host=REMOTE_DATABASE_HOST
remote_db_user=REMOTE_DATABASE_USER
remote_db_pass=REMOTE_DATABASE_PASSWORD
remote_db_name=REMOTE_DATABASE_NAME
remote_port=18765
remote_url=REMOTE_URL
remote_uploads=REMOTE_UPLOADS_DIR

backup_file=$(date +%Y-%m-%d-%H-%M-%S)
sql_backup_loc=SQL_BACKUP_DIR
uploads_backup_loc=UPLOADS_BACKUP_DIR

# Dump the database
mysqldump -h $remote_db_host -u $remote_db_user -p$remote_db_pass $remote_db_name --verbose | bzip2 -c > $sql_backup_loc/$backup_file.sql.bz2

# Drop the local database & recreate
mysqladmin -h $local_db_host -u root -p$local_db_pass drop $local_db_name -f
mysqladmin -h $local_db_host -u root -p$local_db_pass create $local_db_name

# Import dumped database
bunzip2 < $sql_backup_loc/backup-$backup_file.sql.bz2 | mysql -u root -proot $local_db_name

# Replace URLs in database
wp search-replace $remote_url $local_url --verbose

# Sync uploads directory
rsync -rtvP --delete -e "ssh -p $remote_port" $remote_ssh_user@$remote_ssh_host:$remote_uploads $local_uploads

# Backup uploads directory
tar --create --gzip --file=$uploads_backup_loc/$backup_file.tgz $local_uploads
Quick Note: The find and replace URL database feature uses WP-CLI: Command line interface for WordPress. You’ll need to have this installed prior to running this script.

To execute, in terminal run: sh sync.sh

This probably will need to be altered slightly to fit your specific needs and setup, but should give you a good idea and head start on how to get something like this working for your WordPress sites.

I’m no shell expert by any means.

It’s my first attempt at putting together something like this so forgive me if there’s a better way of doing something — of even better, comment below and let me know so I can update it!

Leave a Reply

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

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

All comments are held for moderation and only published when on topic and not rude. You'll even get little stars if you do an extra good job.

You may write comments in Markdown. This is the best way to post any code, inline like `<div>this</div>` or multiline blocks within triple backtick fences (```) with double new lines before and after.

Want to tell me something privately, like pointing out a typo or stuff like that? Contact Me.

icon-search icon-link