A bash script for synchronizing WordPress databases between environments (production, staging, local) using WP-CLI and SSH.
- Interactive menu system for selecting source and destination databases
- Automatic URL replacement using WP-CLI search-replace
- SSH-based remote database operations via WP-CLI
- Lando support for local development environments
- Automatic cleanup of temporary database dumps
- Test connections before syncing
- Compatible with:
- Vanilla WordPress installations
- Bedrock-based WordPress projects
- Flywheel, SpinupWP, and other hosting providers
- WP-CLI installed on all remote servers
- SSH access configured with key-based authentication
- Lando (for local development)
- rsync for file transfers
-
Add syncdb as a git submodule to your project:
git submodule add git@github.com:campaignupgrade/syncdb.git dev/syncdb
-
Create a configuration file at
dev/config/syncdb.sh:cp dev/syncdb/options.sh.example dev/config/syncdb.sh
-
Edit
dev/config/syncdb.shwith your project settings:# SSH host names from ~/.ssh/config _production=mysite_prod _remotes=( mysite_stage mysite_dev ) # Database dump paths (no trailing slash) _local_path="dev/db" # Local path relative to project root _remote_path="db" # Remote path (usually relative to SSH login dir)
Add your remote servers to ~/.ssh/config:
Host mysite_prod
HostName ssh.example.com
User username+sitename
Port 22
IdentityFile ~/.ssh/mysite_rsa
The script looks for configuration in this order:
dev/config/syncdb.sh(recommended, version-controlled)dev/syncdb/options.sh(legacy, local copy)
Configuration variables:
_production: SSH host name for production server_remotes: Array of SSH host names for remote environments_local_path: Path to store database dumps locally_remote_path: Path to store database dumps on remote servers
Run the script from your project root:
./dev/syncdb/syncdb.sh- Sync database - Synchronize database from source to destination
- Test connections - Verify SSH and WP-CLI connectivity to all remotes
- Quit - Exit the script
When syncing a database:
- Select source (production or remote environment)
- Select destination (local or remote environment)
- Confirm the sync operation
- Script performs:
- Export database from source
- Download to local machine
- Upload to destination (if not local)
- Get URLs from source and destination
- Import database to destination
- Search-replace URLs
- Cleanup operations
- Delete temporary files
- Export: Uses
wp db exporton source server to create SQL dump - Download: Uses
rsyncto download dump to local machine - Upload: Uses
rsyncto upload dump to destination (if remote) - URL Detection: Retrieves home URLs from both source and destination
- Import: Imports SQL dump using:
lando db-importfor localwp db importfor remote destinations
- Search-Replace: Updates all URLs in database using
wp search-replace - Cleanup:
- Activates all plugins
- Flushes cache and rewrites
- Purges SpinupWP cache (if available)
- Clears Acorn views (if Bedrock/Sage)
- Removes all temporary SQL dumps
The script automatically deletes temporary database dumps:
- Local dump:
dev/db/syncdb.sql - Source dump:
{remote_path}/syncdb.sqlon source server - Destination dump:
{remote_path}/syncdb.sqlon destination server (if remote)
The script intelligently detects available features:
SpinupWP Cache Purging:
if wp cli has-command "spinupwp cache purge-site"; then
wp spinupwp cache purge-site
fiAcorn (Bedrock/Sage) Views:
if wp cli has-command "acorn view:clear"; then
wp acorn view:clear
wp acorn view:cache
fiThe _remote_path setting depends on where you land when SSH'ing into the server:
# When you SSH in, you land in /www
ssh mysite_prod
# Output: user@sitename:/www>
# Configuration:
_remote_path="db" # Creates /www/db/# When you SSH in, you might land in /home/user
ssh mysite_prod
# Output: user@hostname:~$
# Configuration:
_remote_path="public/db" # Creates ~/public/db/Test your path:
ssh mysite_prod "pwd && mkdir -p db && ls -ld db"Before syncing, test your connections:
./dev/syncdb/syncdb.sh
# Select: 2) Test connectionsThis verifies:
- SSH connectivity
- WP-CLI availability
- WordPress installation detection
- Remote directory creation permissions
- Verify SSH config in
~/.ssh/config - Test manual SSH:
ssh mysite_prod - Check SSH key permissions:
chmod 600 ~/.ssh/mysite_rsa
- WP-CLI not installed on remote server
- Check path:
ssh mysite_prod "which wp"
- Remote path doesn't exist or lacks permissions
- Verify:
ssh mysite_prod "pwd && mkdir -p db"
- Check local Lando is running:
lando info - Verify database credentials
- Check disk space
This version includes improvements for:
- ✅ Vanilla WordPress installations
- ✅ Bedrock-based projects
- ✅ Flywheel hosting
- ✅ SpinupWP hosting
- ✅ Conditional framework-specific commands
- Uses SSH key-based authentication (no passwords)
- Database dumps are temporary and automatically deleted
- Add
dev/db/*.sqlto.gitignoreto prevent accidental commits - Never commit SSH keys or credentials
This is a submodule shared across projects. To contribute:
- Make changes in the submodule directory
- Commit within the submodule
- Push to the syncdb repository
- Update parent projects to use new version
- Takahiro Noguchi - takahiro@campaignupgrade.org
- Scott LaMorte - scott@campaignupgrade.org
MIT License - see LICENSE file for details.
Copyright (c) 2025 Campaign Upgrade