-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathinstall-walarchiver.sh
executable file
·213 lines (181 loc) · 6.82 KB
/
install-walarchiver.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
#!/bin/bash
#==============================================================#
# File : install-postgres-exporter.sh
# Mtime : 2019-03-06
# Desc : Install Postgres Exporter
# Path : bin/install-postgres-exporter.sh
# Author : Vonng([email protected])
# Depend : CentOS7
# Note : check /opt/conf/services/walarchiver.service
#==============================================================#
# module info
__MODULE_INSTALL_WALARCHIVER="install-walarchiver"
PROG_DIR="$(cd $(dirname $0) && pwd)"
PROG_NAME="$(basename $0)"
#--------------------------------------------------------------#
# Name: download_walarchiver
# Desc: Guarantee a usable walarchiver in ${target_location}
# Arg1: download walarchiver url (my github link)
# Arg2: target walarchiver location (/usr/local/bin/walarchiver)
# Arg3: cache walarchiver location (/opt/pkg/walarchiver)
# Arg3: walarchiver version to download (0.4.7)
# Note: Run this as root
# Note: walarchiver is already in cache dir
#--------------------------------------------------------------#
function download_walarchiver() {
local download_location=${1-'https://raw.githubusercontent.com/Vonng/pg/master/test/pkg/walarchiver'}
local target_location=${2-'/usr/local/bin/walarchiver'}
local cache_location=${3-'/opt/pkg/walarchiver'}
# if exact same version already in target location, skip
if [[ -x ${target_location} ]]; then
echo "warn: found walarchiver ${walarchiver_version} on ${target_location}, skip"
return 0
fi
# if walarchiver in /opt/pkg, use it regardless version
if [[ -x ${cache_location} ]]; then
echo "warn: found walarchiver in cache, cp ${cache_location} ${target_location}, skip"
cp -f ${cache_location} ${target_location}
return 0
else
echo "walarchiver not found, did you remove it from /bin/cache ?"
return 2
fi
cd /tmp
# otherwise, download from github
if ! wget ${download_location} 2> /dev/null; then
echo 'error: download walarchiver failed'
return 3
fi
if [[ ! -f walarchiver ]]; then
echo "walarchiver still not found"
return 4
fi
chmod a+x walarchiver
mv -f walarchiver /usr/local/bin/walarchiver
cd -
return 0
}
#--------------------------------------------------------------#
# Name: install_walarchiver
# Desc: install walarchiver service to systemctl
# Note: Assume viable walarchiver binary in /usr/local/bin/walarchiver
# Note: Run this as root
# walarchiver conf file : /etc/walarchiver/env
# walarchiver binary : /usr/local/bin/walarchiver
# walarchiver service : /etc/systemd/system/walarchiver.service
#--------------------------------------------------------------#
function install_walarchiver() {
if [[ ! -x /usr/local/bin/walarchiver ]]; then
echo "warn: /usr/local/bin/walarchiver not found, download"
download_walarchiver
if [[ $? != 0 ]]; then
echo "error: download walarchiver failed"
return $?
fi
fi
echo "warn: install walarchiver will clean up /pg/arcwal"
rm -rf /pg/arcwal/*
mkdir /etc/walarchiver
# services parameter
if [[ -f /opt/conf/walarchiver.env ]]; then
echo "info: found /opt/conf/walarchiver.env , copy walarchiver.env to /etc/walarchiver/env"
rm -rf /etc/walarchiver/env
cp /opt/conf/walarchiver.env /etc/walarchiver/env
else
cat > /etc/walarchiver/env <<- EOF
DBNAME='postgres://[email protected]/postgres'
WALDIR=/pg/arcwal
SLOT_NAME=walarchiver
COMPRESS=4
EOF
fi
# init walarchiver services
if [[ -f /opt/conf/services/walarchiver.service ]]; then
echo "info: found walarchiver.services in /opt/conf, copy walarchiver.service to /etc/systemd/system/"
rm -rf /etc/systemd/system/walarchiver.service
cp -f /opt/conf/services/walarchiver.service /etc/systemd/system/walarchiver.service
else
echo "info: overwrite /etc/systemd/system/walarchiver.service"
cat > /etc/systemd/system/walarchiver.service <<- EOF
[Unit]
Description=PostgreSQL WAL Archiver
Wants=network-online.target
After=network-online.target
[Service]
Type=forking
User=postgres
Group=postgres
EnvironmentFile=/etc/walarchiver/env
ExecStartPre=-"/bin/mkdir -p ${WALDIR}"
ExecStartPre=-"/bin/chown -R postgres:postgres ${WALDIR}"
ExecStart=/usr/local/bin/walarchiver start -d ${DBNAME} -D ${WALDIR} -S ${SLOT_NAME} -Z ${COMPRESS}
ExecStop=/usr/local/bin/walarchiver stop
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
fi
chown -R postgres:postgres /etc/systemd/system/walarchiver.service /etc/walarchiver
systemctl daemon-reload
return 0
}
#--------------------------------------------------------------#
# Name: launch_walarchiver
# Desc: launch walarchiver service
# Note: Assume walarchiver.service installed
#--------------------------------------------------------------#
function launch_walarchiver(){
if ! systemctl | grep walarchiver.service; then
echo "info: walarchiver.service not found"
install_walarchiver
fi
systemctl stop walarchiver > /dev/null 2>&1
systemctl enable walarchiver > /dev/null 2>&1
systemctl restart walarchiver > /dev/null 2>&1
if [[ $? != 0 ]]; then
systemctl status walarchiver
return 5
fi
# Double check
if systemctl status walarchiver > /dev/null 2>&1; then
echo "info: start walarchiver.service"
else
echo "error: fail to start walarchiver.service"
fi
return 0
}
#==============================================================#
# Main #
#==============================================================#
function main(){
if [[ $(whoami) != "root" ]]; then
echo "error: install walarchiver require root"
return 1
fi
# by default, walarchiver will not launch until postgres
# offline instance is proper inited. (which may change
# the target url of walarchiver )
local action=${1-''}
case ${action} in
download ) shift; download_walarchiver $@ ;;
install ) shift; install_walarchiver $@ ;;
launch ) shift; launch_walarchiver $@ ;;
* ) install_walarchiver $@ ;;
esac
return $?
}
#==============================================================#
# Main #
#==============================================================#
# Args:
# $1 action: download | install | launch (install by default)
#
# Code:
# 0 ok
# 1 insufficient privilege
# 2 walarchiver not found
# 3 download walarchiver failed
# 4 walarchiver not found
# 5 launch walarchiver failed
#==============================================================#
main $@