blob: ff0512e0732acff53b75019fb17e6069800fbdd7 [file] [log] [blame]
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +01001#!/usr/bin/env bash
2
3PATH=${PATH}:/opt/bin
4DATE=$(date +%Y-%m-%d_%H_%M_%S)
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +01005LOCAL_ARCH=$(uname -m)
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +01006export LC_ALL=C
7
8echo
9echo "If this script is run automatically by cron or a timer AND you are using block-level snapshots on your backup destination, make sure both do not run at the same time."
10echo "The snapshots of your backup destination should run AFTER the cold standby script finished to ensure consistent snapshots."
11echo
12
13function docker_garbage() {
14 IMGS_TO_DELETE=()
15
16 for container in $(grep -oP "image: \Kmailcow.+" docker-compose.yml); do
17
18 REPOSITORY=${container/:*}
19 TAG=${container/*:}
20 V_MAIN=${container/*.}
21 V_SUB=${container/*.}
22 EXISTING_TAGS=$(docker images | grep ${REPOSITORY} | awk '{ print $2 }')
23
24 for existing_tag in ${EXISTING_TAGS[@]}; do
25
26 V_MAIN_EXISTING=${existing_tag/*.}
27 V_SUB_EXISTING=${existing_tag/*.}
28
29 # Not an integer
30 [[ ! ${V_MAIN_EXISTING} =~ ^[0-9]+$ ]] && continue
31 [[ ! ${V_SUB_EXISTING} =~ ^[0-9]+$ ]] && continue
32
33 if [[ ${V_MAIN_EXISTING} == "latest" ]]; then
34 echo "Found deprecated label \"latest\" for repository ${REPOSITORY}, it should be deleted."
35 IMGS_TO_DELETE+=(${REPOSITORY}:${existing_tag})
36 elif [[ ${V_MAIN_EXISTING} -lt ${V_MAIN} ]]; then
37 echo "Found tag ${existing_tag} for ${REPOSITORY}, which is older than the current tag ${TAG} and should be deleted."
38 IMGS_TO_DELETE+=(${REPOSITORY}:${existing_tag})
39 elif [[ ${V_SUB_EXISTING} -lt ${V_SUB} ]]; then
40 echo "Found tag ${existing_tag} for ${REPOSITORY}, which is older than the current tag ${TAG} and should be deleted."
41 IMGS_TO_DELETE+=(${REPOSITORY}:${existing_tag})
42 fi
43
44 done
45
46 done
47
48 if [[ ! -z ${IMGS_TO_DELETE[*]} ]]; then
49 docker rmi ${IMGS_TO_DELETE[*]}
50 fi
51}
52
53function preflight_local_checks() {
54 if [[ -z "${REMOTE_SSH_KEY}" ]]; then
55 >&2 echo -e "\e[31mREMOTE_SSH_KEY is not set\e[0m"
56 exit 1
57 fi
58
59 if [[ ! -s "${REMOTE_SSH_KEY}" ]]; then
60 >&2 echo -e "\e[31mKeyfile ${REMOTE_SSH_KEY} is empty\e[0m"
61 exit 1
62 fi
63
64 if [[ $(stat -c "%a" "${REMOTE_SSH_KEY}") -ne 600 ]]; then
65 >&2 echo -e "\e[31mKeyfile ${REMOTE_SSH_KEY} has insecure permissions\e[0m"
66 exit 1
67 fi
68
69 if [[ ! -z "${REMOTE_SSH_PORT}" ]]; then
70 if [[ ${REMOTE_SSH_PORT} != ?(-)+([0-9]) ]] || [[ ${REMOTE_SSH_PORT} -gt 65535 ]]; then
71 >&2 echo -e "\e[31mREMOTE_SSH_PORT is set but not an integer < 65535\e[0m"
72 exit 1
73 fi
74 fi
75
76 if [[ -z "${REMOTE_SSH_HOST}" ]]; then
77 >&2 echo -e "\e[31mREMOTE_SSH_HOST cannot be empty\e[0m"
78 exit 1
79 fi
80
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +010081 for bin in rsync docker grep cut; do
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +010082 if [[ -z $(which ${bin}) ]]; then
83 >&2 echo -e "\e[31mCannot find ${bin} in local PATH, exiting...\e[0m"
84 exit 1
85 fi
86 done
87
88 if grep --help 2>&1 | head -n 1 | grep -q -i "busybox"; then
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +010089 echo -e "\e[31mBusyBox grep detected on local system, please install GNU grep\e[0m"
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +010090 exit 1
91 fi
92}
93
94function preflight_remote_checks() {
95
96 if ! ssh -o StrictHostKeyChecking=no \
97 -i "${REMOTE_SSH_KEY}" \
98 ${REMOTE_SSH_HOST} \
99 -p ${REMOTE_SSH_PORT} \
100 rsync --version > /dev/null ; then
101 >&2 echo -e "\e[31mCould not verify connection to ${REMOTE_SSH_HOST}\e[0m"
102 >&2 echo -e "\e[31mPlease check the output above (is rsync >= 3.1.0 installed on the remote system?)\e[0m"
103 exit 1
104 fi
105
106 if ssh -o StrictHostKeyChecking=no \
107 -i "${REMOTE_SSH_KEY}" \
108 ${REMOTE_SSH_HOST} \
109 -p ${REMOTE_SSH_PORT} \
110 grep --help 2>&1 | head -n 1 | grep -q -i "busybox" ; then
111 >&2 echo -e "\e[31mBusyBox grep detected on remote system ${REMOTE_SSH_HOST}, please install GNU grep\e[0m"
112 exit 1
113 fi
114
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100115 for bin in rsync docker; do
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100116 if ! ssh -o StrictHostKeyChecking=no \
117 -i "${REMOTE_SSH_KEY}" \
118 ${REMOTE_SSH_HOST} \
119 -p ${REMOTE_SSH_PORT} \
120 which ${bin} > /dev/null ; then
121 >&2 echo -e "\e[31mCannot find ${bin} in remote PATH, exiting...\e[0m"
122 exit 1
123 fi
124 done
125
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100126 ssh -o StrictHostKeyChecking=no \
127 -i "${REMOTE_SSH_KEY}" \
128 ${REMOTE_SSH_HOST} \
129 -p ${REMOTE_SSH_PORT} \
130 "bash -s" << "EOF"
131if docker compose > /dev/null 2>&1; then
132 exit 0
133elif docker-compose version --short | grep "^2." > /dev/null 2>&1; then
134 exit 1
135else
136exit 2
137fi
138EOF
139
140if [ $? = 0 ]; then
141 COMPOSE_COMMAND="docker compose"
142 echo "DEBUG: Using native docker compose on remote"
143
144elif [ $? = 1 ]; then
145 COMPOSE_COMMAND="docker-compose"
146 echo "DEBUG: Using standalone docker compose on remote"
147
148else
149 echo -e "\e[31mCannot find any Docker Compose on remote, exiting...\e[0m"
150 exit 1
151fi
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100152
153 REMOTE_ARCH=$(ssh -o StrictHostKeyChecking=no -i "${REMOTE_SSH_KEY}" ${REMOTE_SSH_HOST} -p ${REMOTE_SSH_PORT} "uname -m")
154
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100155}
156
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100157SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
158source "${SCRIPT_DIR}/../mailcow.conf"
159COMPOSE_FILE="${SCRIPT_DIR}/../docker-compose.yml"
160CMPS_PRJ=$(echo ${COMPOSE_PROJECT_NAME} | tr -cd 'A-Za-z-_')
161SQLIMAGE=$(grep -iEo '(mysql|mariadb)\:.+' "${COMPOSE_FILE}")
162
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100163preflight_local_checks
164preflight_remote_checks
165
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100166echo
167echo -e "\033[1mFound compose project name ${CMPS_PRJ} for ${MAILCOW_HOSTNAME}\033[0m"
168echo -e "\033[1mFound SQL ${SQLIMAGE}\033[0m"
169echo
170
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100171# Print Message if Local Arch and Remote Arch is not the same
172if [[ $LOCAL_ARCH != $REMOTE_ARCH ]]; then
173 echo
174 echo -e "\e[1;33m!!!!!!!!!!!!!!!!!!!!!!!!!! CAUTION !!!!!!!!!!!!!!!!!!!!!!!!!!\e[0m"
175 echo -e "\e[3;33mDetected Architecture missmatch from source to destination...\e[0m"
176 echo -e "\e[3;33mYour backup is transferred but some volumes might be skipped!\e[0m"
177 echo -e "\e[1;33m!!!!!!!!!!!!!!!!!!!!!!!!!! CAUTION !!!!!!!!!!!!!!!!!!!!!!!!!!\e[0m"
178 echo
179 sleep 2
180fi
181
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100182# Make sure destination exists, rsync can fail under some circumstances
183echo -e "\033[1mPreparing remote...\033[0m"
184if ! ssh -o StrictHostKeyChecking=no \
185 -i "${REMOTE_SSH_KEY}" \
186 ${REMOTE_SSH_HOST} \
187 -p ${REMOTE_SSH_PORT} \
188 mkdir -p "${SCRIPT_DIR}/../" ; then
189 >&2 echo -e "\e[31m[ERR]\e[0m - Could not prepare remote for mailcow base directory transfer"
190 exit 1
191fi
192
193# Syncing the mailcow base directory
194echo -e "\033[1mSynchronizing mailcow base directory...\033[0m"
195rsync --delete -aH -e "ssh -o StrictHostKeyChecking=no \
196 -i \"${REMOTE_SSH_KEY}\" \
197 -p ${REMOTE_SSH_PORT}" \
198 "${SCRIPT_DIR}/../" root@${REMOTE_SSH_HOST}:"${SCRIPT_DIR}/../"
199ec=$?
200if [ ${ec} -ne 0 ] && [ ${ec} -ne 24 ]; then
201 >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer mailcow base directory to remote"
202 exit 1
203fi
204
205# Trigger a Redis save for a consistent Redis copy
206echo -ne "\033[1mRunning redis-cli save... \033[0m"
207docker exec $(docker ps -qf name=redis-mailcow) redis-cli save
208
209# Syncing volumes related to compose project
210# Same here: make sure destination exists
211for vol in $(docker volume ls -qf name="${CMPS_PRJ}"); do
212
213 mountpoint="$(docker inspect ${vol} | grep Mountpoint | cut -d '"' -f4)"
214
215 echo -e "\033[1mCreating remote mountpoint ${mountpoint} for ${vol}...\033[0m"
216
217 ssh -o StrictHostKeyChecking=no \
218 -i "${REMOTE_SSH_KEY}" \
219 ${REMOTE_SSH_HOST} \
220 -p ${REMOTE_SSH_PORT} \
221 mkdir -p "${mountpoint}"
222
223 if [[ "${vol}" =~ "mysql-vol-1" ]]; then
224
225 # Make sure a previous backup does not exist
226 rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
227
228 echo -e "\033[1mCreating consistent backup of MariaDB volume...\033[0m"
229 if ! docker run --rm \
230 --network $(docker network ls -qf name=${CMPS_PRJ}_) \
231 -v $(docker volume ls -qf name=${CMPS_PRJ}_mysql-vol-1):/var/lib/mysql/:ro \
232 --entrypoint= \
233 -v "${SCRIPT_DIR}/../_tmp_mariabackup":/backup \
234 ${SQLIMAGE} mariabackup --host mysql --user root --password ${DBROOT} --backup --target-dir=/backup 2>/dev/null ; then
235 >&2 echo -e "\e[31m[ERR]\e[0m - Could not create MariaDB backup on source"
236 rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
237 exit 1
238 fi
239
240 if ! docker run --rm \
241 --network $(docker network ls -qf name=${CMPS_PRJ}_) \
242 --entrypoint= \
243 -v "${SCRIPT_DIR}/../_tmp_mariabackup":/backup \
244 ${SQLIMAGE} mariabackup --prepare --target-dir=/backup 2> /dev/null ; then
245 >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer MariaDB backup to remote"
246 rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
247 exit 1
248 fi
249
250 chown -R 999:999 "${SCRIPT_DIR}/../_tmp_mariabackup"
251
252 echo -e "\033[1mSynchronizing MariaDB backup...\033[0m"
253 rsync --delete --info=progress2 -aH -e "ssh -o StrictHostKeyChecking=no \
254 -i \"${REMOTE_SSH_KEY}\" \
255 -p ${REMOTE_SSH_PORT}" \
256 "${SCRIPT_DIR}/../_tmp_mariabackup/" root@${REMOTE_SSH_HOST}:"${mountpoint}"
257 ec=$?
258 if [ ${ec} -ne 0 ] && [ ${ec} -ne 24 ]; then
259 >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer MariaDB backup to remote"
260 exit 1
261 fi
262
263 # Cleanup
264 rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
265
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100266 elif [[ "${vol}" =~ "rspamd-vol-1" ]]; then
267 # Exclude rspamd-vol-1 if the Architectures are not the same on source and destination due to compatibility issues.
268 if [[ $LOCAL_ARCH == $REMOTE_ARCH ]]; then
269 echo -e "\033[1mSynchronizing ${vol} from local ${mountpoint}...\033[0m"
270 rsync --delete --info=progress2 -aH -e "ssh -o StrictHostKeyChecking=no \
271 -i \"${REMOTE_SSH_KEY}\" \
272 -p ${REMOTE_SSH_PORT}" \
273 "${mountpoint}/" root@${REMOTE_SSH_HOST}:"${mountpoint}"
274 else
275 echo -e "\e[1;31mSkipping ${vol} from local maschine due to incompatiblity between different architecture...\e[0m"
276 sleep 2
277 continue
278 fi
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100279
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100280 else
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100281 echo -e "\033[1mSynchronizing ${vol} from local ${mountpoint}...\033[0m"
282 rsync --delete --info=progress2 -aH -e "ssh -o StrictHostKeyChecking=no \
283 -i \"${REMOTE_SSH_KEY}\" \
284 -p ${REMOTE_SSH_PORT}" \
285 "${mountpoint}/" root@${REMOTE_SSH_HOST}:"${mountpoint}"
286 ec=$?
287 if [ ${ec} -ne 0 ] && [ ${ec} -ne 24 ]; then
288 >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer ${vol} from local ${mountpoint} to remote"
289 exit 1
290 fi
291 fi
292
293 echo -e "\e[32mCompleted\e[0m"
294
295done
296
297# Restart Dockerd on destination
298echo -ne "\033[1mRestarting Docker daemon on remote to detect new volumes... \033[0m"
299if ! ssh -o StrictHostKeyChecking=no \
300 -i "${REMOTE_SSH_KEY}" \
301 ${REMOTE_SSH_HOST} \
302 -p ${REMOTE_SSH_PORT} \
303 systemctl restart docker ; then
304 >&2 echo -e "\e[31m[ERR]\e[0m - Could not restart Docker daemon on remote"
305 exit 1
306fi
307echo "OK"
308
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100309 echo -e "\e[33mPulling images on remote...\e[0m"
310 echo -e "\e[33mProcess is NOT stuck! Please wait...\e[0m"
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100311
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100312 if ! ssh -o StrictHostKeyChecking=no \
313 -i "${REMOTE_SSH_KEY}" \
314 ${REMOTE_SSH_HOST} \
315 -p ${REMOTE_SSH_PORT} \
316 ${COMPOSE_COMMAND} -f "${SCRIPT_DIR}/../docker-compose.yml" pull --no-parallel --quiet 2>&1 ; then
317 >&2 echo -e "\e[31m[ERR]\e[0m - Could not pull images on remote"
318 fi
319
320echo -e "\033[1mExecuting update script and forcing garbage cleanup on remote...\033[0m"
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100321if ! ssh -o StrictHostKeyChecking=no \
322 -i "${REMOTE_SSH_KEY}" \
323 ${REMOTE_SSH_HOST} \
324 -p ${REMOTE_SSH_PORT} \
325 ${SCRIPT_DIR}/../update.sh -f --gc ; then
326 >&2 echo -e "\e[31m[ERR]\e[0m - Could not cleanup old images on remote"
327fi
328
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100329echo -e "\e[32mDone\e[0m"