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!
Published
Updated
Typical Read
2 minutes
Sponsored
Help support open-source projects & pro-bono non-profit services by donating today.
Find your next web developer job
jobs by Indeed

Shell Script to Sync Local with Remote WordPress Sites was originally published on Sep 18, 2019 and has recently been updated on Aug 5, 2020 to reflect emerging trends.

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!

Did you find Shell Script to Sync Local with Remote WordPress Sites useful? Get articles in your inbox.

…and don’t worry, I hate spam as much as you. Expect to hear from me at most once a week.

Latest Job Postings
Posted on Aug 9, 2020 at 9:17pm
Full-time
Los Angeles
Posted on Aug 9, 2020 at 9:15pm
Full-time
Los Angeles
Posted on Aug 9, 2020 at 1:19am
Littleton, CO
jobs by Indeed
Sponsored
Need help with website, a boost in ranking or online marketing? Contact me today for a free quote.
Sponsored
Do you have a great article to share you want to contribute? Contact me about your idea.
Sponsored
Sponsor my site by donating to help support open-source projects, like WordPress Zero Spam, Referrer Analytics & others.
Sponsored
Sponsor my site by donating to help support open-source projects, like WordPress Zero Spam, Referrer Analytics & others.
Sponsored
Sponsor my site by donating to help support open-source projects, like WordPress Zero Spam, Referrer Analytics & others.

Leave a Reply

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.