|
| 1 | +# Sync Script |
| 2 | + |
| 3 | +Syncing Bedrock-based WordPress environments with WP-CLI aliases and `rsync`. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Create a new `scripts/` directory in your Bedrock directory (`site/`) and place the sync script inside of there. |
| 8 | + |
| 9 | +Make sure that `sync.sh` is executable (`chmod u+x sync.sh`). |
| 10 | + |
| 11 | +## Configuration |
| 12 | + |
| 13 | +Edit the variables at the top of `sync.sh` to match the settings for your environments: |
| 14 | + |
| 15 | +* `DEVDIR` — Local path to uploads directory |
| 16 | +* `DEVSITE` — Local dev URL |
| 17 | +* `PRODDIR` — `user@hostname:/path/to/uploads/` |
| 18 | +* `PRODSITE` — Production URL |
| 19 | +* `STAGDIR` — `user@hostname:/path/to/uploads/` |
| 20 | +* `STAGSITE` — Staging URL |
| 21 | + |
| 22 | +The Kinsta version of the script is slightly different: |
| 23 | + |
| 24 | +* `REMOTEDIR` — `user@hostname:/www/example_123/public/shared/uploads/` |
| 25 | +* `PRODPORT` — Production port |
| 26 | +* `STAGPORT` — Staging port |
| 27 | + |
| 28 | +### WP-CLI aliases |
| 29 | + |
| 30 | +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. |
| 31 | + |
| 32 | +#### Trellis WP-CLI aliases |
| 33 | + |
| 34 | +```yml |
| 35 | +# site/wp-cli.yml |
| 36 | +path: web/wp |
| 37 | + |
| 38 | +@development: |
| 39 | + ssh: vagrant@example.test/srv/www/example.com/current |
| 40 | +@staging: |
| 41 | + ssh: web@staging.example.com/srv/www/example.com/current |
| 42 | +@production: |
| 43 | + ssh: web@example.com/srv/www/example.com/current |
| 44 | +``` |
| 45 | +
|
| 46 | +Test the aliases to make sure they're working: |
| 47 | +
|
| 48 | +```sh |
| 49 | +$ wp @development |
| 50 | +$ wp @staging |
| 51 | +$ wp @production |
| 52 | +``` |
| 53 | + |
| 54 | +#### Trellis + Kinsta WP-CLI aliases |
| 55 | + |
| 56 | +```yml |
| 57 | +# site/wp-cli.yml |
| 58 | +path: web/wp |
| 59 | + |
| 60 | +@development: |
| 61 | + ssh: vagrant@example.test/srv/www/example.com/current |
| 62 | +@staging: |
| 63 | + ssh: example@1.2.3.4:54321/www/example_123/public/current/web |
| 64 | +@production: |
| 65 | + ssh: example@1.2.3.4:12345/www/example_123/public/current/web |
| 66 | +``` |
| 67 | +
|
| 68 | +### `.gitignore` |
| 69 | + |
| 70 | +Open `.gitignore` in your Bedrock directory (`site/`) and add the following: |
| 71 | + |
| 72 | +``` |
| 73 | +# WP-CLI |
| 74 | +*_development*.sql |
| 75 | +``` |
| 76 | + |
| 77 | +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. |
| 78 | + |
| 79 | +### Slack notification |
| 80 | + |
| 81 | +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. |
| 82 | + |
| 83 | +Make sure to [create a new incoming webhook](https://api.slack.com/incoming-webhooks) and updating the URL in the script, as well as the channel. |
| 84 | + |
| 85 | +## Usage |
| 86 | + |
| 87 | +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. |
| 88 | + |
| 89 | +Navigate to the `site/scripts/` directory to use the sync script. |
| 90 | + |
| 91 | +Possible sync directions: |
| 92 | + |
| 93 | +```sh |
| 94 | +# Sync production down to development |
| 95 | +$ ./sync.sh production development |
| 96 | +
|
| 97 | +# Sync staging down to development |
| 98 | +$ ./sync.sh staging development |
| 99 | +
|
| 100 | +# Sync development up to producton |
| 101 | +$ ./sync.sh development production |
| 102 | +
|
| 103 | +# Sync development up to staging |
| 104 | +$ ./sync.sh development staging |
| 105 | +
|
| 106 | +# Sync production to staging |
| 107 | +$ ./sync.sh production staging |
| 108 | +
|
| 109 | +# Sync staging to production |
| 110 | +$ ./sync.sh staging production |
| 111 | +``` |
| 112 | + |
| 113 | +## Troubleshooting |
| 114 | + |
| 115 | +### Unable to connect to development |
| 116 | + |
| 117 | +Make sure that your local development setup is up and running. |
| 118 | + |
| 119 | +### Unable to connect to production or staging |
| 120 | + |
| 121 | +Make sure that you're able to successfully connect with a SSH connection with the same details configured for the same WP-CLI alias. |
| 122 | + |
| 123 | +If your SSH connection doesn't fail, make sure WordPress is first already installed. |
| 124 | + |
| 125 | +## Support |
| 126 | + |
| 127 | +[Shoot me an email](mailto:ben@roots.io) with any issues you run into. |
| 128 | + |
| 129 | +You can re-download the latest version by visiting [https://roots.io/product-links/](https://roots.io/product-links/). |
0 commit comments