├── README.md ├── LICENSE ├── db-pull.yml └── db-push.yml /README.md: -------------------------------------------------------------------------------- 1 | # trellis-db-push-and-pull 2 | Push and pull databases with trellis and ansible playbooks 3 | 4 | ## Usage 5 | 1. set alias for host files 6 | 2. run push or pull playbook `$ ansible-playbook db-push.yml -e "env=ENV site=SITE"` 7 | 8 | ## Hosts Examples 9 | ### 1. Development 10 | 11 | ``` 12 | vagrant 192.168.50.5 ansible_connection=ssh ansible_user=vagrant ansible_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key 13 | [development] 14 | vagrant 15 | 16 | [web] 17 | vagrant 18 | ``` 19 | 20 | ### 2. Staging 21 | 22 | ``` 23 | staging your_server_hostname 24 | 25 | [staging] 26 | staging 27 | 28 | [web] 29 | staging 30 | ``` 31 | 32 | 33 | ### 3. Production 34 | ``` 35 | production your_server_hostname 36 | [production] 37 | production 38 | 39 | [web] 40 | production 41 | ``` 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Hamed Bahrami 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /db-pull.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Pull Database from "{{ env }}" to development 3 | hosts: web:&{{ env }} 4 | remote_user: "{{ web_user }}" 5 | 6 | vars: 7 | project_root: "{{ www_root }}/{{ site }}" 8 | project_web_dir: "{{ project_root }}/current" 9 | from_host: "{{hostvars[env]}}" 10 | url_from: "{{ from_host.wordpress_sites.values().0.site_hosts.0.canonical }}" 11 | url_to: "{{ hostvars.vagrant.wordpress_sites.values().0.site_hosts.0.canonical}}" 12 | 13 | tasks: 14 | - name: create db backup on "{{ env }}" with wp-cli 15 | command: wp db export db.sql --allow-root 16 | args: 17 | chdir: "{{ project_web_dir }}" 18 | 19 | - name: copy db backup from "{{env}}" to vagrant box (via vagrant synced folder) 20 | fetch: 21 | src: "{{project_web_dir}}/db.sql" 22 | dest: ../site 23 | flat: yes # copies file only, without dir structure 24 | 25 | - name: import db backup 26 | delegate_to: vagrant 27 | command: wp db import db.sql 28 | args: 29 | chdir: "{{project_web_dir}}" 30 | 31 | - name: search and replace from "{{url_from}}" to "{{url_to}}" 32 | delegate_to: vagrant 33 | command: wp search-replace '(www.)?{{url_from}}' '{{url_to}}' --allow-root --regex 34 | args: 35 | chdir: "{{project_web_dir}}" 36 | tags: ['search-replace'] 37 | -------------------------------------------------------------------------------- /db-push.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Push Database from development to "{{ env }}" 3 | hosts: web:&{{ env }} 4 | remote_user: "{{ web_user }}" 5 | 6 | vars: 7 | project_root: "{{ www_root }}/{{ site }}" 8 | project_web_dir: "{{ project_root }}/current" 9 | url_from: "{{ hostvars.vagrant.wordpress_sites.values().0.site_hosts.0.canonical }}" 10 | to: "{{hostvars[env]}}" 11 | url_to: "{{to.wordpress_sites.values().0.site_hosts.0.canonical}}" 12 | 13 | tasks: 14 | - name: create db backup on development with wp-cli 15 | delegate_to: vagrant 16 | command: wp db export db.sql 17 | args: 18 | chdir: "{{ project_web_dir }}" 19 | 20 | - name: copy db.sql to "{{env}}" 21 | copy: 22 | src: ../site/db.sql 23 | dest: "{{project_web_dir}}/db.sql" 24 | 25 | - name: import db backup on "{{ env }}" 26 | command: wp db import db.sql --allow-root 27 | args: 28 | chdir: "{{project_web_dir}}" 29 | 30 | # - command: wp search-replace hostvars[groups['development'][0]].wordpress_sites[site].site_hosts.0.canonical hostvars[groups[env][0]].wordpress_sites[site].site_hosts.0.canonical 31 | - name: search and replace from "{{url_from}}" to "{{url_to}}" 32 | command: wp search-replace '(www.)?{{url_from}}' '{{url_to}}' --allow-root --regex 33 | args: 34 | chdir: "{{project_web_dir}}" 35 | tags: ['search-replace'] 36 | --------------------------------------------------------------------------------