forked from MatthisB/mailcow-borg-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
268 lines (218 loc) ยท 7.21 KB
/
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
#!/bin/bash
###############################
# #
# Mailcow Borg Backup #
# #
###############################
# Author: Matthis B. #
# Created: 20201105 #
# Lastchange: 20201119 #
###############################
# Changelog: #
# - 20201105: init #
# - 20201107: small changes #
# - 20201119: bug fixes #
###############################
##
## Cronjob:
## 7 4 * * * /home/path/to/backup.sh > /home/path/to/backup/log/mailcow_sys_$(date +\%Y-\%m-\%d-\%H-\%M-\%S).log 2>&1
##
#
# Functions
#
getDate() { echo $(date +'%d.%m.%Y %H:%M:%S'); }
info() { printf "\n- %s %s\n\n" "$( getDate )" "$*" >&2; }
trap "echo $( getDate ) Backup interrupted >&2; exit 2" INT TERM
#
# Settings
#
# System vars
workDirectory='/opt/mailcow-dockerized' # path to docker-compose.yml
logDirectory='/home/path/to/backup/log' # path top log archive (keep in mind to change path in cron also)
logKeepAmount='10' # how many logfiles should be kept
# Borg env vars
BORG_PREFIX='mx10'
export BORG_REPO='ssh://[email protected]:22/repo/path'
export BORG_PASSPHRASE='passw0rd'
export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
# autocatch vars
source "${workDirectory}/mailcow.conf"
CMPS_PRJ=$(echo $COMPOSE_PROJECT_NAME | tr -cd "[A-Za-z-_]")
volumeVMail=$(docker volume inspect -f '{{ .Mountpoint }}' ${CMPS_PRJ}_vmail-vol-1)
volumeCrypt=$(docker volume inspect -f '{{ .Mountpoint }}' ${CMPS_PRJ}_crypt-vol-1)
volumeRedis=$(docker volume inspect -f '{{ .Mountpoint }}' ${CMPS_PRJ}_redis-vol-1)
volumeRSpamd=$(docker volume inspect -f '{{ .Mountpoint }}' ${CMPS_PRJ}_rspamd-vol-1)
volumePostfix=$(docker volume inspect -f '{{ .Mountpoint }}' ${CMPS_PRJ}_postfix-vol-1)
volumeMySQL=$(docker volume inspect -f '{{ .Mountpoint }}' ${CMPS_PRJ}_mysql-vol-1)
#
# Prerequisites
#
# check for root
if [[ "$(id -u)" != "0" ]] ; then
info "ERROR: no root!"
exit 1
fi
# check project name
if [[ -z "${CMPS_PRJ}" ]] ; then
info "ERROR: empty docker-project-name"
exit 1
fi
# create directories if not already done
if [[ ! -d "${logDirectory}" ]] ; then
if ! mkdir -p "${logDirectory}" ; then
info "ERROR: could not create logDirectory ($logDirectory)"
exit 1
fi
fi
#
# Pre-BackupProcess
#
startTime=$(date +%s)
echo
echo "=================================================="
info "start backup"
# clean old stuff
echo "-- clean up temp/log"
countLogs=$(ls -l ${logDirectory}/*.log 2>/dev/null | wc -l)
echo "--- found $countLogs old log files"
if (( "$countLogs" > "$logKeepAmount" )) ; then
echo "--- delete old logfiles up to the last $logKeepAmount .."
ls -dt ${logDirectory}/*.log | tail -n "+$((logKeepAmount+1))" | xargs rm -v
fi
echo
echo "-- pre borg stuff"
# ensure no more changes are made (e.g. send/receive mails, WebUI changes)
echo "--- mailcow: stop services"
echo "---- stop dovecot"
dovecot_id=$(docker stop "$(docker ps -qf name=dovecot-mailcow)")
if [[ "$(docker inspect -f '{{ .State.ExitCode }}' "$dovecot_id")" == "0" && "$(docker inspect -f '{{ .State.Running }}' "$dovecot_id")" == "false" ]] ; then
echo "----- success"
else
echo "----- FAILED"
fi
echo "---- stop postfix"
postfix_id=$(docker stop "$(docker ps -qf name=postfix-mailcow)")
if [[ "$(docker inspect -f '{{ .State.ExitCode }}' "$postfix_id")" == "0" && "$(docker inspect -f '{{ .State.Running }}' "$postfix_id")" == "false" ]] ; then
echo "----- success"
else
echo "----- FAILED"
fi
# show error on webstuff
echo "--- nginx return service unavailable"
echo "return 503;" > "${workDirectory}/data/conf/nginx/site.backup.custom"
nginx_id=$(docker restart "$(docker ps -qf name=nginx-mailcow)")
if [[ "$(docker inspect -f '{{ .State.Running }}' "$nginx_id")" == "true" ]] ; then
echo "---- success"
else
echo "---- FAILED"
fi
# backup database: mysql
echo "--- mysql-dump"
if [[ -d "${volumeMySQL}/tmp_backup" ]] ; then
echo "---- WARN: tmp backup dir already exists"
rm -r "${volumeMySQL}/tmp_backup"
if [[ -d "${volumeMySQL}/tmp_backup" ]] ; then
echo "---- FAILED: could not delete"
fi
fi
mysql_id=$(docker ps -qf 'name=mysql-mailcow')
docker exec ${mysql_id} /bin/sh -c "mariabackup --host mysql --user root --password ${DBROOT} \
--backup --rsync --target-dir=/var/lib/mysql/tmp_backup ; \
mariabackup --prepare --target-dir=/var/lib/mysql/tmp_backup ; \
chown -R 999:999 /var/lib/mysql/tmp_backup ;" > /dev/null 2>&1
if [[ -d "${volumeMySQL}/tmp_backup" ]] ; then
echo "---- success"
else
echo "---- FAILED: could not create backup"
fi
# backup database: redis
echo "--- redis-dump"
redis_id=$(docker ps -qf name=redis-mailcow)
redis_dump=$(docker exec $redis_id redis-cli save)
if [[ "$redis_dump" == "OK" ]]; then
echo "---- success"
else
echo "---- FAILED: $redisdump"
fi
#
# BackupProcess
#
# starting borg
echo
echo "-- start syncing files"
echo
thisDir="$( cd $( dirname ${BASH_SOURCE[0]} ) >/dev/null 2>&1 && pwd )"
thisFile="$(basename ${0})"
borg create \
--show-rc \
--verbose \
--stats \
--compression lz4 \
--exclude-caches \
::"${BORG_PREFIX}-{now:%Y-%m-%d_%H:%M:%S}" \
"${workDirectory}/.env" \
"${workDirectory}/docker-compose.yml" \
"${workDirectory}/mailcow.conf" \
"${volumeVMail}" \
"${volumeCrypt}" \
"${volumeRedis}" \
"${volumeRSpamd}" \
"${volumePostfix}" \
"${volumeMySQL}/tmp_backup" \
"${thisDir}/${thisFile}"
borg_create_exit=$?
# check state
echo
echo "-- borg finished"
if [[ "${borg_create_exit}" == "0" ]] ; then
echo "--- success"
elif [[ "${borg_create_exit}" == "1" ]] ; then
echo "--- WARN: 1"
else
echo "--- FAILED: ${borg_create_exit}"
fi
#
# Post-BackupProcess
#
echo
echo "-- post borg stuff"
echo "--- mailcow: re/start services"
echo "---- start dovecot"
dovecot_id=$(docker start "$(docker ps -aqf name=dovecot-mailcow)")
if [[ "$(docker inspect -f '{{ .State.Running }}' "$dovecot_id")" == "true" ]] ; then
echo "----- success"
else
echo "----- FAILED"
fi
echo "---- start postfix"
postfix_id=$(docker start "$(docker ps -aqf name=postfix-mailcow)")
if [[ "$(docker inspect -f '{{ .State.Running }}' "$postfix_id")" == "true" ]] ; then
echo "----- success"
else
echo "----- FAILED"
fi
# show error on webstuff
echo "--- nginx normal mode"
if [[ -f "${workDirectory}/data/conf/nginx/site.backup.custom" ]] ; then
rm "${workDirectory}/data/conf/nginx/site.backup.custom"
fi
nginx_id=$(docker restart "$(docker ps -qf name=nginx-mailcow)")
if [[ "$(docker inspect -f '{{ .State.Running }}' "$nginx_id")" == "true" ]] ; then
echo "---- success"
else
echo "---- FAILED"
fi
# clean tmp stuff
echo "--- clean up"
if [[ -d "${volumeMySQL}/tmp_backup" ]] ; then
rm -r "${volumeMySQL}/tmp_backup"
echo "---- removed MySQL tmp_backup dir"
fi
# calc backup duration
endTime=$(date +%s)
duration=$((endTime-startTime))
echo
echo "-- backup duration: $(printf '%02d hours %02d minutes %02d seconds' $((duration / 3600)) $(((duration / 60) % 60)) $((duration % 60)))"
info "-> everything done: exit"
exit ${borg_create_exit}