-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase-push.yml
executable file
·94 lines (80 loc) · 3.52 KB
/
database-push.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
---
- name: Push {{ site }} database from development to {{ env }}
hosts: web:&{{ env }}
remote_user: "{{ web_user }}"
vars:
project_root: "{{ www_root }}/{{ site }}"
project_web_dir: "{{ project_root }}/current"
host: "{{ env }}_host"
to_host: "{{ hostvars[host] }}"
url_from: "{{ hostvars.development_host.wordpress_sites[site].site_hosts.0.canonical }}"
url_to: "{{ to_host.wordpress_sites[site].site_hosts.0.canonical }}"
local_bedrock_dir: "{{ to_host.wordpress_sites[site].local_path }}"
dump_file: "{{ site | regex_replace('\\.+', '_') }}_db_dump.sql.gz"
current_date_and_time: "{{ ansible_date_time.date | regex_replace('\\-+', '_') }}_{{ ansible_date_time.hour }}_{{ ansible_date_time.minute }}_{{ ansible_date_time.second }}"
backup_file: "{{ site | regex_replace('\\.+', '_') }}_{{ env }}_{{ current_date_and_time }}.sql.gz"
tasks:
- name: Abort if environment variable is equal to development
fail:
msg: "ERROR: development is not a valid environment for this mode (you can't push from development to development)."
when: env == "development"
- name: Check if {{ site }} folder exists
delegate_to: development_host
stat:
path: "{{ project_root }}"
register: result
- name: Abort if {{ site }} folder doesn't exists
fail:
msg: "ERROR: {{ site }} is not a valid site name ({{ site }} folder does not exist)."
when: result.stat.exists is defined and result.stat.exists == false or result.stat.isdir is defined and result.stat.isdir == false
- block:
- name: Create database_backup directory if it doesn't exist
delegate_to: development_host
file:
path: "{{ project_web_dir }}/database_backup"
state: directory
mode: 0755
- name: Create database dump on development
delegate_to: development_host
shell: wp db export --allow-root - | gzip > {{ dump_file }}
args:
chdir: "{{ project_web_dir }}"
- name: Push database dump from development to {{ env }}
copy:
src: "{{ local_bedrock_dir }}/{{ dump_file }}"
dest: "{{ project_web_dir }}/{{ dump_file }}"
- name: Delete database dump from development
delegate_to: development_host
shell: rm -f {{ dump_file }}
args:
chdir: "{{ project_web_dir }}"
warn: false
- name: Export {{ env }} database before importing dump (backup)
shell: wp db export - | gzip > {{ backup_file }}
args:
chdir: "{{ project_web_dir }}"
- name: Pull exported database from {{ env }} to development (backup)
fetch:
src: "{{project_web_dir}}/{{ backup_file }}"
dest: "{{ local_bedrock_dir }}/database_backup/"
flat: yes
- name: Delete exported database from {{ env }} (backup)
shell: rm -f {{ backup_file }}
args:
chdir: "{{ project_web_dir }}"
warn: false
- name: Import database dump on {{ env }}
shell: gzip -c -d {{ dump_file }} | wp db import -
args:
chdir: "{{ project_web_dir }}"
- name: Delete database dump from {{ env }}
shell: rm -f {{ dump_file }}
args:
chdir: "{{ project_web_dir }}"
warn: false
- name: Search for {{ url_from }} and replace with {{ url_to }} on development
command: wp search-replace '//{{ url_from }}' '//{{ url_to }}' --allow-root --all-tables
args:
chdir: "{{ project_web_dir }}"
tags: ['search-replace']
when: result.stat.exists is defined and result.stat.exists and result.stat.isdir is defined and result.stat.isdir