Sync WP Site Makefile Recipe
A quick recipe to sync your codebase and database from a local host to a remote one.
Make sure you have set up remote directory to sync with local.
Install Bedrock to your directory of choice (in this case to “bedrock-app”):
$ composer create-project roots/bedrock bedrock-app
- Configure Bedrock as needed. Pay attention to your local & remote paths.
Clone or download this repo to a directory inside your app. (ex. bedrock-app/wp-sync/sync.mk)
$ git clone https://github.com/lopadz/wp-sync
srdb.mkfiles to match your project needs.
When you’re ready to sync to remote,
cdto your project’s directory and run:
$ make -f wp-sync/sync.mk
- Double-check the paths and press y to run the makefile. Press any other key to exit.
If everything worked, it will open a new connection to the remote host and
cdyou into the project’s directory.
- Make sure your DB credentials in the remote .env (or wp-config.php) file have been configured.
Reset and/or import the synced db:
$ wp db reset
<p> or </p> <pre>$ wp db reset && wp db import db/DB_NAME_HERE.sql</pre>
srdb.mkhas been configured with old and new URLs.
Run this to search & replace URLs in the remote database:
$ make -f wp-sync/srdb.mk
Assumptions & other notes
- Both local and remote hosts have Bash shell and WP-CLI installed.
- SSH connection to remote host is set up and working.
- If SSH key has passphrase, it will need to be entered multiple times. 🤷
The makefile can be called from anywhere–the script will detect where it’s located and use relative paths to run the commands. However, when you call it, make sure you add the path relative to where you are located. For example,
If you are in
and the app is in
- then run:
<pre> $ make -f bedrock-app/wp-sync/sync.mk</pre>
- If you are in
The recipe exports the database to:
db/dev-DATABASE_NAME-YYYY-MM-DD-HHMMSS.sql. To change the path or filename, update lines 33, 55, 56.
- If you would like to completely mirror both local and remote directories, you could pass a –delete flag in lines 93 & 114. Be very careful with this flag. If paths are configured incorrectly, this could delete files not meant to be deleted.
If you only need to run a specific step in the
sync.mk script, run:
$ make -f wp-sync/sync.mk step_name_here
Available steps are:
- rsync_code: Syncs the codebase directory.
- dump_db: Exports the local db.
- rsync_db: Syncs the database directory.
- ssh_remote: SSH into the remote host.