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.
- Downloads a copy of the database from the remote server using mysqldump.
- Drops & recreates the local database.
- Imports the downloaded database from the remote server to the newly created local database.
- Replaces the URLs in the database using WP-CLI.
- Syncs the local uploads directory with the remote uploads directory.
- 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!
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 sync.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 sync.sh file. Just update the variables with your values.
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.