Repository navigation
Expand file tree
/
Copy pathrestore.yml
More file actions
117 lines (107 loc) · 4.8 KB
/
Copy pathrestore.yml
File metadata and controls
117 lines (107 loc) · 4.8 KB
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
# Restore the Obcell's *data* from an `obcell backup` archive onto a host the playbook has already set up
# (configs come from site.yml; this brings back what only lived in databases/volumes):
# - Open5GS subscribers (SIM keys) -> mongorestore (existing IMSIs are kept, not overwritten)
# - Open Live productions/settings -> CouchDB open-live (same revisions as in the backup)
# - Caddy's internal root CA -> devices that trust the old CA keep working
# - OwnTracks location history -> replaces ~/owntracks/store (positions recorded since the build are lost)
#
# ansible-playbook restore.yml --limit <host> -e backup=~/obcell-backups/obcell-config-YYYYMMDD-HHMM.tar.gz
- name: Restore Obcell data
hosts: obcell
become: true
vars:
work: /root/obcell-restore
caddy_pki: /var/lib/docker/volumes/openlive_caddy_data/_data/caddy
tasks:
- name: Backup archive given
ansible.builtin.assert:
that: backup is defined
fail_msg: "usage: ansible-playbook restore.yml --limit <host> -e backup=/path/to/obcell-config-....tar.gz"
- name: Right machine?
ansible.builtin.import_tasks: tasks/right_host.yml
- name: Work directory
ansible.builtin.file:
path: "{{ work }}"
state: directory
mode: "0700"
# Older backups have no OwnTracks part, and tar fails on a member that isn't there.
- name: Members of the archive
ansible.builtin.command: tar -tzf {{ backup | expanduser }}
delegate_to: localhost
become: false
register: members
changed_when: false
check_mode: false
- name: Unpack the data members of the archive
ansible.builtin.unarchive:
src: "{{ backup }}"
dest: "{{ work }}"
include: "{{ ['./open5gs-mongo.archive.gz', './open-live-couchdb.json', './caddy-pki.tar']
+ (['./owntracks-store.tar'] if './owntracks-store.tar' in members.stdout_lines else []) }}"
- name: Which parts are in this backup?
ansible.builtin.stat:
path: "{{ work }}/{{ item }}"
loop: [open5gs-mongo.archive.gz, open-live-couchdb.json, caddy-pki.tar, owntracks-store.tar]
register: parts
- name: Open5GS subscribers
ansible.builtin.command: mongorestore --quiet --gzip --archive={{ work }}/open5gs-mongo.archive.gz --nsInclude=open5gs.*
when: parts.results[0].stat.exists
- name: Open Live productions (CouchDB)
ansible.builtin.shell: |
set -e
python3 -I - "{{ work }}/open-live-couchdb.json" > {{ work }}/bulk.json <<'PY'
import json, sys
rows = json.load(open(sys.argv[1]))["rows"]
print(json.dumps({"new_edits": False, "docs": [r["doc"] for r in rows if "doc" in r]}))
PY
curl -sf -u "$U:$P" -X PUT http://127.0.0.1:5984/open-live >/dev/null || true
curl -sf -u "$U:$P" -H 'Content-Type: application/json' -X POST http://127.0.0.1:5984/open-live/_bulk_docs -d @{{ work }}/bulk.json >/dev/null
environment:
U: "{{ couchdb_user }}"
P: "{{ couchdb_password }}"
args:
executable: /bin/bash
when: parts.results[1].stat.exists
- name: Caddy root CA
when: parts.results[2].stat.exists
block:
- name: Stop Caddy
ansible.builtin.command: docker compose stop caddy
args:
chdir: "{{ obcell_home }}/openlive"
- name: Put the old PKI back
ansible.builtin.shell: rm -rf {{ caddy_pki }}/pki && tar -C {{ caddy_pki }} -xf {{ work }}/caddy-pki.tar
- name: Start Caddy
ansible.builtin.command: docker compose start caddy
args:
chdir: "{{ obcell_home }}/openlive"
- name: Republish the CA certificate
ansible.builtin.copy:
src: "{{ caddy_pki }}/pki/authorities/local/root.crt"
remote_src: true
dest: "{{ item }}"
owner: "{{ obcell_user }}"
group: "{{ obcell_user }}"
mode: "0600"
loop:
- "{{ obcell_home }}/openlive/openlive-root-ca.crt"
- "{{ obcell_home }}/monitoring/blackbox/lan-root-ca.crt"
- name: OwnTracks location history
when: parts.results[3].stat.exists and owntracks_enabled | bool
block:
- name: Stop the Recorder
ansible.builtin.command: docker compose stop recorder
args:
chdir: "{{ obcell_home }}/owntracks"
- name: Put the old store back
ansible.builtin.shell: rm -rf store && tar -xf {{ work }}/owntracks-store.tar
args:
chdir: "{{ obcell_home }}/owntracks"
- name: Start the Recorder
ansible.builtin.command: docker compose start recorder
args:
chdir: "{{ obcell_home }}/owntracks"
- name: Clean up
ansible.builtin.file:
path: "{{ work }}"
state: absent