-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathS72postgresql
executable file
·102 lines (82 loc) · 2.44 KB
/
S72postgresql
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
#!/bin/bash
BOOT_CONF="/boot/postgresql.conf"
SYS_CONF="/etc/postgresql.conf"
CONF="/data/etc/postgresql.conf"
PROG="/usr/bin/pg_ctl"
DB_DIR="/var/lib/postgresql"
USER="postgres"
LOG="/var/log/postgresql.log"
DUMP_FILE="/var/lib/postgresql-dump.sql.gz"
function run_pg_ctl() {
su ${USER} -c "${PROG} $*"
}
test -x ${PROG} || exit 0
test -n "${OS_VERSION}" || source /etc/init.d/base
prepare_conf ${CONF} ${SYS_CONF} ${BOOT_CONF}
function start() {
# Remove postgresql data dir if bin/data version mismatch
if [[ -f ${DB_DIR}/PG_VERSION ]]; then
data_version=$(cat ${DB_DIR}/PG_VERSION)
bin_version=$(${PROG} -V | cut -d ' ' -f 3 | cut -d . -f 1)
if [[ "${data_version}" != "${bin_version}" ]]; then
msg_begin "Backing up old postgresql data directory"
rm -rf ${DB_DIR}.old
mv ${DB_DIR} ${DB_DIR}.old
msg_done
fi
fi
mkdir -p ${DB_DIR}
chown -R ${USER} ${DB_DIR}
touch ${LOG}
chown ${USER} ${LOG}
cd ${DB_DIR}
# Initialize data dir if not present
fresh=
if ! [[ -f ${DB_DIR}/PG_VERSION ]]; then
msg_begin "Initializing postgresql data directory"
run_pg_ctl initdb -s -D ${DB_DIR} -o \'-E UTF-8 --no-locale\' &>> ${LOG}
test $? == 0 && msg_done || msg_fail
echo "include_if_exists = '${CONF}'" >> ${DB_DIR}/postgresql.conf
fresh=true
fi
# Start server
msg_begin "Starting postgresql"
run_pg_ctl start -s -D ${DB_DIR} -l ${LOG}
test $? == 0 && msg_done || msg_fail
# Restore from dump if present
if [[ -f ${DUMP_FILE} ]] && [[ -n "${fresh}" ]]; then
msg_begin "Restoring postgresql data from dump"
if gzip -t ${DUMP_FILE} &>/dev/null; then
# Remove any existing old DB backup directory to make space
rm -rf ${DB_DIR}.old
if gunzip < ${DUMP_FILE} | psql -U ${USER} &>> ${LOG}; then
msg_done
rm ${DUMP_FILE}
else
msg_fail
fi
else
msg_fail "invalid dump file"
fi
fi
}
function stop() {
msg_begin "Stopping postgresql"
run_pg_ctl stop -s -D ${DB_DIR} -m fast &>> ${LOG}
test $? == 0 && msg_done || msg_fail
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac