/
new-restic-server
executable file
·448 lines (403 loc) · 11.2 KB
/
new-restic-server
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/bin/bash
#
# Configure the server-side components of a restic backup
# system, outlined at:
# https://kula.tproa.net/lnt/computers/backups/restic-systems-backups/
#
# Copyright © 2017, 2018 Thomas L. Kula
# All Rights Reserved
#
# See LICENSE file in the top of this repository for license information
BN=`basename $0`
VER=2
usage() {
echo "$BN: create a server directory for restic backups"
echo "Usage: $BN [-c <cfg file> ] -h <hostname> -p <port> [-U <user>] [-G <group> ]"
echo ""
echo " -c <cfg file>"
echo " Path to optional configuration file"
echo " -H <hostname>"
echo " Hostname of system being backed up"
echo " -p <port>"
echo " Port to run minio server on"
echo " -U <user>"
echo " User to own directories"
echo " -G <group>"
echo " Group to own directories"
echo " -h"
echo " Print this help"
echo ""
}
# Read command line options
while getopts ":c:H:p:hU:G:" opt; do
case "${opt}" in
c)
CFG_FILE="${OPTARG}"
;;
H)
HOST="${OPTARG}"
;;
p)
PORT="${OPTARG}"
;;
U)
CLI_BACKUP_USER="${OPTARG}"
;;
G)
CLI_BACKUP_GROUP="${OPTARG}"
;;
h)
usage
exit 0
;;
\?)
echo "ERROR: Invalid option -${OPTARG}" >&2
echo "" >&2
usage >&2
exit 1
;;
:)
echo "ERROR: Option -${OPTARG} requires an option" >&2
echo "" >&2
usage >&2
exit 1
;;
esac
done
# Verify command line options
if [ -z "${HOST:-}" ]; then
echo "ERROR: must set -H" >&2
echo "" >&2
usage >&2
exit 1
fi
if [ -z "${PORT:-}" ]; then
echo "ERROR: must set -p" >&2
echo "" >&2
usage >&2
exit 1
fi
# Handle config file and set defaults
if [ -n "${CFG_FILE:-}" ]; then
if [ ! -f "${CFG_FILE}" ]; then
echo "ERROR: ${CFG_FILE} unreadable" >&2
echo "" >&2
exit 1
fi
. "${CFG_FILE}"
fi
BACKUPS_DIR="${BACKUPS_DIR:-/backups}"
BIN_DIR="${BIN_DIR:-${BACKUPS_DIR}/bin}"
MINIO="${MINIO:-${BIN_DIR}/minio}"
BACKUP_SYNCD="${BACKUP_SYNCD:-${BIN_DIR}/backup-syncd}"
SVLOGD="${SVLOGD:-/usr/bin/svlogd}"
SVLOGD_OPTS="${SVLOGD_OPTS:--ttt}"
SYSTEMS_DIR="${SYSTEMS_DIR:-${BACKUPS_DIR}/systems}"
SYSTEMS_CONF_DIR="${SYSTEMS_CONF_DIR:-${SYSTEMS_DIR}/conf}"
BACKUP_USER="${CLI_BACKUP_USER:-${BACKUP_USER:-backups}}"
BACKUP_GROUP="${CLI_BACKUP_GROUP:-${BACKUP_GROUP:-backups}}"
# Sub-tasks
make_top_dir() {
install -d -o "${BACKUP_USER}" -g "${BACKUP_GROUP}" -m 2770 "${SYSTEMS_DIR}/${HOST}"
if [ $? != 0 ]; then
echo "Creating ${SYSTEMS_DIR}/${HOST} failed" >&2
exit 1
fi
}
make_logs_dir() {
for dir in logs logs/minio logs/backup-syncd logs/runsvdir logs/replica_sync; do
install -d -o "${BACKUP_USER}" -g "${BACKUP_GROUP}" -m 2770 "${SYSTEMS_DIR}/${HOST}/${dir}"
if [ $? != 0 ]; then
echo "Creating ${SYSTEMS_DIR}/${HOST}/${dir} failed" >&2
exit 1
fi
done
}
make_conf_dir() {
install -d -o "${BACKUP_USER}" -g "${BACKUP_GROUP}" -m 2770 "${SYSTEMS_DIR}/${HOST}/conf"
if [ $? != 0 ]; then
echo "Creating ${SYSTEMS_DIR}/${HOST}/conf failed" >&2
exit 1
fi
}
make_minio_dir() {
install -d -o "${BACKUP_USER}" -g "${BACKUP_GROUP}" -m 2770 "${SYSTEMS_DIR}/${HOST}/minio"
if [ $? != 0 ]; then
echo "Creating ${SYSTEMS_DIR}/${HOST}/minio failed" >&2
exit 1
fi
}
make_backup_syncd_setup() {
# 1. Drop in an empty 'replica_sync' script
# 2. symlink backup-syncd.log.conf to system conf template
cat <<EOF > ${SYSTEMS_DIR}/${HOST}/conf/replica_sync
#!/bin/bash
# Created by $BN version $VER
# Override this to do whatever sync you want to do when this system
# creates a snapshot
exit 0
EOF
chmod 550 ${SYSTEMS_DIR}/${HOST}/conf/replica_sync
chown ${BACKUP_USER}:${BACKUP_GROUP} ${SYSTEMS_DIR}/${HOST}/conf/replica_sync
ln -s ${SYSTEMS_DIR}/conf/backup-syncd.log.conf ${SYSTEMS_DIR}/${HOST}/conf/backup-syncd.log.conf
}
make_runsv_setup() {
# We make a top-level 'runsvs' dir, which has all of our runsv-managed
# services, then under that we have individual units. Current units
# are 'minio', to run the minio storage engine for this system; and
# 'backup-syncd', to sync backups to replica storage.
#
# These 'runsv' units are managed by a top-level 'runsvdir', which
# has its own runsv unit in the 'runsvdir' right inside the conf
# directory, which is ran by the top-level runsv called from the
# systemd unit.
#
# In brief:
#
# - systemd runs runsvdir /backups/systems/conf/runit/
# - in there are symlinks to /backups/systems/aserver.example.com/conf/runsvdir
# - which runs runsvdir /backups/systems/aserver.example.com/conf/runsvs
# - which runs everything necessary to backup aserver.
#
# Making the symlink to .../aserver.example.com/conf/runsvs is manual,
# and allows you to easily turn on and turn off the services to run
# aserver.
local services='minio backup-syncd'
install -d -o "${BACKUP_USER}" -g "${BACKUP_GROUP}" -m 2770 "${SYSTEMS_DIR}/${HOST}/conf/runsvs"
if [ $? != 0 ]; then
echo "Creating ${SYSTEMS_DIR}/${HOST}/conf/runsvs failed" >&2
exit 1
fi
for runsv in $services; do
for dir in '' log; do
install -d -o "${BACKUP_USER}" -g "${BACKUP_GROUP}" -m 2770 "${SYSTEMS_DIR}/${HOST}/conf/runsvs/${runsv}/${dir}"
if [ $? != 0 ]; then
echo "Creating ${SYSTEMS_DIR}/${HOST}/conf/runsvs/${runsv}/${dir} failed" >&2
exit 1
fi
done
done
# Make minio run files
cat <<EOF > "${SYSTEMS_DIR}/${HOST}/conf/runsvs/minio/run"
#!/bin/bash
# Created with $BN version $VER
exec ${MINIO} server --quiet --config-dir ${SYSTEMS_DIR}/${HOST}/conf/minio/ --address :${PORT} ${SYSTEMS_DIR}/${HOST}/minio 2>&1
EOF
cat <<EOF > "${SYSTEMS_DIR}/${HOST}/conf/runsvs/minio/log/run"
#!/bin/bash
# Created with $BN version $VER
exec ${SVLOGD} ${SVLOGD_OPTS} ${SYSTEMS_DIR}/${HOST}/logs/minio
EOF
# Make backup-syncd files
cat <<EOF > "${SYSTEMS_DIR}/${HOST}/conf/runsvs/backup-syncd/run"
#!/bin/bash
# Created with $BN version $VER
exec ${BACKUP_SYNCD} --watchdir=${SYSTEMS_DIR}/${HOST}/minio/backups/restic/snapshots --script=${SYSTEMS_DIR}/${HOST}/conf/replica_sync --logconf ${SYSTEMS_DIR}/${HOST}/conf/backup-syncd.log.conf 2>&1
EOF
cat <<EOF > "${SYSTEMS_DIR}/${HOST}/conf/runsvs/backup-syncd/log/run"
#!/bin/bash
# Created with $BN version $VER
exec ${SVLOGD} ${SVLOGD_OPTS} ${SYSTEMS_DIR}/${HOST}/logs/backup-syncd
EOF
for runsv in $services; do
chmod +x ${SYSTEMS_DIR}/${HOST}/conf/runsvs/${runsv}/run ${SYSTEMS_DIR}/${HOST}/conf/runsvs/${runsv}/log/run
done
# Make runsvdir top-level manager
for dir in '' log; do
install -d -o "${BACKUP_USER}" -g "${BACKUP_GROUP}" -m 2770 "${SYSTEMS_DIR}/${HOST}/conf/runsvdir/${dir}"
if [ $? != 0 ]; then
echo "Creating ${SYSTEMS_DIR}/${HOST}/conf/runsvdir/${dir} failed" >&2
exit 1
fi
done
cat <<EOF > ${SYSTEMS_DIR}/${HOST}/conf/runsvdir/run
#!/bin/bash
# Created with $BN version $VER
exec /usr/bin/runsvdir -P ${SYSTEMS_DIR}/${HOST}/conf/runsvs 2>&1
EOF
cat <<EOF > ${SYSTEMS_DIR}/${HOST}/conf/runsvdir/log/run
#!/bin/bash
# Created with $BN version $VER
exec ${SVLOGD} ${SVLOGD_OPTS} ${SYSTEMS_DIR}/${HOST}/logs/runsvdir
EOF
chmod +x ${SYSTEMS_DIR}/${HOST}/conf/runsvdir/run ${SYSTEMS_DIR}/${HOST}/conf/runsvdir/log/run
}
make_access_key() {
# minio default access keys are 20 random bytes, translated
# in a somewhat biased way to [A-Z0-9]. See
# https://github.com/minio/minio/blob/b233345f1974a6969ce64dc301f4095f6a73df87/cmd/credential.go#L123
#
# We simply assign this job to pwgen and tr. Whether or not it's
# more or less biased I couldn't say.
pwgen 20 1 | tr '[a-z]' '[A-Z]'
}
make_secret_key() {
# minio default secret keys are 40 random bytes, translated to base64
# (which will expand the length) and then truncated to 40 random
# bytes. See
# https://github.com/minio/minio/blob/b233345f1974a6969ce64dc301f4095f6a73df87/cmd/credential.go#L123
#
# https://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers/
# "Use urandom. Use urandom. Use urandom."
key=$( dd if=/dev/urandom bs=1 count=40 2>/dev/null | base64 )
echo ${key:0:40}
}
make_minio_config_version_19() {
# Make dirs
install -d -o "${BACKUP_USER}" -g "${BACKUP_GROUP}" -m 2770 "${SYSTEMS_DIR}/${HOST}/conf/minio"
if [ $? != 0 ]; then
echo "Creating ${SYSTEMS_DIR}/${HOST}/conf/minio failed" >&2
exit 1
fi
# Generate access and secret keys
access_key=$(make_access_key)
secret_key=$(make_secret_key)
# Generate configuration file
# Minio as of version '2017-08-05T00:00:53Z' crashes
# if there isn't configuration for every notify method....
cat <<EOF > ${SYSTEMS_DIR}/${HOST}/conf/minio/config.json
{
"version": "19",
"credential": {
"accessKey": "${access_key}",
"secretKey": "${secret_key}"
},
"region": "",
"browser": "off",
"logger": {
"console": {
"enable": true
},
"file": {
"enable": false
}
},
"notify": {
"amqp": {
"1": {
"enable": false,
"url": "",
"exchange": "",
"routingKey": "",
"exchangeType": "",
"deliveryMode": 0,
"mandatory": false,
"immediate": false,
"durable": false,
"internal": false,
"noWait": false,
"autoDeleted": false
}
},
"nats": {
"1": {
"enable": false,
"address": "",
"subject": "",
"username": "",
"password": "",
"token": "",
"secure": false,
"pingInterval": 0,
"streaming": {
"enable": false,
"clusterID": "",
"clientID": "",
"async": false,
"maxPubAcksInflight": 0
}
}
},
"elasticsearch": {
"1": {
"enable": false,
"format": "",
"url": "",
"index": ""
}
},
"redis": {
"1": {
"enable": false,
"format": "",
"address": "",
"password": "",
"key": ""
}
},
"postgresql": {
"1": {
"enable": false,
"format": "",
"connectionString": "",
"table": "",
"host": "",
"port": "",
"user": "",
"password": "",
"database": ""
}
},
"kafka": {
"1": {
"enable": false,
"brokers": null,
"topic": ""
}
},
"webhook": {
"1": {
"enable": false,
"endpoint": ""
}
},
"mysql": {
"1": {
"enable": false,
"format": "",
"dsnString": "",
"table": "",
"host": "",
"port": "",
"user": "",
"password": "",
"database": ""
}
},
"mqtt": {
"1": {
"enable": false,
"broker": "",
"topic": "",
"qos": 0,
"clientId": "",
"username": "",
"password": ""
}
}
}
}
EOF
# Make certs dir and symlinks
install -d -o "${BACKUP_USER}" -g "${BACKUP_GROUP}" -m 2770 "${SYSTEMS_DIR}/${HOST}/conf/minio/certs"
if [ $? != 0 ]; then
echo "Creating ${SYSTEMS_DIR}/${HOST}/conf/minio/certs failed" >&2
exit 1
fi
for file in private.key public.crt; do
ln -s "${SYSTEMS_DIR}/conf/${file}" "${SYSTEMS_DIR}/${HOST}/conf/minio/certs/${file}"
if [ $? != 0 ]; then
echo "Symlinking ${SYSTEMS_DIR}/${HOST}/conf/minio/certs/${file} -> " >&2
echo " ${SYSTEMS_DIR}/${HOST}/conf/minio/${file} failed" >&2
exit 1
fi
done
}
## Main
make_top_dir
make_logs_dir
make_conf_dir
make_minio_dir
make_backup_syncd_setup
make_runsv_setup
make_minio_config_version_19