-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathinit-standby.sh
executable file
·466 lines (403 loc) · 16.4 KB
/
init-standby.sh
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/bin/bash
set -uo pipefail
#==============================================================#
# File : init-standby.sh
# Mtime : 2019-03-07
# Desc : Init PostgreSQL standby
# Path : bin/init-standby.sh
# Author : Vonng([email protected])
# Note : This scripts is idempotent
#==============================================================#
# module info
__MODULE_INIT_STANDBY="init-standby"
PROG_DIR="$(cd $(dirname $0) && pwd)"
PROG_NAME="$(basename $0)"
#--------------------------------------------------------------#
# Name: init_pgpass
# Desc: init .pgpass with replicator user credential
# Arg1: repl_user replication username (replicator)
# Arg2: repl_pass replication password (replicating)
# Note: this function is idempotent
#--------------------------------------------------------------#
function init_pgpass(){
local repl_user=${1-'replicator'}
local repl_pass=${2-'replicating'}
local pgpass_file="${HOME}/.pgpass"
local credential='*:*:*:'"${repl_user}:${repl_pass}"
# if found /opt/conf/pgass, use it anyway
if [[ -f /opt/conf/pgpass ]]; then
echo "info: found /opt/conf/pgass , overwrite to ${pgpass}"
rm -rf ${pgpass_file}
cp -f /opt/conf/pgpass ${pgpass_file}
return 0
fi
# create pgpass
[[ ! -f ${pgpass_file} ]]
touch ${pgpass_file} 2>/dev/null
if [[ $? != 0 ]]; then
echo "error: failed to touch ${pgpass_file}" # permission denied
return 11
fi
# write credential if not exists
if ! (grep ${credential} ${pgpass_file} >/dev/null 2>&1); then
echo "info: write ${repl_user} credential to ${pgpass_file}"
echo ${credential} >> ${pgpass_file}
fi
chmod 0600 ${pgpass_file}
return 0
}
#--------------------------------------------------------------#
# Name: init_pgdata
# Desc: create a new database cluster on given path
# Arg1: primary url (example: postgres://[email protected]/postgres)
# Arg2: pgdata data directory (/pg/data)
# Arg3: force force remove old data (true)
# Note: this function is idempotent
#--------------------------------------------------------------#
function init_pgdata(){
local dbname=${1}
local pgdata=${2-'/pg/data'}
local force=${3-'true'}
# cleanup
if systemctl status postgresql > /dev/null 2>&1 ; then
echo "warn: shutdown postgresql.service"
sudo systemctl stop postgresql > /dev/null 2>&1
fi
if [[ -f ${pgdata}/postmaster.pid ]] ; then
echo "warn: shutdown postgresql on ${pgdata}"
pg_ctl -D ${pgdata} stop -m immediate > /dev/null 2>&1
fi
# make sure dir is empty with postgres 0700 access
# if force is not true, prompt choice
[[ ! -d ${pgdata} ]] && mkdir -p ${pgdata}
if [[ "$(ls ${pgdata} | wc -l )" != "0" ]]; then
if [[ ${force} == 'true' ]]; then
echo "warn: ${pgdata} not empty: force remove ${pgdata}"
rm -rf ${pgdata}
else
echo "WARNING: ${pgdata} is not empty, remove all stuff?"
local choice=""
read -p "Continue (y/n)?>" choice
case "$choice" in
y|Y )
if ! rm -rf ${pgdata}; then
echo "error: ${pgdata} remove failed"
return 21
fi
echo "warn: ${pgdata} removed"
;;
* )
echo "error: user choose to abort"
return 22
;;
esac
fi
fi
[[ ! -d ${pgdata} ]] && mkdir -p ${pgdata}
chmod 0700 ${pgdata}
# init pgdata with pgbasebackup
pg_basebackup \
--dbname ${dbname} \
--pgdata ${pgdata} \
--checkpoint=fast \
--write-recovery-conf \
--no-password
if [[ $? != 0 ]]; then
echo "error: make base backup failed"
return 23
fi
echo "info: database cluster ${pgdata} initialized"
return 0
}
#--------------------------------------------------------------#
# Name: init_postgresql_conf
# Desc: Use user provided conf in /opt/conf or make default change
# Arg1: conf_path database conf path (/pg/data/postgresql.conf)
# Arg2: check_path user provided conf (/opt/conf/postgresql.conf)
# Note: this function is idempotent
#--------------------------------------------------------------#
function init_postgresql_conf(){
local conf_path=${1-'/pg/data/postgresql.conf'}
local check_path=${2-'/opt/conf/postgresql.conf'}
local backup_path="$(dirname ${conf_path})/postgresql.conf.backup"
# if found user provided conf, use it instead
if [[ -f ${check_path} ]]; then
echo "info: found ${check_path}, overwrite to ${conf_path}"
[[ -f ${conf_path} ]] && mv -f ${conf_path} $(dirname ${conf_path})/postgresql.conf.old
cp -f ${check_path} ${conf_path}
return 0
fi
# standby does not require configuration edit here
return 0
}
#--------------------------------------------------------------#
# Name: init_pg_hba_conf
# Desc: Use user provided hba conf in /opt/conf or make default change
# Arg1: conf_path database conf path (/pg/data/pg_hba.conf)
# Arg2: check_path user provided conf (/opt/conf/pg_hba.conf)
# Note: this function is idempotent
#--------------------------------------------------------------#
function init_pg_hba_conf(){
local conf_path=${1-'/pg/data/pg_hba.conf'}
local check_path=${2-'/opt/conf/pg_hba.conf'}
# if found user provided version, use it instead
if [[ -f ${check_path} ]]; then
echo "info: found ${check_path}, overwrite to ${conf_path}"
[[ -f ${conf_path} ]] && mv -f ${conf_path} $(dirname ${conf_path})/pg_hba.conf.old
cp -f ${check_path} ${conf_path}
return 0
fi
# standby does not require configuration edit here
return 0
}
#--------------------------------------------------------------#
# Name: init_recovery_conf
# Desc: Use user provided hba conf in /opt/conf or make default change
# Arg1: conf_path database conf path (/pg/data/recovery.done)
# Arg2: check_path user provided conf (/opt/conf/recovery.conf)
# Arg3: application_name (standby)
# Note: this function is idempotent
#--------------------------------------------------------------#
function init_recovery_conf(){
local conf_path=${1-'/pg/data/restore.conf'}
local check_path=${2-'/opt/conf/restore.conf'}
local application_name=${3-'standby'}
# if found user provided version, use it instead (note this is standby)
if [[ -f ${check_path} ]]; then
echo "info: found ${check_path}, overwrite to ${conf_path}"
rm -rf ${conf_path}
cp -f ${check_path} ${conf_path}
return 0
fi
# usually recovery.conf is not provided, pg_basebackup will write one
# skip if already have edit mark
if ( grep -q "ADDITIONAL RECOVERY CONF" ${conf_path} 2>/dev/null); then
echo "info: ${conf_path} been edited, skip"
return 0
fi
# otherwise, append to recovery.conf
echo "info: append config to ${conf_path}"
cat >> ${conf_path} <<- EOF
# ADDITIONAL RECOVERY CONF
recovery_target_timeline = 'latest'
promote_trigger_file = '/pg/promote'
#restore_command ='gzcat /pg/arcwal/%f 1> %p 2> /pg/log/restore.log'
#archive_cleanup_command = '/usr/pgsql/bin/pg_archivecleanup -x .gz /pg/arcwal %r'
#recovery_min_apply_delay = '0'
#recovery_end_command = ''
#recovery_target = 'immediate'
#recovery_target_name = '' # e.g. 'daily backup 2011-01-26'
#recovery_target_time = '' # e.g. '2004-07-14 22:39:00 EST'
#recovery_target_xid = '' # e.g. '1234567'
#recovery_target_lsn = '' # e.g. '0/70006B8'
#recovery_target_inclusive = true
#recovery_target_action = 'shutdown'
#reovery_target_action = 'pause'
EOF
# and append application_name to primary conn info
sed -ie "s/user=/application_name=${application_name} user=/" ${conf_path}
return 0
}
#--------------------------------------------------------------#
# Name: launch_postgresql
# Desc: start postgresql services
# Note: require proper sudo privileges
# Note: this function is idempotent
#--------------------------------------------------------------#
function launch_postgresql(){
sudo systemctl enable postgresql > /dev/null 2>&1
if systemctl status postgresql > /dev/null 2>&1; then
echo "warn: postgresql.service already running, restart anyway"
if ! sudo systemctl stop postgresql ; then
echo "error: fail to stop postgresql.service"
return 66
fi
fi
if ! sudo systemctl restart postgresql > /dev/null 2>&1; then
echo "error: start postgresql.service on standby failed"
systemctl status postgresql
return 67
fi
if ! systemctl status postgresql > /dev/null 2>&1; then
echo "error: postgresql.service status failed"
systemctl status postgresql
return 68
fi
touch ${pgdata}/log/postgresql-{Mon,Tue,Wed,Thu,Fri,Sat,Sun}.csv 2> /dev/null
return 0
}
#--------------------------------------------------------------#
# Name: retarget_pgbouncer
# Desc: make pgbouncer route to new default database instead of postgres
# Arg1: datname default database/user name
# Note: this function is idempotent
# Note: make sure postgres owns /etc/pgbouncer.ini and have sudo systemctl * pgbouncer
# Note: this assume pgbouncer is already installed
#--------------------------------------------------------------#
function retarget_pgbouncer(){
local datname=${1-'postgres'}
# pgbouncer conf not found
if [[ ! -f /etc/pgbouncer/pgbouncer.ini ]]; then
echo "error: /etc/pgbouncer/pgbouncer.ini not exist"
return 71
fi
# you'd better keep pgbouncer.ini format intact to make this work
# usually it is not a problem using default settings, but be cautious
# when use your own pgbouncer.ini
# delete the line right after '[databases]'
sed -ie '/\[databases\]/{n;d}' /etc/pgbouncer/pgbouncer.ini
# append the new line right after '[databases]'
sed -ie "/\[databases\]/a ${datname} =" /etc/pgbouncer/pgbouncer.ini
if [[ $? != 0 ]]; then
echo "error: fail to edit /etc/pgbouncer/pgbouncer.ini"
return 72
fi
echo "info: restart pgbouncer.service"
if ! sudo systemctl restart pgbouncer > /dev/null 2>&1; then
echo "error: restart pgbouncer failed"
systemctl status pgbouncer
return 73
fi
sudo systemctl enable pgbouncer 2> /dev/null
return 0
}
#--------------------------------------------------------------#
# Name: retarget_postgres_exporter
# Desc: make postgres exporter route to new default database instead of postgres
# Arg1: datname default database/user name
# Note: this function is idempotent
# Note: make sure postgres owns /etc/postgres_exporter/env and have proper sudo privileges
# Note: this assume postgres_exporter is already installed
#--------------------------------------------------------------#
function retarget_postgres_exporter(){
local datname=${1-'postgres'}
# pgbouncer conf not found
if [[ ! -f /etc/postgres_exporter/env ]]; then
echo "error: /etc/postgres_exporter/env not exist"
return 81
fi
# replace old dbname=.... to new database
sed -ie 's/dbname=[0-9a-zA-Z_-]*'"/dbname=${datname}/" /etc/postgres_exporter/env 2> /dev/null
if [[ $? != 0 ]]; then
echo "error: fail to edit /etc/postgres_exporter/env"
return 82
fi
echo "info: restart postgres_exporter.service"
if ! sudo systemctl restart postgres_exporter > /dev/null 2>&1; then
echo "error: restart postgres_exporter failed"
systemctl status postgres_exporter
return 83
fi
return 0
}
#==============================================================#
# Main #
#==============================================================#
function main(){
# check privilege
if [[ "$(whoami)" != "postgres" ]]; then
echo "error: run this as postgres"
return 1
fi
# parse pg url style argument
local pgurl=${1}
local proto="$(echo ${pgurl} | grep :// | sed -e's,^\(.*://\).*,\1,g')"
local urlbody="$(echo ${pgurl/${proto}/})"
proto=${proto%%"://"}
local user="$(echo ${urlbody} | grep @ | cut -d@ -f1)"
local host="$(echo ${urlbody/${user}@/} | cut -d/ -f1)"
local pass=$(echo ${user} | grep : | cut -d: -f2)
[[ -n "${pass}" ]] && user=$(echo ${user} | grep : | cut -d: -f1)
local port="$(echo ${host} | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
host=${host%%":${port}"}
local urlpath="$(echo ${urlbody} | grep / | cut -d/ -f2-)"
local queries="$(echo ${urlpath} | grep '?' | cut -d'?' -f2)"
[[ -n "${queries}" ]] && urlpath="$(echo ${urlpath} | grep '?' | cut -d'?' -f1)"
local pgdata=$(echo ${queries} | grep -Eo 'pgdata=([^&]*)')
pgdata=${pgdata##'pgdata='}
local application_name=$(echo ${queries} | grep -Eo 'application_name=([^&]*)')
application_name=${application_name##'application_name='}
# parameters
[[ -z "${user}" ]] && user='replicator'
[[ -z "${pass}" ]] && pass='replicating'
[[ -z "${host}" ]] && host='url.primary.pg'
[[ -z "${port}" ]] && port='5432'
[[ -z "${pgdata}" ]] && pgdata='/pg/data'
[[ -z "${application_name}" ]] && application_name='standby'
local dbname=${urlpath-'postgres'}
local primary_url="postgres://${user}:${pass}@${host}:${port}/${dbname}"
# check url protocol start with postgres
if [[ ${proto} != postgres* ]]; then
echo "error: invalid protocol"
return 2
fi
echo "info: init standby instance on ${HOSTNAME}:${pgdata} : ${primary_url}"
# init standby instance
#--------------------------------------------#
echo "info: init-standby (1/8) init_pgpass"
init_pgpass ${user} ${pass}
[[ $? != 0 ]] && return $?
#--------------------------------------------#
echo "info: init-standby (2/8) init_pgdata"
init_pgdata ${primary_url} ${pgdata}
[[ $? != 0 ]] && return $?
#--------------------------------------------#
echo "info: init-standby (3/8) init_postgresql_conf"
init_postgresql_conf ${pgdata}/postgresql.conf
[[ $? != 0 ]] && return $?
#--------------------------------------------#
echo "info: init-standby (4/8) init_pg_hba_conf"
init_pg_hba_conf ${pgdata}/pg_hba.conf
[[ $? != 0 ]] && return $?
#--------------------------------------------#
echo "info: init-standby (5/8) init_recovery_conf"
init_recovery_conf ${pgdata}/restore.conf /opt/conf/recovery.conf ${application_name}
[[ $? != 0 ]] && return $?
#--------------------------------------------#
echo "info: init-standby (6/8) launch_postgresql"
launch_postgresql
[[ $? != 0 ]] && return $?
#--------------------------------------------#
echo "info: init-standby (7/8) retarget_pgbouncer"
retarget_pgbouncer ${dbname}
[[ $? != 0 ]] && return $?
#--------------------------------------------#
echo "info: init-standby (8/8) retarget_postgres_exporter"
retarget_postgres_exporter ${dbname}
[[ $? != 0 ]] && return $?
#--------------------------------------------#
# init hook (skip error)
echo "info: running user defined init.sh"
[[ -f /opt/conf/init.sh ]] && bash /opt/conf/init.sh
return 0
}
#==============================================================#
# Main #
#==============================================================#
# Args:
# $1 connection string , example:
# postgresql://replicator:[email protected]:5432/test?pgdata=/pg/data
# repl_user repl_pass primary-host-port dbname data_dir
#
# If you want to use default settings with default dbname=test
# just use postgres:///test
#
# Code:
# 0 ok
# 1 insufficient privilege
# 2 invalid url protocol
# 11 fail to create .pgpass file
# 21 fail to remove old data dir
# 22 user abort due to non-empty data dir
# 23 fail to init database cluster d
# 66 fail to stop postgresql.service
# 67 fail to start postgresql.service
# 68 postgresql.service status abnormal
# 71 /etc/pgbouncer/pgbouncer.ini not found
# 72 fail to edit /etc/pgbouncer/pgbouncer.ini
# 73 fail to start pgbouncer.service
# 81 /etc/postgres_exporter/env not found
# 82 fail to edit /etc/postgres_exporter/env
# 83 fail to start postgres_exporter
#==============================================================#
main $@