-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfb_pgsql_backup.sh
449 lines (370 loc) · 10.8 KB
/
fb_pgsql_backup.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
#!/bin/bash
# Copyright (c) 2017 eniorm
# BSD Licence
#
# SIMPLE BASH SHELL SCRIPT TO BACKUP DATABASES
# AND SEND IT TO A NFS STORAGE VOLUME
#
# ALL SUGGESTIONS, COMMENTS AND IMPROVEMENTS ARE WELCOME
#
#
# set -x # UNCOMMENT TO RUNNING IN DEBUG MODE
# set -n # UNCOMMENT TO SINTAX CHECK WITHOUT RUNNING
#
# LIST OF FIREBIRD DATABASES ALIASES
BANCOS_FB15="aspMateriais aspLeis"
BANCOS_FB21="aspReceitas aspCemiterio aspSequencia aspDigitalizacao"
BANCOS_FB25="aspRH aspCompras aspContabil aspPatrimonio aspFrota aspProcesso aspPrestacao"
# LIST OF PGSQL DATABASES NAMES
BANCOS_PGSQL="saude saude-log"
BANCOS_PGSQL+=" educacao educacao-log chat-educacao"
BANCOS_PGSQL+=" compras compras-log"
BANCOS_PGSQL+=" rhweb rh-log"
BANCOS_PGSQL+=" receitas"
BANCOS_PGSQL+=" social social-log"
BANCOS_PGSQL+=" jasperserver postgres"
# FOR DB AUTHENTICATION
ISC_USER="<username>"
ISC_PASS="<pwd>"
export PGPASSWORD="<pwd>"
# OUTRAS MACROS
EMAIL="somebox@someent.tld"
ARGS="-b -v -t -g -ig -mode read_write"
DIR="/somedir/backups"
DATA=$(date +%Y_%m_%d)
MES=$(date +%Y_%m)
DESTINO="${DIR}/${MES}/${DATA}"
LOG="${DESTINO}"
IDX_SQL="/root/scripts/indices_firebird.sql"
NETWORK="NET-IP/NET-BITMASK"
# TAR COMMAND AND FLAGS
TAR="$(which tar)"
TAR+=" -c -z -p -f"
# STORAGE
STORAGE_IP="STORAGE-IP"
STORAGE_DIR="/share/CACHEDEV1_DATA/Download"
VOLUME="/somedir/storage"
STORAGE_DESTINO="${VOLUME}/BKP_assessor/${MES}/${DATA}"
# MOUNT COMMAND AND FLAGS
MOUNT="$(which mount)"
UMOUNT="$(which umount)"
MOUNT_FLAGS="-t nfs -o rw,tcp,noatime"
function zerar_logs()
{
# COMPRESS FIREBIRD FILES AND FILES
# AND EMPTY IT
${TAR} "${DESTINO}/firebird_logs.tgz" \
/opt/firebird/firebird.log \
/opt/firebird156/firebird.log \
/opt/firebird214/firebird.log
echo > /opt/firebird/firebird.log
echo > /opt/firebird156/firebird.log
echo > /opt/firebird214/firebird.log
}
function bancos_acao()
{
# STOP ALL FIREBIRD SERVICES, USING RC SCRIPTS
# DO A FORCED STOP WITH KILL 15 AND KILL 9 IF
# EXISTS ANY REMAINING PROCESS
local RCD="/etc/init.d"
local LOG="$(mktemp)"
local NTS="$(which netstat)"
sync && sleep 10
case "$1" in
start)
"${RCD}/firebird156" $1
"${RCD}/firebird214" $1
"${RCD}/firebird255" $1
"${RCD}/postgresql" $1
sleep 5
${NTS} -ant | tee ${LOG} | egrep "3050|3053|3054" || mail -s "ERRO FIREBIRD serviços parados" ${EMAIL} < ${LOG}
;;
stop)
"${RCD}/firebird156" $1
"${RCD}/firebird214" $1
"${RCD}/firebird255" $1
"${RCD}/postgresql" $1
sleep 15
for PID in $(pgrep "fbserver|fbguard|fb_smp_server")
do
kill -15 ${PID}
done
sleep 15
for PID in $(pgrep "fbserver|fbguard|fb_smp_server")
do
kill -9 ${PID}
done
;;
*)
echo "Error"
exit 1
;;
esac
}
function reiniciar_bancos()
{
# RESTART DATABASE SERVICES
bancos_acao "stop"
bancos_acao "start"
}
function apenas_root()
{
# FORCE ONLY ROOT TO RUN THE SCRIPT
[ $(id -u) -ne "0" ] && echo "ERRO: deve ser root" && exit 1
}
function iptables_acao()
{
# IF THIS ACTION WAS SETTLED IN BLOCK MODE,
# WILL DROP ALL # ATTEMPTS FROM NETWORK TO
# CONNECT IN DATABASE SERVICES
local IPT="$(which iptables)"
case "$1" in
block)
${IPT} -A INPUT -p tcp -s ${NETWORK} --dport 3050 -j DROP
${IPT} -A INPUT -p tcp -s ${NETWORK} --dport 3053 -j DROP
${IPT} -A INPUT -p tcp -s ${NETWORK} --dport 3054 -j DROP
${IPT} -A INPUT -p tcp -s ${NETWORK} --dport 5432 -j DROP
;;
allow)
${IPT} -F
${IPT} -Z
;;
esac
}
# # #
# # # BELOW IS THE FUNCTIONS TO MAKE FIREBIRD BACKUPS
# # # IF GBAK RETURNS OK, THEN BACKUP FILE WILL BE COMPRESSED
# # # AND REMOVED
function backup_fb15()
{
local GBAK="/opt/firebird156/bin/gbak"
local GBAK_ARGS="${ARGS}"
cd ${DESTINO}
${GBAK} ${GBAK_ARGS} -user ${ISC_USER} -pass ${ISC_PASS} -y "${LOG}/$1.log" -se localhost/3050:service_mgr $1 "${DESTINO}/$1.fbk"
[ $? == "0" ] && ${TAR} "$1.fbk.tgz" "$1.fbk" && rm -f "$1.fbk"
}
function backup_fb21()
{
local GBAK="/opt/firebird214/bin/gbak"
local GBAK_ARGS="${ARGS}"
cd ${DESTINO}
${GBAK} ${GBAK_ARGS} -user ${ISC_USER} -pass ${ISC_PASS} -y "${LOG}/$1.log" -se localhost/3053:service_mgr $1 "${DESTINO}/$1.fk2"
[ $? == "0" ] && ${TAR} "$1.fk2.tgz" "$1.fk2" && rm -f "$1.fk2"
}
function backup_fb25()
{
local GBAK="/opt/firebird/bin/gbak"
local GBAK_ARGS="${ARGS}"
cd ${DESTINO}
${GBAK} ${GBAK_ARGS} -user ${ISC_USER} -pass ${ISC_PASS} -y "${LOG}/$1.log" -se localhost/3054:service_mgr $1 "${DESTINO}/$1.fk2"
[ $? == "0" ] && ${TAR} "$1.fk2.tgz" "$1.fk2" && rm -f "$1.fk2"
}
# # #
# # # BELOW IS THE FUNCTIONS TO DO A GBAK-SWEEP IN FIREBIRD DATABASES
# # #
function sweep_fb15()
{
local BIN="/opt/firebird156/bin/gfix"
local SWEEP_LOG="$(mktemp)"
cd ${DESTINO}
${BIN} -sweep -user ${ISC_USER} -pass ${ISC_PASS} "localhost/3050:$1" 2> ${SWEEP_LOG} || mail -s "Erro Gfix: $1" ${EMAIL} < ${SWEEP_LOG}
rm -f ${SWEEP_LOG}
}
function sweep_fb21()
{
local BIN="/opt/firebird214/bin/gfix"
local SWEEP_LOG="$(mktemp)"
cd ${DESTINO}
${BIN} -sweep -user ${ISC_USER} -pass ${ISC_PASS} "localhost/3053:$1" 2> ${SWEEP_LOG} || mail -s "Erro Gfix: $1" ${EMAIL} < ${SWEEP_LOG}
rm -f ${SWEEP_LOG}
}
function sweep_fb25()
{
local BIN="/opt/firebird/bin/gfix"
local SWEEP_LOG="$(mktemp)"
cd ${DESTINO}
${BIN} -sweep -user ${ISC_USER} -pass ${ISC_PASS} "localhost/3054:$1" 2> ${SWEEP_LOG} || mail -s "Erro Gfix: $1" ${EMAIL} < ${SWEEP_LOG}
rm -f ${SWEEP_LOG}
}
# # #
# # # BELOW IS THE FUNCTIONS TO RUN A SQL SCRIPT TO MAINTAIN FIREBIRD INDEXES TO DATABASES
# # #
function manut_index_fb15()
{
local BIN="/opt/firebird156/bin/isql"
local IDX_LOG=$(mktemp)
cd ${DESTINO}
${BIN} -user ${ISC_USER} -pass ${ISC_PASS} "localhost/3050:$1" -i ${IDX_SQL} 2> ${IDX_LOG} || mail -s "Erro script: $1" ${EMAIL} < ${IDX_LOG}
rm -f ${IDX_LOG}
}
function manut_index_fb21()
{
local BIN="/opt/firebird214/bin/isql"
local IDX_LOG=$(mktemp)
cd ${DESTINO}
${BIN} -user ${ISC_USER} -pass ${ISC_PASS} "localhost/3053:$1" -i ${IDX_SQL} 2> ${IDX_LOG} || mail -s "Erro script: $1" ${EMAIL} < ${IDX_LOG}
rm -f ${IDX_LOG}
}
function manut_index_fb25()
{
local BIN="/opt/firebird/bin/isql"
local IDX_LOG=$(mktemp)
cd ${DESTINO}
${BIN} -user ${ISC_USER} -pass ${ISC_PASS} "localhost/3054:$1" -i ${IDX_SQL} 2> ${IDX_LOG} || mail -s "Erro script: $1" ${EMAIL} < ${IDX_LOG}
rm -f ${IDX_LOG}
}
# # #
# # # BELOW IS THE FUNCTIONS TO DO A MAINTENANCE IN FIREBIRD DATABASES
# # # USE THIS WITH CARE
# # #
function gfix_fb15()
{
local GFIX="/opt/firebird156/bin/gfix"
local LOG="$(mktemp)"
${GFIX} -user ${ISC_USER} -pass ${ISC_PASS} -validate -full localhost/3050:$1 > ${LOG} 2>&1
[ -s ${LOG} ] && mail -s "GFIX erro $1 ${DATA}" ${EMAIL} < ${LOG}
rm -f ${LOG}
}
function gfix_fb21()
{
local GFIX="/opt/firebird214/bin/gfix"
local LOG="$(mktemp)"
${GFIX} -user ${ISC_USER} -pass ${ISC_PASS} -validate -full localhost/3053:$1 > ${LOG} 2>&1
[ -s ${LOG} ] && mail -s "GFIX erro $1 ${DATA}" ${EMAIL} < ${LOG}
rm -f ${LOG}
}
function gfix_fb25()
{
local GFIX="/opt/firebird/bin/gfix"
local LOG="$(mktemp)"
${GFIX} -user ${ISC_USER} -pass ${ISC_PASS} -validate -full localhost/3054:$1 > ${LOG} 2>&1
[ -s ${LOG} ] && mail -s "GFIX erro $1 ${DATA}" ${EMAIL} < ${LOG}
rm -f ${LOG}
}
# # #
# # # CONTINUING...
# # #
function backup_pgsql()
{
# CREATE A DUMP FILE FROM PGSQL DATABASE
# IF RETURNS OK, THEN COMPRESS THE DUMP FILE
local PGDUMP="/opt/pgsql_8_4/bin/pg_dump -b -h localhost -U assessorpublico -f"
local PG_LOG="$(mktemp)"
${PGDUMP} "${DESTINO}/pg_$1.dump" $1 2> ${PG_LOG} || mail -s "Erro PGSQL: $1" ${EMAIL} < ${PG_LOG}
[ -s "${DESTINO}/pg_$1.dump" ] && ${TAR} "${DESTINO}/pg_$1.dump.tgz" "${DESTINO}/pg_$1.dump" && rm -f "${DESTINO}/pg_$1.dump"
rm -f ${PG_LOG}
}
function email_logs()
{
# GET LAST LINE FROM FIREBIRD GBAK LOGS AND SEND TO EMAIL
tail -n1 ${DESTINO}/*.log | mail -s "GBAK relatorio $DATA" ${EMAIL}
}
function excluir_bkps_antigos()
{
# WILL REMOVE OLD BACKUP FILES FROM 3 LAST DAYS
cd ${DIR}
find ${DIR} -type f -ctime +3d -exec rm -f {} \;
find ${DIR} -type f -atime +3d -exec rm -f {} \;
}
function montar_desmontar_nfs()
{
# FUNCTION TO MOUNT AND UMOUNT A NFS VOLUME FROM STORAGE
case "$1" in
"montar")
${MOUNT} ${MOUNT_FLAGS} ${STORAGE_IP}:${STORAGE_DIR} ${VOLUME}
if [ $? -ne 0 ] ; then
echo "ERRO: nao foi possivel montar o volume NFS"
exit 1
fi
;;
"desmontar")
if [ $( ${MOUNT} | grep "${VOLUME}" | wc -l ) -ne 0 ] ; then
sync
${UMOUNT} ${VOLUME}
fi
;;
esac
}
function enviar_backups_storage()
{
# TO SEND BACKUP FILES TO STORAGE
#
# IF NFS IS MOUNTED, UMOUNT THEN FIRST
montar_desmontar_nfs "desmontar"
# MOUNT THE NFS VOLUME
montar_desmontar_nfs "montar"
# CREATE A SERVER CONFIG BACKUP
${TAR} ${DESTINO}/dbserver_conf.tgz /etc /root/scripts
mkdir -p ${STORAGE_DESTINO} 2> /dev/null
# LOOP FOR COPY ALL FILES FROM LOCAL TO STORAGE VOLUME
# IF NECESSARY, REMOVE BACKUPS FROM LOCAL
cd ${DESTINO}
for ARQUIVO in *;
do
cp "${DESTINO}/${ARQUIVO}" "${STORAGE_DESTINO}/"
# [ $? == 0 ] && rm -f "${ARQUIVO}" # UNCOMMENT TO REMOVE BACKUPS FROM LOCAL
done
}
# # #
# # # THE EXEC OF SCRIPT STARTS BELOW
# # #
# 0) CHECK IF IS ROOT
apenas_root;
# 1) CREATE A LOCAL FOLDER DO SEND BACKUP FILES
mkdir -p ${DESTINO} 2> /dev/null
# 2) REMOVE OLD BACKUP FILES
excluir_bkps_antigos;
# 3) STOP DATABASE SERVICES
bancos_acao "stop";
# 4) EMPTY THE LOGS
zerar_logs;
# 5) DROP CONNECTIONS
iptables_acao "block";
# 6) START DATABASE SERVICES
bancos_acao "start";
# 7) START OF FIREBIRD 1.5 BACKUP SERVICES ON EARCH DATABASE
for BANCO in ${BANCOS_FB15}
do
backup_fb15 ${BANCO}
sweep_fb15 ${BANCO}
gfix_fb15 $BANCO
manut_index_fb15 ${BANCO}
done
unset ${BANCO}
# 8) START OF FIREBIRD 2.1 BACKUP SERVICES ON EARCH DATABASE
for BANCO in ${BANCOS_FB21}
do
backup_fb21 ${BANCO}
sweep_fb21 ${BANCO}
gfix_fb21 ${BANCO}
manut_index_fb21 ${BANCO}
done
unset ${BANCO}
# 9) START OF FIREBIRD 2.5 BACKUP SERVICES ON EARCH DATABASE
for BANCO in ${BANCOS_FB25}
do
backup_fb25 ${BANCO}
sweep_fb25 ${BANCO}
gfix_fb25 ${BANCO}
manut_index_fb25 ${BANCO}
done
unset ${BANCO}
# 10) START OF PGSQL BACKUP SERVICES ON EARCH DATABASE
for BANCO in ${BANCOS_PGSQL}
do
backup_pgsql ${BANCO}
done
unset ${BANCO}
# 11) SEND ALL LOGS TO EMAIL
email_logs;
# 12) SEND BACKUP FILES TO STORAGE
enviar_backups_storage;
# 13) UMOUNT NFS VOLUME
montar_desmontar_nfs "desmontar";
# 14) RESTART DATABASE SERVICES
reiniciar_bancos;
# 15) EMPTY IPTABLES RULES TO ALLOW CONNECTIONS AGAIN
iptables_acao "allow";
# END
exit 0
# ALL SUGGESTIONS, COMMENTS AND IMPROVEMENTS ARE WELCOME