-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.yml
212 lines (181 loc) · 6.03 KB
/
backup.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
---
- hosts: backup
vars:
check: /var/containers
gather_facts: yes
become: yes
tasks:
- name: Create a directory if it does not exist
file:
path: /srv/containers/sync_backup/storage/{{ ansible_hostname }}
state: directory
mode: '0755'
delegate_to: localhost
- name: Check a directory if it does not exist
stat:
path: "{{ check }}"
register: check
- name: Check a directory if it does not exist, delegate to 127.0.0.1
stat:
path: /srv/containers/sync_backup/storage/{{ ansible_hostname }}/containers
register: containers_local
delegate_to: localhost
- name: Rsync etc with copy symlinks
synchronize:
dest_port: "{{ ansible_port }}"
mode: pull
src: "{{ item.source }}"
dest: "{{ item.dest }}"
archive: yes
links: yes
private_key: /root/.ssh/host_rsa
rsync_opts:
- "--copy-links"
- "--ignore-missing-args"
- "--quiet"
- "--no-motd"
- "--exclude-from=exclude-list.txt"
with_items:
- { source: '/etc', dest: '/srv/containers/sync_backup/storage/{{ ansible_hostname }}' }
delegate_to: localhost
- name: Rsync containers with copy symlinks
synchronize:
dest_port: "{{ ansible_port }}"
mode: pull
src: "{{ item.source }}"
dest: "{{ item.dest }}"
archive: yes
links: yes
private_key: /root/.ssh/host_rsa
rsync_opts:
- "--copy-links"
- "--ignore-missing-args"
- "--quiet"
- "--no-motd"
- "--exclude-from=exclude-list.txt"
with_items:
- { source: '/var/containers',
dest: '/srv/containers/sync_backup/storage/{{ ansible_hostname }}' }
delegate_to: localhost
when: check.stat.exists and check.stat.isdir
- set_fact:
localhost_hostname: "{{ ansible_hostname }}"
- name: Copy fact to localhost server
set_fact:
localhost_hostname: "{{ localhost_hostname }}"
delegate_to: localhost
delegate_facts: True
- name: Find all containers directories
find:
paths: /srv/containers/sync_backup/storage/{{ localhost_hostname }}
recurse: no
file_type: directory
patterns: 'containers'
register: containers_dirs
delegate_to: localhost
- name: Find all etc directories
find:
paths: /srv/containers/sync_backup/storage/{{ localhost_hostname }}
recurse: no
file_type: directory
patterns: 'etc'
register: etc_dirs
delegate_to: localhost
- set_fact:
containers: "{{ containers_dirs.files | map(attribute='path') | list }}"
etc: "{{ etc_dirs.files | map(attribute='path') | list }}"
- name: Create a bz2 archive of multiple files, rooted at /etc
archive:
path: "{{ item }}"
remove: yes
dest: /srv/containers/sync_backup/storage/{{ localhost_hostname }}/etc.tar.bz2
format: bz2
loop:
- "{{ etc }}"
delegate_to: localhost
- name: Create a bz2 archive of multiple files, rooted at /containers
archive:
path: "{{ item }}"
remove: yes
dest: /srv/containers/sync_backup/storage/{{ localhost_hostname }}/containers.tar.bz2
format: bz2
loop:
- "{{ containers }}"
delegate_to: localhost
when: check.stat.exists and check.stat.isdir
- name: Ansible remove file wildcard
file:
path:
- "{{ item }}"
state: absent
force: yes
with_items:
- "{{ etc_dirs.files | map(attribute='path') | list }}/"
- "{{ containers_dirs.files | map(attribute='path') | list }}/"
delegate_to: localhost
- name: Remove directory
shell: rm -rf {{ item }}
loop:
- /srv/containers/sync_backup/storage/*/etc
- /srv/containers/sync_backup/storage/*/containers
become: yes
delegate_to: localhost
- name: Find all archives bz2
find:
paths: /srv/containers/sync_backup/storage/{{ localhost_hostname }}
patterns: '*.tar.bz2'
recurse: no
register: archives_matched
delegate_to: localhost
- set_fact:
encrypt_archives: "{{ archives_matched.files | map(attribute='path') | list }}"
- name: Vault encrypt archives
command: ansible-vault encrypt {{ item }} --vault-password-file=.env
args:
chdir: /srv/containers/sync_backup
with_items:
- "{{ encrypt_archives }}"
delegate_to: localhost
- name: Create a bz2 archive of multiple files, rooted at /
archive:
path: /srv/containers/sync_backup/storage/{{ localhost_hostname }}
remove: yes
dest: /srv/containers/sync_backup/storage/{{ localhost_hostname }}.tar.bz2
format: bz2
delegate_to: localhost
- hosts: localhost
gather_facts: yes
become: yes
tasks:
- name: Find archives bz2, only host
find:
paths: /srv/containers/sync_backup/storage
recurse: no
patterns: '*.tar.bz2'
register: send_to_git
- set_fact:
send: "{{ send_to_git.files | map(attribute='path') | list }}"
- name: Vault encrypt archives
command: ansible-vault encrypt {{ item }} --vault-password-file=.env
with_items:
- "{{ send }}"
args:
chdir: /srv/containers/sync_backup
- name: Find all containers directories
find:
paths: /srv/containers/sync_backup/storage
recurse: no
file_type: directory
register: remove_all
- set_fact:
remove: "{{ remove_all.files | map(attribute='path') | list }}"
- name: Ansible remove file wildcard
file:
path: "{{ item }}"
state: absent
force: yes
with_items: "{{ remove }}"
- name: sync backups to bitbucket
shell: git lfs track storage/* && git checkout master && git add . --all && git commit -m "backups" && git push -u origin master
args:
chdir: /srv/containers/sync_backup