Skip to content

Synchronize your WordPress site database and uploads between environments with WP-CLI aliases and rsync

Notifications You must be signed in to change notification settings

roots/sync-script

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Sync Script

Syncing Bedrock-based WordPress environments with WP-CLI aliases and rsync.

Installation

Create a new scripts/ directory in your Bedrock directory (site/) and place the sync script inside of there.

Make sure that sync.sh is executable (chmod u+x sync.sh).

WP-CLI must be installed on all environments (local and remote).

Configuration

Edit the variables at the top of sync.sh to match the settings for your environments:

  • DEVDIR — Local path to uploads directory
  • DEVSITE — Local dev URL
  • PRODDIRuser@hostname:/path/to/uploads/
  • PRODSITE — Production URL
  • STAGDIRuser@hostname:/path/to/uploads/
  • STAGSITE — Staging URL

The Kinsta version of the script is slightly different:

  • REMOTEDIRuser@hostname:/www/example_123/public/shared/uploads/
  • PRODPORT — Production port
  • STAGPORT — Staging port

WP-CLI aliases

WP-CLI aliases must be properly setup in order for the sync script to work. Open wp-cli.yml and setup the aliases for your environments.

Trellis WP-CLI aliases

# site/wp-cli.yml
path: web/wp

@development:
  ssh: [email protected]/srv/www/example.com/current
@staging:
  ssh: [email protected]/srv/www/example.com/current
@production:
  ssh: [email protected]/srv/www/example.com/current

Test the aliases to make sure they're working:

$ wp @development
$ wp @staging
$ wp @production

Trellis + Kinsta WP-CLI aliases

# site/wp-cli.yml
path: web/wp

@development:
  ssh: [email protected]/srv/www/example.com/current
@staging:
  ssh: [email protected]:54321/www/example_123/public/current/web
@production:
  ssh: [email protected]:12345/www/example_123/public/current/web

.gitignore

Open .gitignore in your Bedrock directory (site/) and add the following:

# WP-CLI
*_development*.sql

When you sync down to your local development environment a database backup is performed with wp db export. This helps you safely recover your database if you accidentally sync, and by making this modification to .gitignore you're ensuring that your local database export doesn't accidentally get commited to your git repository.

Slack notification

Uncomment the lines near the end of the script if you'd like to enable the Slack notification for when the sync directory is up or horizontal.

Make sure to create a new incoming webhook and updating the URL in the script, as well as the channel.

Usage

If your local development environment is a VM, don't run the sync script from inside of the VM — run it on your host machine.

Navigate to the site/scripts/ directory to use the sync script.

Possible sync directions:

# Sync production down to development
$ ./sync.sh production development

# Sync staging down to development
$ ./sync.sh staging development

# Sync development up to producton
$ ./sync.sh development production

# Sync development up to staging
$ ./sync.sh development staging

# Sync production to staging
$ ./sync.sh production staging

# Sync staging to production
$ ./sync.sh staging production

Skipping database or assets during sync

The --skip-db flag can be passed to skip syncing the database.

The --skip-assets flag can be passed to skip syncing assets.

# Sync production down to development
$ ./sync.sh --skip-db production development
# Sync production down to development
$ ./sync.sh --skip-assets production development

Local development without VM (Valet, etc.)

The --local flag can be passed to skip using WP-CLI aliases for development. This means that you can use the sync script on a local development setup such as Valet.

# Sync production down to development
$ ./sync.sh --local production development

Troubleshooting

Unable to connect to development

Make sure that your local development setup is up and running.

Unable to connect to production or staging

Make sure that you're able to successfully connect with a SSH connection with the same details configured for the same WP-CLI alias.

If your SSH connection doesn't fail, make sure WordPress is first already installed.

Support

Shoot me an email with any issues you run into.

You can re-download the latest version by visiting https://roots.io/product-links/.

About

Synchronize your WordPress site database and uploads between environments with WP-CLI aliases and rsync

Topics

Resources

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages