Shell Script to Sync Local with Remote WordPress Sites

Written by
Typical Read
2 minutes

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.

backup_file=$(date +%Y-%m-%d-%H-%M-%S)
# 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

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!

2 comments on “Shell Script to Sync Local with Remote WordPress Sites”.


# Oct 16, 2020

Hello Ben! This is so awesome, I’ve been looking for this exact script, but I’m kind of a newbie here, how exactly do you store the script with all the specific data of the databases and server, so that one would only have to execute the "sh" command? Sorry if this is a dumb question, but as I stated before I’m kind of a newbie with shell. BTW I’m using iterm2 as my terminal app

# Dec 8, 2020

No problem, it’s stored in the file. Just update the variables with your values.

Join the conversation.

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

All comments posted on 'Shell Script to Sync Local with Remote WordPress Sites' are held for moderation and only published when on topic and not rude. Get a gold star if you actually read & follow these rules.

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.