blob: d4c3d0cf25b15caf83cff33c008a345502a8dfed [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
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +010013function preflight_local_checks() {
14 if [[ -z "${REMOTE_SSH_KEY}" ]]; then
15 >&2 echo -e "\e[31mREMOTE_SSH_KEY is not set\e[0m"
16 exit 1
17 fi
18
19 if [[ ! -s "${REMOTE_SSH_KEY}" ]]; then
20 >&2 echo -e "\e[31mKeyfile ${REMOTE_SSH_KEY} is empty\e[0m"
21 exit 1
22 fi
23
24 if [[ $(stat -c "%a" "${REMOTE_SSH_KEY}") -ne 600 ]]; then
25 >&2 echo -e "\e[31mKeyfile ${REMOTE_SSH_KEY} has insecure permissions\e[0m"
26 exit 1
27 fi
28
29 if [[ ! -z "${REMOTE_SSH_PORT}" ]]; then
30 if [[ ${REMOTE_SSH_PORT} != ?(-)+([0-9]) ]] || [[ ${REMOTE_SSH_PORT} -gt 65535 ]]; then
31 >&2 echo -e "\e[31mREMOTE_SSH_PORT is set but not an integer < 65535\e[0m"
32 exit 1
33 fi
34 fi
35
36 if [[ -z "${REMOTE_SSH_HOST}" ]]; then
37 >&2 echo -e "\e[31mREMOTE_SSH_HOST cannot be empty\e[0m"
38 exit 1
39 fi
40
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +010041 for bin in rsync docker grep cut; do
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +010042 if [[ -z $(which ${bin}) ]]; then
43 >&2 echo -e "\e[31mCannot find ${bin} in local PATH, exiting...\e[0m"
44 exit 1
45 fi
46 done
47
48 if grep --help 2>&1 | head -n 1 | grep -q -i "busybox"; then
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +010049 echo -e "\e[31mBusyBox grep detected on local system, please install GNU grep\e[0m"
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +010050 exit 1
51 fi
52}
53
54function preflight_remote_checks() {
55
56 if ! ssh -o StrictHostKeyChecking=no \
57 -i "${REMOTE_SSH_KEY}" \
58 ${REMOTE_SSH_HOST} \
59 -p ${REMOTE_SSH_PORT} \
60 rsync --version > /dev/null ; then
61 >&2 echo -e "\e[31mCould not verify connection to ${REMOTE_SSH_HOST}\e[0m"
62 >&2 echo -e "\e[31mPlease check the output above (is rsync >= 3.1.0 installed on the remote system?)\e[0m"
63 exit 1
64 fi
65
66 if ssh -o StrictHostKeyChecking=no \
67 -i "${REMOTE_SSH_KEY}" \
68 ${REMOTE_SSH_HOST} \
69 -p ${REMOTE_SSH_PORT} \
70 grep --help 2>&1 | head -n 1 | grep -q -i "busybox" ; then
71 >&2 echo -e "\e[31mBusyBox grep detected on remote system ${REMOTE_SSH_HOST}, please install GNU grep\e[0m"
72 exit 1
73 fi
74
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +010075 for bin in rsync docker; do
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +010076 if ! ssh -o StrictHostKeyChecking=no \
77 -i "${REMOTE_SSH_KEY}" \
78 ${REMOTE_SSH_HOST} \
79 -p ${REMOTE_SSH_PORT} \
80 which ${bin} > /dev/null ; then
81 >&2 echo -e "\e[31mCannot find ${bin} in remote PATH, exiting...\e[0m"
82 exit 1
83 fi
84 done
85
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +010086 ssh -o StrictHostKeyChecking=no \
87 -i "${REMOTE_SSH_KEY}" \
88 ${REMOTE_SSH_HOST} \
89 -p ${REMOTE_SSH_PORT} \
90 "bash -s" << "EOF"
91if docker compose > /dev/null 2>&1; then
92 exit 0
93elif docker-compose version --short | grep "^2." > /dev/null 2>&1; then
94 exit 1
95else
96exit 2
97fi
98EOF
99
100if [ $? = 0 ]; then
101 COMPOSE_COMMAND="docker compose"
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200102 echo "INFO: Using native docker compose on remote"
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100103
104elif [ $? = 1 ]; then
105 COMPOSE_COMMAND="docker-compose"
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200106 echo "INFO: Using standalone docker compose on remote"
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100107
108else
109 echo -e "\e[31mCannot find any Docker Compose on remote, exiting...\e[0m"
110 exit 1
111fi
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100112
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200113 REMOTE_ARCH=$(ssh -o StrictHostKeyChecking=no -i "${REMOTE_SSH_KEY}" ${REMOTE_SSH_HOST} -p ${REMOTE_SSH_PORT} "uname -m")
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100114
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100115}
116
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100117SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
118source "${SCRIPT_DIR}/../mailcow.conf"
119COMPOSE_FILE="${SCRIPT_DIR}/../docker-compose.yml"
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200120CMPS_PRJ=$(echo ${COMPOSE_PROJECT_NAME} | tr -cd '0-9A-Za-z-_')
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100121SQLIMAGE=$(grep -iEo '(mysql|mariadb)\:.+' "${COMPOSE_FILE}")
122
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100123preflight_local_checks
124preflight_remote_checks
125
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100126echo
127echo -e "\033[1mFound compose project name ${CMPS_PRJ} for ${MAILCOW_HOSTNAME}\033[0m"
128echo -e "\033[1mFound SQL ${SQLIMAGE}\033[0m"
129echo
130
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100131# Print Message if Local Arch and Remote Arch is not the same
132if [[ $LOCAL_ARCH != $REMOTE_ARCH ]]; then
133 echo
134 echo -e "\e[1;33m!!!!!!!!!!!!!!!!!!!!!!!!!! CAUTION !!!!!!!!!!!!!!!!!!!!!!!!!!\e[0m"
135 echo -e "\e[3;33mDetected Architecture missmatch from source to destination...\e[0m"
136 echo -e "\e[3;33mYour backup is transferred but some volumes might be skipped!\e[0m"
137 echo -e "\e[1;33m!!!!!!!!!!!!!!!!!!!!!!!!!! CAUTION !!!!!!!!!!!!!!!!!!!!!!!!!!\e[0m"
138 echo
139 sleep 2
140fi
141
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100142# Make sure destination exists, rsync can fail under some circumstances
143echo -e "\033[1mPreparing remote...\033[0m"
144if ! ssh -o StrictHostKeyChecking=no \
145 -i "${REMOTE_SSH_KEY}" \
146 ${REMOTE_SSH_HOST} \
147 -p ${REMOTE_SSH_PORT} \
148 mkdir -p "${SCRIPT_DIR}/../" ; then
149 >&2 echo -e "\e[31m[ERR]\e[0m - Could not prepare remote for mailcow base directory transfer"
150 exit 1
151fi
152
153# Syncing the mailcow base directory
154echo -e "\033[1mSynchronizing mailcow base directory...\033[0m"
155rsync --delete -aH -e "ssh -o StrictHostKeyChecking=no \
156 -i \"${REMOTE_SSH_KEY}\" \
157 -p ${REMOTE_SSH_PORT}" \
158 "${SCRIPT_DIR}/../" root@${REMOTE_SSH_HOST}:"${SCRIPT_DIR}/../"
159ec=$?
160if [ ${ec} -ne 0 ] && [ ${ec} -ne 24 ]; then
161 >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer mailcow base directory to remote"
162 exit 1
163fi
164
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200165# Let the remote side create all network, volumes and containers to prevent need for external:true #
166echo -e "\e[33mCreating networks, volumes and containers on remote...\e[0m"
167
168if ! ssh -o StrictHostKeyChecking=no \
169 -i "${REMOTE_SSH_KEY}" \
170 ${REMOTE_SSH_HOST} \
171 -p ${REMOTE_SSH_PORT} \
172 "cd \"${SCRIPT_DIR}/../\" && ${COMPOSE_COMMAND} create 2>&1" ; then
173 >&2 echo -e "\e[31m[ERR]\e[0m - Could not create networks, volumes and containers on remote"
174fi
175
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100176# Trigger a Redis save for a consistent Redis copy
177echo -ne "\033[1mRunning redis-cli save... \033[0m"
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200178docker exec $(docker ps -qf name=redis-mailcow) redis-cli -a ${REDISPASS} --no-auth-warning save
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100179
180# Syncing volumes related to compose project
181# Same here: make sure destination exists
182for vol in $(docker volume ls -qf name="${CMPS_PRJ}"); do
183
184 mountpoint="$(docker inspect ${vol} | grep Mountpoint | cut -d '"' -f4)"
185
186 echo -e "\033[1mCreating remote mountpoint ${mountpoint} for ${vol}...\033[0m"
187
188 ssh -o StrictHostKeyChecking=no \
189 -i "${REMOTE_SSH_KEY}" \
190 ${REMOTE_SSH_HOST} \
191 -p ${REMOTE_SSH_PORT} \
192 mkdir -p "${mountpoint}"
193
194 if [[ "${vol}" =~ "mysql-vol-1" ]]; then
195
196 # Make sure a previous backup does not exist
197 rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
198
199 echo -e "\033[1mCreating consistent backup of MariaDB volume...\033[0m"
200 if ! docker run --rm \
201 --network $(docker network ls -qf name=${CMPS_PRJ}_) \
202 -v $(docker volume ls -qf name=${CMPS_PRJ}_mysql-vol-1):/var/lib/mysql/:ro \
203 --entrypoint= \
204 -v "${SCRIPT_DIR}/../_tmp_mariabackup":/backup \
205 ${SQLIMAGE} mariabackup --host mysql --user root --password ${DBROOT} --backup --target-dir=/backup 2>/dev/null ; then
206 >&2 echo -e "\e[31m[ERR]\e[0m - Could not create MariaDB backup on source"
207 rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
208 exit 1
209 fi
210
211 if ! docker run --rm \
212 --network $(docker network ls -qf name=${CMPS_PRJ}_) \
213 --entrypoint= \
214 -v "${SCRIPT_DIR}/../_tmp_mariabackup":/backup \
215 ${SQLIMAGE} mariabackup --prepare --target-dir=/backup 2> /dev/null ; then
216 >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer MariaDB backup to remote"
217 rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
218 exit 1
219 fi
220
221 chown -R 999:999 "${SCRIPT_DIR}/../_tmp_mariabackup"
222
223 echo -e "\033[1mSynchronizing MariaDB backup...\033[0m"
224 rsync --delete --info=progress2 -aH -e "ssh -o StrictHostKeyChecking=no \
225 -i \"${REMOTE_SSH_KEY}\" \
226 -p ${REMOTE_SSH_PORT}" \
227 "${SCRIPT_DIR}/../_tmp_mariabackup/" root@${REMOTE_SSH_HOST}:"${mountpoint}"
228 ec=$?
229 if [ ${ec} -ne 0 ] && [ ${ec} -ne 24 ]; then
230 >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer MariaDB backup to remote"
231 exit 1
232 fi
233
234 # Cleanup
235 rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
236
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100237 elif [[ "${vol}" =~ "rspamd-vol-1" ]]; then
238 # Exclude rspamd-vol-1 if the Architectures are not the same on source and destination due to compatibility issues.
239 if [[ $LOCAL_ARCH == $REMOTE_ARCH ]]; then
240 echo -e "\033[1mSynchronizing ${vol} from local ${mountpoint}...\033[0m"
241 rsync --delete --info=progress2 -aH -e "ssh -o StrictHostKeyChecking=no \
242 -i \"${REMOTE_SSH_KEY}\" \
243 -p ${REMOTE_SSH_PORT}" \
244 "${mountpoint}/" root@${REMOTE_SSH_HOST}:"${mountpoint}"
245 else
246 echo -e "\e[1;31mSkipping ${vol} from local maschine due to incompatiblity between different architecture...\e[0m"
247 sleep 2
248 continue
249 fi
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100250
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +0100251 else
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100252 echo -e "\033[1mSynchronizing ${vol} from local ${mountpoint}...\033[0m"
253 rsync --delete --info=progress2 -aH -e "ssh -o StrictHostKeyChecking=no \
254 -i \"${REMOTE_SSH_KEY}\" \
255 -p ${REMOTE_SSH_PORT}" \
256 "${mountpoint}/" 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 ${vol} from local ${mountpoint} to remote"
260 exit 1
261 fi
262 fi
263
264 echo -e "\e[32mCompleted\e[0m"
265
266done
267
268# Restart Dockerd on destination
269echo -ne "\033[1mRestarting Docker daemon on remote to detect new volumes... \033[0m"
270if ! ssh -o StrictHostKeyChecking=no \
271 -i "${REMOTE_SSH_KEY}" \
272 ${REMOTE_SSH_HOST} \
273 -p ${REMOTE_SSH_PORT} \
274 systemctl restart docker ; then
275 >&2 echo -e "\e[31m[ERR]\e[0m - Could not restart Docker daemon on remote"
276 exit 1
277fi
278echo "OK"
279
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100280 echo -e "\e[33mPulling images on remote...\e[0m"
281 echo -e "\e[33mProcess is NOT stuck! Please wait...\e[0m"
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100282
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100283 if ! ssh -o StrictHostKeyChecking=no \
284 -i "${REMOTE_SSH_KEY}" \
285 ${REMOTE_SSH_HOST} \
286 -p ${REMOTE_SSH_PORT} \
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200287 "cd \"${SCRIPT_DIR}/../\" && ${COMPOSE_COMMAND} pull --quiet 2>&1" ; then
Matthias Andreas Benkard3c18a082022-12-27 17:32:58 +0100288 >&2 echo -e "\e[31m[ERR]\e[0m - Could not pull images on remote"
289 fi
290
291echo -e "\033[1mExecuting update script and forcing garbage cleanup on remote...\033[0m"
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100292if ! ssh -o StrictHostKeyChecking=no \
293 -i "${REMOTE_SSH_KEY}" \
294 ${REMOTE_SSH_HOST} \
295 -p ${REMOTE_SSH_PORT} \
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200296 "cd \"${SCRIPT_DIR}/../\" && ./update.sh -f --gc" ; then
Matthias Andreas Benkardb4b5cc12021-12-28 18:02:04 +0100297 >&2 echo -e "\e[31m[ERR]\e[0m - Could not cleanup old images on remote"
298fi
299
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +0200300echo -e "\e[32mDone\e[0m"