# Database syncing

**URL:** https://discourse.roots.io/t/database-syncing/7283
**Category:** trellis
**Created:** 2016-07-27T18:49:09Z
**Posts:** 17

## Post 1 by @Aggressive — 2016-07-27T18:49:09Z

What opinions are there for database syncing?

The obvious one is wp db migrate pro. Is there any others I would like to work in the command line since I’m already there.

Seems like the only thing missing.

Thanks

---

## Post 2 by @darjanpanic — 2016-07-27T22:34:10Z

Trellis uses [wp cli](http://wp-cli.org) and you can export the database with `wp db export` command. Works same with import after you deploy.

---

## Post 3 by @Aggressive — 2016-07-28T01:35:57Z

So I would `vagrant ssh` in order to export the database? Is there a guide that you know of to show examples of steps involved?

Thank you!

---

## Post 4 by @cfx — 2016-07-28T02:04:00Z

[https://wp-cli.org/commands/db/](https://wp-cli.org/commands/db/)

You can also use the forum’s search feature to read threads that have already addressed this :thumbsup:

---

## Post 5 by @darjanpanic — 2016-07-28T07:47:17Z

And Migrate DB pro also has WP Cli Addon in case you’d prefer that, here’s a nice video showing it - [https://deliciousbrains.com/wp-migrate-db-pro/doc/cli-addon/](https://deliciousbrains.com/wp-migrate-db-pro/doc/cli-addon/)

---

## Post 6 by @paul_tibbetts — 2016-07-28T11:55:22Z

> [@Aggressive](#):
>
> Is there a guide that you know of to show examples of steps involved?

> [@cfx](#):
>
> You can also use the forum’s search feature to read threads that have already addressed this :thumbsup:

I’ve written up how you can use WP-CLI and SCP/rsync to keep production and staging in sync here:

> [@Local > Staging > Deploy Setup Help slight_smile](https://discourse.roots.io/t/local-staging-deploy-setup-help/7073/2):
>
> Hey @uberaff, welcome to Roots! If I understand correctly, you’d like to have a staging site on, say, staging.example.com with the same code and database as production. First, you’ll need the IP of the staging server added to [hosts/staging](https://github.com/roots/trellis/blob/master/hosts/staging) under both [staging] and [web]. Next, add your site details to staging/wordpress\_sites.yml. If you’ve already configured it for production you can copy and paste this over. Remember to also modify the vault.yml file with [the name of your site](https://github.com/roots/trellis/blob/master/group_vars/staging/wordpress_sites.yml#L6). site\_hosts c…

---

## Post 7 by @Aggressive — 2016-07-28T15:41:59Z

Thank you all for the information. Couple options to consider and best workflow I’ll have to consider.

---

## Post 8 by @valentinocossar — 2016-09-12T13:15:32Z

WP Migrate DB is better but I’ve made [this](https://github.com/itsensoul/capistrano-bedrock) based on [roots/bedrock-capistrano](https://github.com/roots/bedrock-capistrano) and [lavmeiker/capistrano-wpcli](https://github.com/lavmeiker/capistrano-wpcli). Take a look and tell me what you think. :smile:

---

## Post 9 by @valentinocossar — 2017-06-24T18:43:01Z

> [@Pushing local database to the remote with wp_cli](https://discourse.roots.io/t/pushing-local-database-to-the-remote-with-wp-cli/3160/25):
>
> For who is still interested in this topic, [I’ve made a more simple tool](https://github.com/valentinocossar/trellis-database-uploads-migration) that works with Ansible (and it’s more Trellis agnostic) and doesn’t need Ruby and Capistrano. Take a look and tell me what you think. smile

---

## Post 10 by @strarsis — 2017-06-24T19:41:26Z

Related tool:

> [@Leveraging WP-CLI Aliases in Your WordPress Development Workflow](https://discourse.roots.io/t/leveraging-wp-cli-aliases-in-your-wordpress-development-workflow/8414/22):
>
> Actually another pull request by @guilro is in line for automating database and file backups using [Stouts.backup](https://github.com/Stouts/Stouts.backup) Ansible role with [Duplicity](https://help.ubuntu.com/community/DuplicityBackupHowto) and is mentioned here: [https://github.com/roots/trellis/pull/650](https://github.com/roots/trellis/pull/650) . I think that would be really good to have and can be configured in many different ways.

---

## Post 11 by @robrecord — 2019-04-22T19:46:52Z

I use the following, with WP-CLI aliases (see above):

```
# export production alias to local
wp db export \ # backup the local database
&& wp db reset \ # optional - create a blank slate
&& wp @production db export - | wp db import - \ # export the production database and pipe it into our local database
&& wp search-replace '//production-url.com' '//local-url.test' --skip-columns=guid --report-changed-only \
&& wp cache flush
```

```
# export local db to staging alias
wp @staging db export \ # backup the staging database
&& wp @staging db reset \ # optional - create a blank slate on staging
&& wp db export - | wp @staging db import - \ # export the local database and pipe it into our staging database
&& wp @staging search-replace '//local-url.test' '//staging-url.com' --skip-columns=guid --report-changed-only \
&& wp @staging cache flush
```

Use inside `vagrant ssh` if using trellis, or create a @dev alias for vagrant

I have these commands set up in composer scripts. I use rsync for the uploads directory.

I’d like to incorporate this into Trellis but ansible scares me.

---

## Post 12 by @rguttersohn — 2023-02-28T17:58:10Z

Currently using this method to move local export of db tables to a staging site. How did you get around the permission issues with scp? I keep getting a permission denied error when attempting to scp into /srv/www/mydomain.com/current. From my research, it looks like this expected behavior and that I should drop it into Home and then – I guess – move to it current.

---

## Post 13 by @ben — 2023-02-28T22:31:18Z

Since this topic has been revived I’m gonna go ahead and plug this:

> **[Sync Script for WordPress Database and Uploads](https://roots.io/products/sync-script/)**
>
> Sync your WordPress database and media library between environments with WP-CLI aliases and rsync.

---

## Post 14 by @rguttersohn — 2023-03-02T14:34:37Z

Does this make it easy to back up a db during sync?

---

## Post 15 by @Jack_Kudla — 2023-03-02T23:38:13Z

No, you will manually have to backup files/database. It’s just a tool to synchronize between production/staging/dev. Personally, I find it the best available strictly based on cost.

---

## Post 16 by @chrillep — 2023-06-10T11:27:36Z

Any plans on rewriting the sync script as a WP-CLI or Acorn package?  
would be easier to install and maintain that in composer :heart:

---

## Post 17 by @MWDelaney — 2023-06-12T13:01:32Z

For anyone looking, [trellis-github-deployment](https://github.com/MWDelaney/trellis-github-deployment) can sync assets and database between production and staging, and can with a little modification work the other way, too.

Support of GitHub “releases”, too, might meet some needs here. From the README (emphasis mine):

> When a deploy to `production` is completed, the following occurs:
> 
> - A new release is created with the current date and time **including site’s database and uploads attached as artifacts (GitHub release size restrictions apply)**.
> - A GitHub Deployment is created with a link to the environment.

[https://discourse.roots.io/t/trellis-deployment-with-github-actions/](https://discourse.roots.io/t/trellis-deployment-with-github-actions/)
