forked from prodrigestivill/docker-postgres-backup-local
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.sh
More file actions
executable file
·377 lines (282 loc) · 9 KB
/
backup.sh
File metadata and controls
executable file
·377 lines (282 loc) · 9 KB
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
#!/usr/bin/env bash
set -Eeo pipefail
source log.sh
export LOGDIR="${BACKUP_DIR}/logs"
export DATETIME=`date +"%Y%m%d_%H%M%S"`
export matrix_verbosity=${BACKUP_MATRIX_VERBOSITY}
export verbosity=${BACKUP_VERBOSITY}
export ELEMENT_SERVER="${BACKUP_ELEMENT_SERVER}"
export ROOM_ID="${BACKUP_ROOM_ID}"
export ACCESS_TOKEN="${BACKUP_ACCESS_TOKEN}"
export PGHOST="${POSTGRES_HOST}"
export PGPORT="${POSTGRES_PORT}"
KEEP_MINS=${BACKUP_KEEP_MINS}
KEEP_DAYS=`expr $((${BACKUP_KEEP_DAYS} - 1))`
KEEP_WEEKS=`expr $(((${BACKUP_KEEP_WEEKS} * 7) + 1))`
KEEP_MONTHS=`expr $(((${BACKUP_KEEP_MONTHS} * 31) + 1))`
WEEK_DAY="${BACKUP_WEEK_DAY}"
MONTH_DAY="${BACKUP_MONTH_DAY}"
Log_Open
edebug "Starting To Setup Environment..."
HOOKS_DIR="/hooks"
if [ -d "${HOOKS_DIR}" ]; then
on_error(){
run-parts -a "error" "${HOOKS_DIR}"
}
trap 'on_error' ERR
fi
if [ "${POSTGRES_DB}" = "**None**" -a "${POSTGRES_DB_FILE}" = "**None**" ]; then
eerror "You need to set the POSTGRES_DB or POSTGRES_DB_FILE environment variable."
exit 1
fi
if [ "${POSTGRES_HOST}" = "**None**" ]; then
if [ -n "${POSTGRES_PORT_5432_TCP_ADDR}" ]; then
POSTGRES_HOST=${POSTGRES_PORT_5432_TCP_ADDR}
POSTGRES_PORT=${POSTGRES_PORT_5432_TCP_PORT}
else
eerror "You need to set the POSTGRES_HOST environment variable."
exit 1
fi
fi
if [ "${POSTGRES_USER}" = "**None**" -a "${POSTGRES_USER_FILE}" = "**None**" ]; then
eerror "You need to set the POSTGRES_USER or POSTGRES_USER_FILE environment variable."
exit 1
fi
if [ "${POSTGRES_PASSWORD}" = "**None**" -a "${POSTGRES_PASSWORD_FILE}" = "**None**" -a "${POSTGRES_PASSFILE_STORE}" = "**None**" ]; then
eerror "You need to set the POSTGRES_PASSWORD or POSTGRES_PASSWORD_FILE or POSTGRES_PASSFILE_STORE environment variable or link to a container named POSTGRES."
exit 1
fi
edebug "Starting To Process Variables..."
#Process vars
if [ "${POSTGRES_DB_FILE}" = "**None**" ]; then
POSTGRES_DBS=$(echo "${POSTGRES_DB}" | tr , " ")
elif [ -r "${POSTGRES_DB_FILE}" ]; then
POSTGRES_DBS=$(cat "${POSTGRES_DB_FILE}")
else
eerror "Missing POSTGRES_DB_FILE file."
exit 1
fi
if [ "${POSTGRES_USER_FILE}" = "**None**" ]; then
export PGUSER="${POSTGRES_USER}"
elif [ -r "${POSTGRES_USER_FILE}" ]; then
export PGUSER=$(cat "${POSTGRES_USER_FILE}")
else
eerror "Missing POSTGRES_USER_FILE file."
exit 1
fi
if [ "${POSTGRES_PASSWORD_FILE}" = "**None**" -a "${POSTGRES_PASSFILE_STORE}" = "**None**" ]; then
export PGPASSWORD="${POSTGRES_PASSWORD}"
elif [ -r "${POSTGRES_PASSWORD_FILE}" ]; then
export PGPASSWORD=$(cat "${POSTGRES_PASSWORD_FILE}")
elif [ -r "${POSTGRES_PASSFILE_STORE}" ]; then
export PGPASSFILE="${POSTGRES_PASSFILE_STORE}"
else
eerror "Missing POSTGRES_PASSWORD_FILE or POSTGRES_PASSFILE_STORE file."
exit 1
fi
if [ $matrix_verbosity -gt 0 ]
then
if [ "${BACKUP_ELEMENT_SERVER}" = "**None**" ]; then
eerror "You need to set the BACKUP_ELEMENT_SERVER environment variable or set BACKUP_MATRIX_VERBOSITY to 0."
exit 1
fi
if [ "${BACKUP_ROOM_ID}" = "**None**" ]; then
eerror "You need to set the BACKUP_ROOM_ID environment variable or set BACKUP_MATRIX_VERBOSITY to 0."
exit 1
fi
if [ "${BACKUP_ACCESS_TOKEN}" = "**None**" ]; then
eerror "You need to set the BACKUP_ACCESS_TOKEN environment variable or set BACKUP_MATRIX_VERBOSITY to 0."
exit 1
fi
fi
edebug "...Finished Processing Variables"
# Pre-backup hook
if [ -d "${HOOKS_DIR}" ]; then
run-parts -a "pre-backup" --exit-on-error "${HOOKS_DIR}"
fi
#Initialize dirs
FREQUENCY=( last daily weekly monthly )
for f in ${FREQUENCY[@]}
do
mkdir -p "${BACKUP_DIR}/${f}/"
done
MONTH_DAY=`date +%d`
WEEK_DAY=`date +%A`
edebug "...Finished Setting Up Environment"
#Create Backups
create_backups () {
edebug "${POSTGRES_DBS[@]}"
for DB in ${POSTGRES_DBS}
do
edebug "Backing up Database: ${DB}"
LAST_FILENAME="${DB}-`date +%Y%m%d-%H%M%S`${BACKUP_SUFFIX}"
FILE="${BACKUP_DIR}/last/${LAST_FILENAME}"
create_dump
einfo "Point last backup file to this last backup..."
ln -svf "${LAST_FILENAME}" "${BACKUP_DIR}/last/${DB}-latest${BACKUP_SUFFIX}"
create_hardlinks "${FILE}" "daily"
if [ "${BACKUP_MONTH_DAY}" = "${MONTH_DAY}" ]
then
create_hardlinks "${FILE}" "monthly"
elif [ "${BACKUP_WEEK_DAY}" = "${WEEK_DAY}" ]
then
create_hardlinks "${FILE}" "weekly"
fi
einfo "SQL backup created successfully"
done
# Post-backup hook
if [ -d "${HOOKS_DIR}" ]; then
run-parts -a "post-backup" --reverse --exit-on-error "${HOOKS_DIR}"
fi
}
#Create dump
create_dump () {
if [ "${POSTGRES_CLUSTER}" = "TRUE" ]; then
einfo "Creating cluster dump of ${DB} database from ${POSTGRES_HOST}..."
pg_dumpall -l "${DB}" ${POSTGRES_EXTRA_OPTS} | gzip > "${FILE}" || echo ecrit "Postgres Dump Failed"
else
einfo "Creating dump of ${DB} database from ${POSTGRES_HOST}..."
pg_dump -d "${DB}" -f "${FILE}" ${POSTGRES_EXTRA_OPTS} || echo ecrit "Postgres Dump Failed"
fi
}
create_hardlinks () {
SRC=$1
INCREMENT=$2
edebug "Source: ${SRC}"
edebug "Increment: ${INCREMENT}"
if [ "${INCREMENT}" = "daily" ]
then
FILENAME="${DB}-`date +%Y%m%d`${BACKUP_SUFFIX}"
elif [ "${INCREMENT}" = "weekly" ]
then
FILENAME="${DB}-`date +%G%V`${BACKUP_SUFFIX}"
elif [ "${INCREMENT}" = "monthly" ]
then
FILENAME="${DB}-`date +%Y%m`${BACKUP_SUFFIX}"
fi
DEST="${BACKUP_DIR}/${INCREMENT}/${FILENAME}"
edebug "Destionation: ${DEST}"
edebug "Creating Linking..."
#Copy (hardlink) for each entry
if [ -d "${SRC}" ]
then
DESTNEW="${DEST}-new"
rm -rf "${DESTNEW}"
mkdir "${DESTNEW}"
ln -f "${SRC}/"* "${DESTNEW}/"
rm -rf "${DEST}"
einfo "Replacing ${INCREMENT} backup ${DEST} file this last backup..."
mv "${DESTNEW}" "${DEST}"
else
einfo "Replacing ${INCREMENT} backup ${DEST} file this last backup..."
ln -vf "${SRC}" "${DEST}"
fi
# Update latest symlinks
einfo "Replacing lastest ${INCREMENT} backup to this last backup..."
ln -svf "${DEST}" "${BACKUP_DIR}/${INCREMENT}/${DB}-latest"
edebug "...Link has been created"
}
#Clean up old backups
cleanup_backups () {
einfo "Starting to clean up old backups..."
for folder in "${FREQUENCY[@]}"
do
if [ $folder == 'last' ]
then
time=$KEEP_MINS
unit="minutes"
keep=1
elif [ $folder == 'daily' ]
then
time=$KEEP_DAYS
unit="days"
keep=(${KEEP_DAYS})
elif [ $folder == "weekly" ]
then
time=$KEEP_WEEKS
unit="days"
keep=`expr $(((${KEEP_WEEKS} - 1) / 7))`
elif [ $folder == 'monthly' ]
then
time=$KEEP_MONTHS
unit="days"
keep=`expr $(((${KEEP_MONTHS} - 1) / 31))`
fi
edebug "Keeping $keep backups over the past $time $unit"
if [[ $time -gt 0 ]]
then
for DB in ${POSTGRES_DBS}
do
#Clean old files
edebug "Cleaning up backups of $DB database..."
local all=( `find "${BACKUP_DIR}/${folder}" -maxdepth 1 -name "${DB}-*" | sort -t/ -k3` )
local files=()
number=$((${#all[@]}-$keep))
einfo "Number of Backups to be deleted: $number"
if [[ $number -lt 0 ]]
then
ecrit "Only ${#all[@]} Backups exist for ${DB} and you want to keep $keep."
ecrit "If you have just started taking backups you may ignore this"
ecrit "Otherwise you may want to investigate why backups are not being taken"
elif [[ $number -gt 0 ]]
then
edebug "Getting Current Time and Date"
local date=$(date +%Y%m%d --date "$time $unit ago")
date=$(date -d "$date")
einfo "Cleaning files older than $date in ${folder} for ${DB} database from ${POSTGRES_HOST}..."
date=$(date -d "$date" +%s)
for backup in ${all[@]}
do
edebug "Checking Modified Date of Backup"
local filemod=$(date -r "$backup" +%s)
einfo "Checking Backup: $backup"
einfo "File Last Modified: $(date -r $backup)"
if [[ $date -ge $filemod ]]
then
edebug "Mark Backup for Deletion"
files=( $backup )
((number--))
fi
if [[ $number -le 0 ]]
then
edebug "...All Extra Backups have been marked for deletion"
break
fi
done
fi
if [ "${#files[@]}" -gt 0 ]
then
for file in "${files[@]}"
do
einfo "Deleting Backup: $file"
rm -r $file
done
fi
done
fi
done
}
for arg in "$@"; do
case $arg in
-a)
einfo "Starting Backup..."
create_backups
cleanup_backups
;;
-b)
einfo "Starting Backup..."
create_backups
;;
-c)
einfo "Starting Cleanning Up Old Backups..."
cleanup_backups
;;
*)
einfo "Starting Backup..."
create_backups
cleanup_backups
;;
esac
done
edebug "...Backup Finished"
Log_Close