A bash script that helps you
- sync your WordPress database from production or staging to your local dev environment
- deploy your local core, plugins, mu-plugins and theme to production or staging
- run common tasks through wp-cli on the remote server to ensure your deploy works as expected
- tested on OSX and Linux systems
Note
For less technical users I recommend to use a plugin instead. For database migrations, I'd recommend WP Migrate. For deployment, I'd recommend good old (S)FTP.
- WP-CLI installed on your local machine. On the remote server, wp-sync-deploy takes care of installing WP-CLI automatically.
- A WordPress directory structure similar to this (adjustable through a
.env.wp-sync-deploy
file):
.
βββ content # your WordPress content folder (equivalent to the standard wp-content)
β βββ plugins
β βββ themes
β βββ ...
βββ core # your WordPress core folder (wp-admin, wp-includes, ...)
βββ index.php # main WordPress entry file
βββ wp-config.php # your wp-config file
Tip
While it's easy to setup the custom directory structure yourself, I'd recommend to use a framework like Bedrock, WPStarter or wordplate. All of these provide amazing convencience features for modern WordPress development.
# CD into your project's root folder
cd /path/to/your/root
# Clone this repo
git clone git@github.com:hirasso/wp-sync-deploy.git
# Make sure the scripts are exectutable
chmod +x ./wp-sync-deploy/*.sh
Alternatively, you can install this script as submodule:
git submodule add git@github.com:hirasso/wp-sync-deploy.git
If you want to clone your main repo and already have wp-sync-deploy as a submodule, use this command:
git clone --recurse-submodules git@github.com:yourname/your-repo.git
Run this script:
./wp-sync-deploy/setup.sh
This will move the required configuration files to your current working directory and remove the .example
part. You should now have these two files in your working directory:
This file holds all information about your various environments (local, staging, production). Make sure you add .env.wp-sync-deploy
to your .gitignore
file! Otherwise, it's possible that sensitive information makes it into your repo.
VSCode can syntax highlight the env file for you.
This file is being used to run automated tasks after deployment. You can adjust this file as you wish or delete it if you don't want it to be executed.
wp-sync-deploy performs a few security checks before proceeding with a deploy:
- Do all directories marked for deployment actually exist in both environments (locally and remotely)?
- Does a hidden file
.allow-deployment
exist on the remote environment's web root? - Does the local command-line PHP version match the one on the remote environment?
- Does the local web-facing PHP version match the one on the remote environment?
So when you are starting, you will need to
- Perform the first deployment manually
- Add an empty file
.allow-deployment
to your remote web root - Make sure that your local and remote server are set to use the same PHP version
# sync the database from your production server
./wp-sync-deploy/sync.sh production
# sync the database from your staging server
./wp-sync-deploy/sync.sh staging
# push your local database to your staging server
./wp-sync-deploy/sync.sh staging push
Note
Syncing your local database is only possible to the staging server by default. If you are sure you know what you are doing, you can also enable syncing to the production server.
# deploy your files to your production server (dry)
./wp-sync-deploy/deploy.sh production
# deploy your files to your staging server (dry)
./wp-sync-deploy/deploy.sh staging
# deploy your files to your production server (non-dry)
./wp-sync-deploy/deploy.sh production run
# deploy your files to your staging server (non-dry)
./wp-sync-deploy/deploy.sh staging run
wp-sync-deploy will automatically run tasks on the target server when you sync or deploy. Modify the wp-sync-deploy.tasks.php
file created by the setup script, to customize which tasks should be executed.
Default tasks defined in the file are:
- Optionally delete all transients
- Optionally delete your static cache
- When deploying: Optionally update the rewrite rules
wp-sync-deploy has a default list of files and directories that will be ignored during a deploy. If you wish to customize this list, you can do so by modifying the file .deployignore.