forked from robbrucks/zabbix-postgresql-auto-partitioning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUNinstall_partitioning.sh
executable file
·175 lines (148 loc) · 5.54 KB
/
UNinstall_partitioning.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
#!/bin/bash
dbname=zabbix
#####################################################################
# UNinstall_partitioning.sh
# Removes PostgreSQL partitioning for Zabbix history and trend tables
#
# Adapted From:
# https://www.zabbix.org/wiki/Docs/howto/zabbix2_postgresql_autopartitioning
#
# - Must be run as DB super-user
#####################################################################
main () {
echo '================================================================'
date +"%Y-%m-%d %H:%M:%S %Z"
echo "Logging to: ${logfile}"
echo "Settings:"
echo " dbname=${dbname}"
drop_partition_triggers || exit 1
drop_trigger_function || exit 1
echo 'BEGIN;' > copy_and_drop.sql
gen_copy_sql >> copy_and_drop.sql || exit 1
echo 'DROP SCHEMA partitions CASCADE;' >> copy_and_drop.sql
echo 'DROP FUNCTION zbx_part_cleanup_func(interval, text);' >> copy_and_drop.sql
echo 'COMMIT;' >> copy_and_drop.sql
echo "${notice1}"
}
drop_partition_triggers () {
echo 'drop_partitions ------------------------------------------------'
date +"%Y-%m-%d %H:%M:%S %Z"
psql -Xe -v ON_ERROR_STOP=on ${dbname} <<EOF
DROP TRIGGER zbx_partition_trg ON history;
DROP TRIGGER zbx_partition_trg ON history_uint;
DROP TRIGGER zbx_partition_trg ON history_str;
DROP TRIGGER zbx_partition_trg ON history_text;
DROP TRIGGER zbx_partition_trg ON history_log;
DROP TRIGGER zbx_partition_trg ON trends;
DROP TRIGGER zbx_partition_trg ON trends_uint;
EOF
rc=$?
if [[ $rc -eq 0 ]]; then
echo "================================"
echo " Triggers successfully dropped."
echo "================================"
fi
return $rc
}
drop_trigger_function () {
echo 'drop_trigger_function ------------------------------------------'
date +"%Y-%m-%d %H:%M:%S %Z"
psql -Xe -v ON_ERROR_STOP=on ${dbname} <<EOF
DROP FUNCTION zbx_part_trigger_func();
EOF
rc=$?
if [[ $rc -eq 0 ]]; then
echo "========================================"
echo " Trigger function successfully dropped."
echo "========================================"
fi
return $rc
}
gen_copy_sql () {
echo '-- gen_copy_sql ---------------------------------------------------'
date +"-- %Y-%m-%d %H:%M:%S %Z"
psql -Xqt -v ON_ERROR_STOP=on ${dbname} <<EOF | egrep -v '^$'
SELECT 'INSERT INTO public.history SELECT * from partitions.' || table_name ||';'
FROM information_schema.tables
WHERE table_schema = 'partitions'
AND table_name LIKE 'history_p%'
ORDER BY 1;
SELECT 'INSERT INTO public.history_log SELECT * from partitions.' || table_name ||';'
FROM information_schema.tables
WHERE table_schema = 'partitions'
AND table_name LIKE 'history_log_p%'
ORDER BY 1;
SELECT 'INSERT INTO public.history_str SELECT * from partitions.' || table_name ||';'
FROM information_schema.tables
WHERE table_schema = 'partitions'
AND table_name LIKE 'history_str_p%'
ORDER BY 1;
SELECT 'INSERT INTO public.history_text SELECT * from partitions.' || table_name ||';'
FROM information_schema.tables
WHERE table_schema = 'partitions'
AND table_name LIKE 'history_text_p%'
ORDER BY 1;
SELECT 'INSERT INTO public.history_uint SELECT * from partitions.' || table_name ||';'
FROM information_schema.tables
WHERE table_schema = 'partitions'
AND table_name LIKE 'history_uint_p%'
ORDER BY 1;
SELECT 'INSERT INTO public.trends SELECT * from partitions.' || table_name ||';'
FROM information_schema.tables
WHERE table_schema = 'partitions'
AND table_name LIKE 'trends_p%'
ORDER BY 1;
SELECT 'INSERT INTO public.trends_uint SELECT * from partitions.' || table_name ||';'
FROM information_schema.tables
WHERE table_schema = 'partitions'
AND table_name LIKE 'trends_uint_p%'
ORDER BY 1;
EOF
return ${PIPESTATUS[0]}
}
#########
# SETUP #
#########
notice1='
============================================================
=========
NOTICE!!!
=========
The "daily_partition_cleanup.sh" function, "partitions"
schema, and partitioned tables were *NOT DROPPED*. You will
need to decide how to save the history and trend data.
You can either:
1. Run the generated "copy_and_drop.sql" SQL file against
the zabbix database (*NOT RECOMMENDED* for more than 1gb
of partitioned data).
*OR*
2. Allow the "daily_partition_cleanup.sh" script to continue
running from cron to automatically drop the partitions as
they age out.
If you choose option #2, you will need to execute the
following steps manually (weeks or months later) after all
the partitions been dropped by the cleanup script:
* Remove the cleanup job from the crontab
* Drop the "partitions" schema and the cleanup function
The SQL for option #1 has been written to file
"copy_and_drop.sql" in your current working directory.
=================
BE SURE TO...
=================
If the housekeeper was disabled in Zabbix, be sure to
re-enable it to purge the history and trend data from the
main tables.
============================================================
'
abspath=`cd ${0%/*};pwd` # get absolute path of script directory
logdir=${abspath}/logs # set log directory under script directory
logfile=${logdir}/UNinstall_partitioning.$(date "+%Y-%m-%d_%H.%M").log
# create log subdirectory if does not exist
if [[ ! -d ${logdir} ]]; then
mkdir -p ${logdir}
if [[ $? -ne 0 ]]; then
echo "ERROR: unable to create log directory \"${logdir}\"" >&2
exit 2
fi
fi
main 2>&1 | tee -a ${logfile}