This script syncs the files and database from the production site/server of a Wordpress install to a local or staging copy. It assumes a few things...
- You have ssh access between the local/staging and remote machines and they both have WP CLI installed globally
- You have added an additional-rsync-excludes.txt or (additional-rsync-excludes-staging.txt or additional-rsync-excludes-local.txt) file to reflect any files and/or folders that you don't want synced from the remote for this specific environment
- The .env.wpenvsync, and if applicable additional-rsync-excludes-local-override.txt, files are gitignored
- If using composer, your composer.json file is in the wp root
If you are migrating from v2.x to v3.x. A few important breaking changes have been made...
- The main script has been renamed from
sync-prod.shtowp-env-sync.sh - All other files aside from the main script and the
.env.wpenvsyncfile now belong in awp-env-syncfolder which should be in the same location at thewp-env-sync.shand.env.wpenvsyncfiles... the Wordpress root directory
add these things to your composer.json file...
"repositories": [
{
"type": "vcs",
"url": "git@github.com:reggieofarrell/wp-env-sync.git"
}
],
"scripts": {
"post-install-cmd": [
"cp ./vendor/reggieofarrell/wp-env-sync/wp-env-sync.sh ./",
"if [ ! -f ./wp-env-sync/ ]; then cp -r ./vendor/reggieofarrell/wp-env-sync/wp-env-sync/ ./wp-env-sync; fi;"
],
"post-update-cmd": [
"cp ./vendor/reggieofarrell/wp-env-sync/wp-env-sync.sh ./",
"if [ ! -f ./wp-env-sync/ ]; then cp -r ./vendor/reggieofarrell/wp-env-sync/wp-env-sync/ ./wp-env-sync; fi;"
]
}then run...
composer require reggieofarrell/wp-env-sync
- Setup your
.env.wpenvsyncfile (see below) in the WP root directory - In a
wp-env-syncdirectory within the WP root directory...- Add any necessary additional rsync exclues not already in
wp-env-sync.shfor this environment (typically files or folders for your WP project that are in version control) to a file namedadditional-rsync-excludes.txtor (additional-rsync-excludes-staging.txtoradditional-rsync-excludes-local.txtif you need different excludes per environment) depending on the environment type - If you're running wp multisite, the default search-replace command that we run will not work properly. In this case you'll need to add your own
db-search-replace.shfile in thewp-env-syncdirectory. If this file is detected, the commands in that file will be run instead of the default search-replace commands - Add any additional shell commands to run at the end of the script to a file name
wp-env-sync-ext-local.shorwp-env-sync-ext-staging.shdepending on the environment type
- Add any necessary additional rsync exclues not already in
- Run script (
bash wp-env-sync.sh) - You may want to create your own script that calls
wp-env-sync.shwith the correct options for your use case already added.
-d --no-db-sync :: skips the step of syncing the database from the remove environment
-f --no-file-sync :: skips the step of syncing the files from the remote environment
-p --no-perms :: skips the step of tring to reset file permissions after the file sync
-s --slow :: pauses 2 seconds after some commands to show output
There are a few places in the main script where it will look for specifically named .sh or .txt files in the wp-env-sync directory and execute them or include them if found...
- [all environments] specify additional excludes for the file sync operation. See the rsync manual on
--exclude-from=FILEfor syntax
- [staging only] specify additional excludes for the file sync operation. See the rsync manual on
--exclude-from=FILEfor syntax
- [local only] specify additional excludes for the file sync operation. See the rsync manual on
--exclude-from=FILEfor syntax
- [staging only] script to be run at the end of the main
wp-env-syncscript
- [local only] script to be run at the end of the main
wp-env-syncscript
- [all environments] script to be run just before the db is cleared and production is imported
- [all environments] script to be run instead of the standard db search-replace commands. useful for multisite where the standard commands don't do the trick
You should have a .env.wpenvsync file in the root of the wp project with all of these defined. Values are just examples...
# locaion of your ssh key for the remote server
SSH_KEY_PATH=~/.ssh/id_rsa
# user@domain for the remote
SSH_USER=someuser@1.1.1.1
# ssh port on the remote server (typically 22)
SSH_PORT=22
# is this site env running on https?
LOCAL_ENV_IS_HTTPS=false
# your local domain for this site
LOCAL_DOMAIN=example.com
# is the remote site env running on https?
REMOTE_ENV_IS_HTTPS=true
# remote wordpress site domain
REMOTE_DOMAIN=anothersite.com
# relative remote path of wp from remote home folder (no leading slash)
REMOTE_PATH=files
# full path to the install on the remote server
FULL_REMOTE_PATH=/sites/example.com/files
# remote env name (prod, staging, etc..) for db file naming purposes
REMOTE_ENV=prod
# local env being synced to ['local', 'staging']
LOCAL_ENV=local
# local mysql version
LOCAL_MYSQL_VER=5.7
# remote mysql version
REMOTE_MYSQL_VER=8.0
# optional - additional options to pass to wp db export
# https://developer.wordpress.org/cli/commands/db/export/
WP_DB_EXPORT_ARGS=