-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.backup_db.sh
More file actions
317 lines (270 loc) · 10 KB
/
Copy pathapp.backup_db.sh
File metadata and controls
317 lines (270 loc) · 10 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
#!/usr/bin/env -S bash -euo pipefail
# -------------------------------------------------------------------------------------------------------------------- #
# DATABASE BACKUP
# Backup of PostgreSQL and MariaDB databases.
# -------------------------------------------------------------------------------------------------------------------- #
# @package Bash
# @author Kai Kimera <[email protected]>
# @license MIT
# @version 0.1.0
# @link https://libsys.ru/ru/2025/05/57f8f8c0-b963-5708-b310-129ea98a2423/
# -------------------------------------------------------------------------------------------------------------------- #
(( EUID != 0 )) && { echo >&2 'This script should be run as root!'; exit 1; }
# -------------------------------------------------------------------------------------------------------------------- #
# CONFIGURATION
# -------------------------------------------------------------------------------------------------------------------- #
# Sources.
SRC_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd -P )"
SRC_NAME="$( basename "$( readlink -f "${BASH_SOURCE[0]}" )" )"
# shellcheck source=/dev/null
. "${SRC_DIR}/${SRC_NAME%.*}.conf"
# Parameters.
DB_SRC=("${DB_SRC[@]:?}"); readonly DB_SRC
DB_USER="${DB_USER:?}"; readonly DB_USER
DB_PASS="${DB_PASS:?}"; readonly DB_PASS
FS_DST="${FS_DST:?}"; readonly FS_DST
FS_TPL="${FS_TPL:?}"; readonly FS_TPL
ENC_ON="${ENC_ON:?}"; readonly ENC_ON
ENC_APP="${ENC_APP:?}"; readonly ENC_APP
ENC_PASS="${ENC_PASS:?}"; readonly ENC_PASS
SSH_ON="${SSH_ON:?}"; readonly SSH_ON
SSH_HOST="${SSH_HOST:?}"; readonly SSH_HOST
SSH_USER="${SSH_USER:?}"; readonly SSH_USER
SSH_PASS="${SSH_PASS:?}"; readonly SSH_PASS
SSH_DST="${SSH_DST:?}"; readonly SSH_DST
SSH_MNT="${SSH_MNT:?}"; readonly SSH_MNT
RSYNC_ON="${RSYNC_ON:?}"; readonly RSYNC_ON
RSYNC_HOST="${RSYNC_HOST:?}"; readonly RSYNC_HOST
RSYNC_USER="${RSYNC_USER:?}"; readonly RSYNC_USER
RSYNC_PASS="${RSYNC_PASS:?}"; readonly RSYNC_PASS
RSYNC_DST="${RSYNC_DST:?}"; readonly RSYNC_DST
MAIL_ON="${MAIL_ON:?}"; readonly MAIL_ON
MAIL_FROM="${MAIL_FROM:?}"; readonly MAIL_FROM
MAIL_TO=("${MAIL_TO[@]:?}"); readonly MAIL_TO
GITLAB_ON="${GITLAB_ON:?}"; readonly GITLAB_ON
GITLAB_API="${GITLAB_API:?}"; readonly GITLAB_API
GITLAB_PROJECT="${GITLAB_PROJECT:?}"; readonly GITLAB_PROJECT
GITLAB_TOKEN="${GITLAB_TOKEN:?}"; readonly GITLAB_TOKEN
# Variables.
META="$( date '+%FT%T%:z' ) $( hostname -f ) ${SRC_NAME}"
LOG_BACKUP="${SRC_DIR}/log.db.backup"
LOG_CHECK="${SRC_DIR}/log.db.check"
LOG_CLEAN="${SRC_DIR}/log.db.clean"
LOG_MOUNT="${SRC_DIR}/log.db.mount"
LOG_SYNC="${SRC_DIR}/log.db.sync"
LOG_UMOUNT="${SRC_DIR}/log.db.umount"
FS_CHECK='.backup_db'
# -------------------------------------------------------------------------------------------------------------------- #
# -----------------------------------------------------< SCRIPT >----------------------------------------------------- #
# -------------------------------------------------------------------------------------------------------------------- #
function _error() {
echo "${META}: $*" >&2; exit 1
}
function _success() {
echo "${META}: $*" >&2
}
function _mail() {
(( ! "${MAIL_ON}" )) && return 0
local type; type="#type:backup:${1}"
local subj; subj="[$( hostname -f )] ${SRC_NAME}: ${2}"
local body; body="${3}"
local id; id="#id:$( hostname -f ):$( dmidecode -s 'system-uuid' )"
local ip; ip="#ip:$( hostname -I )"
local date; date="#date:$( date '+%FT%T%:z' )"
local opts; opts=('-S' 'v15-compat' '-s' "${subj}" '-r' "${MAIL_FROM}")
[[ "${MAIL_SMTP_SERVER:-}" ]] && opts+=(
'-S' "mta=${MAIL_SMTP_SERVER} smtp-use-starttls"
'-S' "smtp-auth=${MAIL_SMTP_AUTH:-none}"
)
opts+=('-.')
printf "%s\n\n-- \n%s\n%s\n%s\n%s" "${body}" "${id^^}" "${ip^^}" "${date^^}" "${type^^}" \
| s-nail "${opts[@]}" "${MAIL_TO[@]}"
}
function _gitlab() {
(( ! "${GITLAB_ON}" )) && return 0
local label; label="${1}"
local title; title="[$( hostname -f )] ${SRC_NAME}: ${2}"
local desc; desc="${3}"
local id; id="#id:$( hostname -f ):$( dmidecode -s 'system-uuid' )"
local ip; ip="#ip:$( hostname -I )"
local date; date="#date:$( date '+%FT%T%:z' )"
local type; type="#type:backup:${label}"
curl "${GITLAB_API}/projects/${GITLAB_PROJECT}/issues" -X 'POST' -kfsLo '/dev/null' \
-H "PRIVATE-TOKEN: ${GITLAB_TOKEN}" -H 'Content-Type: application/json' \
-d @- <<EOF
{
"title": "${title}",
"description": "${desc//\'/\`}\n\n---\n\n- \`${id^^}\`\n- \`${ip^^}\`\n- \`${date^^}\`\n- \`${type^^}\`",
"labels": "backup,database,${label}"
}
EOF
}
function _msg() {
_mail "${1}" "${2}" "${3}"
_gitlab "${1}" "${2}" "${3}"
case "${1}" in
'error') _error "${3}" ;;
'success') _success "${3}" ;;
*) _error "'MSG_TYPE' does not exist!" ;;
esac
}
function _mongo() {
local opts; opts=(
"--host=${DB_HOST:-127.0.0.1}"
"--port=${DB_PORT:-27017}"
"--username=${DB_USER:-root}"
"--password=${DB_PASS}"
"--authenticationDatabase=${DB_AUTH:-admin}"
"--oplog"
"--archive"
"--db=${1}"
)
mongodump "${opts[@]}"
}
function _mysql() {
local cmd; cmd='mariadb-dump'; [[ -x "$( command -v 'mysqldump' )" ]] && cmd='mysqldump'
local opts; opts=(
"--host=${DB_HOST:-127.0.0.1}"
"--port=${DB_PORT:-3306}"
"--user=${DB_USER:-root}"
"--password=${DB_PASS}"
"--databases=${1}"
)
(( "${MYSQL_ST:-1}" )) && opts+=('--single-transaction')
(( "${MYSQL_SLT:-1}" )) && opts+=('--skip-lock-tables')
"${cmd}" "${opts[@]}"
}
function _pgsql() {
local opts; opts=(
"--host=${DB_HOST:-127.0.0.1}"
"--port=${DB_PORT:-5432}"
"--username=${DB_USER:-postgres}"
'--no-password'
"--dbname=${1}"
)
(( "${PGSQL_CLN:-1}" )) && opts+=('--clean')
(( "${PGSQL_IE:-1}" )) && opts+=('--if-exists')
(( "${PGSQL_NO:-1}" )) && opts+=('--no-owner')
(( "${PGSQL_NP:-1}" )) && opts+=('--no-privileges')
(( "${PGSQL_QAI:-1}" )) && opts+=('--quote-all-identifiers')
case "${PGSQL_FMT:-plain}" in
'plain') opts+=('--format=plain') ;;
'custom') opts+=('--format=custom' "--compress=${PGSQL_Z:-zstd:5}") ;;
*) _error "'PGSQL_FMT' does not exist!" ;;
esac
PGPASSWORD="${DB_PASS}" pg_dump "${opts[@]}"
}
function _dump() {
local dbms; dbms="${1%%.*}"
local db; db="${1##*.}"
case "${dbms}" in
'mongo') _mongo "${db}" ;;
'mysql') _mysql "${db}" ;;
'pgsql') _pgsql "${db}" ;;
*) _error "'DBMS' does not exist!" ;;
esac
}
function _gpg() {
gpg --batch --yes --passphrase "${1}" --symmetric --output "${1}.gpg" \
--s2k-cipher-algo "${ENC_GPG_CIPHER:-AES256}" \
--s2k-digest-algo "${ENC_GPG_DIGEST:-SHA512}" \
--s2k-count "${ENC_GPG_COUNT:-65536}"
}
function _ssl() {
openssl enc "-${ENC_SSL_CIPHER:-aes-256-cfb}" -out "${1}.ssl" -pass "pass:${2}" \
-salt -md "${ENC_SSL_DIGEST:-sha512}" -iter "${ENC_SSL_COUNT:-65536}" -pbkdf2
}
function _enc() {
if (( "${ENC_ON}" )); then
case "${ENC_APP}" in
'gpg') _gpg "${1}" "${ENC_PASS}" ;;
'ssl') _ssl "${1}" "${ENC_PASS}" ;;
*) _error "'ENC_APP' does not exist!" ;;
esac
else
cat < '/dev/stdin' > "${1}"
fi
}
function _sum() {
local f; f="${1}"; (( "${ENC_ON}" )) && f="${1}.${ENC_APP}"
sha256sum "${f}" | sed 's| .*/| |g' | tee "${f}.txt" > '/dev/null'
}
function _ssh() {
echo "${SSH_PASS}" | sshfs "${SSH_USER:-root}@${SSH_HOST}:${1}" "${2}" -p "${SSH_PORT:-22}" -o 'password_stdin'
}
function _rsync() {
local opts; opts=('--archive' '--quiet')
(( "${RSYNC_DEL:-0}" )) && opts+=('--delete')
(( "${RSYNC_RSF:-0}" )) && opts+=('--remove-source-files')
(( "${RSYNC_PED:-0}" )) && opts+=('--prune-empty-dirs')
(( "${RSYNC_CVS:-0}" )) && opts+=('--cvs-exclude')
rsync "${opts[@]}" -e "sshpass -p '${RSYNC_PASS}' ssh -p ${RSYNC_PORT:-22}" \
"${1}/" "${RSYNC_USER:-root}@${RSYNC_HOST}:${2}/"
}
function fs_mount() {
(( ! "${SSH_ON}" )) && return 0
local msg_e; msg_e=(
'error'
'Error mounting SSH FS!'
"Error mounting SSH FS to '${SSH_MNT}'!"
)
_ssh "${SSH_DST}" "${SSH_MNT}" || _msg "${msg_e[@]}"
}
function fs_umount() {
(( ! "${SSH_ON}" )) && return 0
{ { findmnt -M "${SSH_MNT}" > '/dev/null'; } && umount "${SSH_MNT}"; } || return 0
}
function fs_check() {
local file; file="${FS_DST}/${FS_CHECK}"; [[ -f "${file}" ]] && return 0
local msg_e; msg_e=(
'error'
"File '${file}' not found!"
"File '${file}' not found! Please check the remote storage status!"
); _msg "${msg_e[@]}"
}
function db_backup() {
local ts; ts="$( date -u '+%m.%d-%H' )"
for i in "${DB_SRC[@]}"; do
local dst; dst="${FS_DST}/${FS_TPL}"
local file; file="${i}.${ts}.xz"
local msg_e; msg_e=(
'error'
"Error backing up database '${i}'"
"Error backing up database '${i}'! File '${dst}/${file}' not received or corrupted!"
)
local msg_s; msg_s=(
'success'
"Backup of database '${i}' completed successfully"
"Backup of database '${i}' completed successfully. File '${dst}/${file}' received."
)
[[ ! -d "${dst}" ]] && mkdir -p "${dst}"; cd "${dst}" || _error "Directory '${dst}' not found!"
{ { { _dump "${i}" | xz | _enc "${file}"; } && _sum "${file}"; } && _msg "${msg_s[@]}"; } \
|| _msg "${msg_e[@]}"
done
}
function fs_sync() {
(( ! "${RSYNC_ON}" )) && return 0
local msg_e; msg_e=(
'error'
'Error synchronizing with remote storage'
'Error synchronizing with remote storage!'
)
local msg_s; msg_s=(
'success'
'Synchronization with remote storage completed successfully'
'Synchronization with remote storage completed successfully.'
)
{ _rsync "${FS_DST}" "${RSYNC_DST}" && _msg "${msg_s[@]}"; } || _msg "${msg_e[@]}"
}
function fs_clean() {
find "${FS_DST}" -not -path '*/[@.]*' -type 'f' -mtime "+${FS_DAYS:-30}" -print0 | xargs -0 rm -f --
find "${FS_DST}" -mindepth 1 -type 'd' -not -name 'lost+found' -empty -delete
}
function main() {
{ fs_mount 2>&1 | tee "${LOG_MOUNT}"; } \
&& { fs_check 2>&1 | tee "${LOG_CHECK}"; } \
&& { db_backup 2>&1 | tee "${LOG_BACKUP}"; } \
&& { fs_sync 2>&1 | tee "${LOG_SYNC}"; } \
&& { fs_clean 2>&1 | tee "${LOG_CLEAN}"; }
{ fs_umount 2>&1 | tee "${LOG_UMOUNT}"; }
}; main "$@"