_cold-standby.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #!/usr/bin/env bash
  2. PATH=${PATH}:/opt/bin
  3. DATE=$(date +%Y-%m-%d_%H_%M_%S)
  4. export LC_ALL=C
  5. echo
  6. echo "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."
  7. echo "The snapshots of your backup destination should run AFTER the cold standby script finished to ensure consistent snapshots."
  8. echo
  9. function docker_garbage() {
  10. IMGS_TO_DELETE=()
  11. for container in $(grep -oP "image: \Kmailcow.+" docker-compose.yml); do
  12. REPOSITORY=${container/:*}
  13. TAG=${container/*:}
  14. V_MAIN=${container/*.}
  15. V_SUB=${container/*.}
  16. EXISTING_TAGS=$(docker images | grep ${REPOSITORY} | awk '{ print $2 }')
  17. for existing_tag in ${EXISTING_TAGS[@]}; do
  18. V_MAIN_EXISTING=${existing_tag/*.}
  19. V_SUB_EXISTING=${existing_tag/*.}
  20. # Not an integer
  21. [[ ! ${V_MAIN_EXISTING} =~ ^[0-9]+$ ]] && continue
  22. [[ ! ${V_SUB_EXISTING} =~ ^[0-9]+$ ]] && continue
  23. if [[ ${V_MAIN_EXISTING} == "latest" ]]; then
  24. echo "Found deprecated label \"latest\" for repository ${REPOSITORY}, it should be deleted."
  25. IMGS_TO_DELETE+=(${REPOSITORY}:${existing_tag})
  26. elif [[ ${V_MAIN_EXISTING} -lt ${V_MAIN} ]]; then
  27. echo "Found tag ${existing_tag} for ${REPOSITORY}, which is older than the current tag ${TAG} and should be deleted."
  28. IMGS_TO_DELETE+=(${REPOSITORY}:${existing_tag})
  29. elif [[ ${V_SUB_EXISTING} -lt ${V_SUB} ]]; then
  30. echo "Found tag ${existing_tag} for ${REPOSITORY}, which is older than the current tag ${TAG} and should be deleted."
  31. IMGS_TO_DELETE+=(${REPOSITORY}:${existing_tag})
  32. fi
  33. done
  34. done
  35. if [[ ! -z ${IMGS_TO_DELETE[*]} ]]; then
  36. docker rmi ${IMGS_TO_DELETE[*]}
  37. fi
  38. }
  39. function preflight_local_checks() {
  40. if [[ -z "${REMOTE_SSH_KEY}" ]]; then
  41. >&2 echo -e "\e[31mREMOTE_SSH_KEY is not set\e[0m"
  42. exit 1
  43. fi
  44. if [[ ! -s "${REMOTE_SSH_KEY}" ]]; then
  45. >&2 echo -e "\e[31mKeyfile ${REMOTE_SSH_KEY} is empty\e[0m"
  46. exit 1
  47. fi
  48. if [[ $(stat -c "%a" "${REMOTE_SSH_KEY}") -ne 600 ]]; then
  49. >&2 echo -e "\e[31mKeyfile ${REMOTE_SSH_KEY} has insecure permissions\e[0m"
  50. exit 1
  51. fi
  52. if [[ ! -z "${REMOTE_SSH_PORT}" ]]; then
  53. if [[ ${REMOTE_SSH_PORT} != ?(-)+([0-9]) ]] || [[ ${REMOTE_SSH_PORT} -gt 65535 ]]; then
  54. >&2 echo -e "\e[31mREMOTE_SSH_PORT is set but not an integer < 65535\e[0m"
  55. exit 1
  56. fi
  57. fi
  58. if [[ -z "${REMOTE_SSH_HOST}" ]]; then
  59. >&2 echo -e "\e[31mREMOTE_SSH_HOST cannot be empty\e[0m"
  60. exit 1
  61. fi
  62. for bin in rsync docker grep cut; do
  63. if [[ -z $(which ${bin}) ]]; then
  64. >&2 echo -e "\e[31mCannot find ${bin} in local PATH, exiting...\e[0m"
  65. exit 1
  66. fi
  67. done
  68. echo "checking docker compose version...";
  69. if docker --help | grep compose; then
  70. echo ''
  71. elif docker-compose version --short | grep -m1 "^1" > /dev/null 2>&1; then
  72. echo -e "\e[31mWARN: Your machine is using Docker-Compose v1!\e[0m"
  73. echo -e "\e[31mmailcow will drop the Docker-Compose v1 Support in December 2022\e[0m"
  74. echo -e "\e[31mPlease consider a upgrade to Docker-Compose v2.\e[0m"
  75. echo
  76. echo
  77. echo -e "\e[33mContinuing...\e[0m"
  78. sleep 3
  79. else
  80. echo -e "\e[31mCannot find Docker-Compose v1 or v2 on your System. Please install Docker-Compose v2 and re-run the Script.\e[0m"
  81. exit 1
  82. fi
  83. if grep --help 2>&1 | head -n 1 | grep -q -i "busybox"; then
  84. echo -e "\e[31mBusyBox grep detected on local system, please install GNU grep\e[0m"
  85. exit 1
  86. fi
  87. }
  88. function preflight_remote_checks() {
  89. if ! ssh -o StrictHostKeyChecking=no \
  90. -i "${REMOTE_SSH_KEY}" \
  91. ${REMOTE_SSH_HOST} \
  92. -p ${REMOTE_SSH_PORT} \
  93. rsync --version > /dev/null ; then
  94. >&2 echo -e "\e[31mCould not verify connection to ${REMOTE_SSH_HOST}\e[0m"
  95. >&2 echo -e "\e[31mPlease check the output above (is rsync >= 3.1.0 installed on the remote system?)\e[0m"
  96. exit 1
  97. fi
  98. if ssh -o StrictHostKeyChecking=no \
  99. -i "${REMOTE_SSH_KEY}" \
  100. ${REMOTE_SSH_HOST} \
  101. -p ${REMOTE_SSH_PORT} \
  102. grep --help 2>&1 | head -n 1 | grep -q -i "busybox" ; then
  103. >&2 echo -e "\e[31mBusyBox grep detected on remote system ${REMOTE_SSH_HOST}, please install GNU grep\e[0m"
  104. exit 1
  105. fi
  106. for bin in rsync docker; do
  107. if ! ssh -o StrictHostKeyChecking=no \
  108. -i "${REMOTE_SSH_KEY}" \
  109. ${REMOTE_SSH_HOST} \
  110. -p ${REMOTE_SSH_PORT} \
  111. which ${bin} > /dev/null ; then
  112. >&2 echo -e "\e[31mCannot find ${bin} in remote PATH, exiting...\e[0m"
  113. exit 1
  114. fi
  115. done
  116. echo "checking docker compose version on remote...";
  117. if ssh -q -o StrictHostKeyChecking=no \
  118. -i "${REMOTE_SSH_KEY}" \
  119. ${REMOTE_SSH_HOST} \
  120. -p ${REMOTE_SSH_PORT} \
  121. -t docker --help | grep compose; then
  122. COMPOSE_COMMAND="docker compose"
  123. elif ssh -q -o StrictHostKeyChecking=no \
  124. -i "${REMOTE_SSH_KEY}" \
  125. ${REMOTE_SSH_HOST} \
  126. -p ${REMOTE_SSH_PORT} \
  127. 'docker-compose version --short' | grep -m1 "^1" > /dev/null 2>&1; then
  128. echo -e "\e[31mWARN: The remote is using Docker-Compose v1!\e[0m"
  129. echo -e "\e[31mmailcow will drop the Docker-Compose v1 Support in December 2022\e[0m"
  130. echo -e "\e[31mPlease consider a upgrade to Docker-Compose v2 on remote.\e[0m"
  131. echo
  132. echo
  133. echo -e "\e[33mContinuing...\e[0m"
  134. sleep 3
  135. COMPOSE_COMMAND="docker-compose"
  136. else
  137. echo -e "\e[31mCannot find Docker-Compose v1 or v2 on the Remote Machine! Please install Docker-Compose v2 on that and re-run the script.\e[0m"
  138. exit 1
  139. fi
  140. }
  141. preflight_local_checks
  142. preflight_remote_checks
  143. SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
  144. COMPOSE_FILE="${SCRIPT_DIR}/../docker-compose.yml"
  145. source "${SCRIPT_DIR}/../mailcow.conf"
  146. CMPS_PRJ=$(echo ${COMPOSE_PROJECT_NAME} | tr -cd 'A-Za-z-_')
  147. SQLIMAGE=$(grep -iEo '(mysql|mariadb)\:.+' "${COMPOSE_FILE}")
  148. echo
  149. echo -e "\033[1mFound compose project name ${CMPS_PRJ} for ${MAILCOW_HOSTNAME}\033[0m"
  150. echo -e "\033[1mFound SQL ${SQLIMAGE}\033[0m"
  151. echo
  152. # Make sure destination exists, rsync can fail under some circumstances
  153. echo -e "\033[1mPreparing remote...\033[0m"
  154. if ! ssh -o StrictHostKeyChecking=no \
  155. -i "${REMOTE_SSH_KEY}" \
  156. ${REMOTE_SSH_HOST} \
  157. -p ${REMOTE_SSH_PORT} \
  158. mkdir -p "${SCRIPT_DIR}/../" ; then
  159. >&2 echo -e "\e[31m[ERR]\e[0m - Could not prepare remote for mailcow base directory transfer"
  160. exit 1
  161. fi
  162. # Syncing the mailcow base directory
  163. echo -e "\033[1mSynchronizing mailcow base directory...\033[0m"
  164. rsync --delete -aH -e "ssh -o StrictHostKeyChecking=no \
  165. -i \"${REMOTE_SSH_KEY}\" \
  166. -p ${REMOTE_SSH_PORT}" \
  167. "${SCRIPT_DIR}/../" root@${REMOTE_SSH_HOST}:"${SCRIPT_DIR}/../"
  168. ec=$?
  169. if [ ${ec} -ne 0 ] && [ ${ec} -ne 24 ]; then
  170. >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer mailcow base directory to remote"
  171. exit 1
  172. fi
  173. # Trigger a Redis save for a consistent Redis copy
  174. echo -ne "\033[1mRunning redis-cli save... \033[0m"
  175. docker exec $(docker ps -qf name=redis-mailcow) redis-cli save
  176. # Syncing volumes related to compose project
  177. # Same here: make sure destination exists
  178. for vol in $(docker volume ls -qf name="${CMPS_PRJ}"); do
  179. mountpoint="$(docker inspect ${vol} | grep Mountpoint | cut -d '"' -f4)"
  180. echo -e "\033[1mCreating remote mountpoint ${mountpoint} for ${vol}...\033[0m"
  181. ssh -o StrictHostKeyChecking=no \
  182. -i "${REMOTE_SSH_KEY}" \
  183. ${REMOTE_SSH_HOST} \
  184. -p ${REMOTE_SSH_PORT} \
  185. mkdir -p "${mountpoint}"
  186. if [[ "${vol}" =~ "mysql-vol-1" ]]; then
  187. # Make sure a previous backup does not exist
  188. rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
  189. echo -e "\033[1mCreating consistent backup of MariaDB volume...\033[0m"
  190. if ! docker run --rm \
  191. --network $(docker network ls -qf name=${CMPS_PRJ}_) \
  192. -v $(docker volume ls -qf name=${CMPS_PRJ}_mysql-vol-1):/var/lib/mysql/:ro \
  193. --entrypoint= \
  194. -v "${SCRIPT_DIR}/../_tmp_mariabackup":/backup \
  195. ${SQLIMAGE} mariabackup --host mysql --user root --password ${DBROOT} --backup --target-dir=/backup 2>/dev/null ; then
  196. >&2 echo -e "\e[31m[ERR]\e[0m - Could not create MariaDB backup on source"
  197. rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
  198. exit 1
  199. fi
  200. if ! docker run --rm \
  201. --network $(docker network ls -qf name=${CMPS_PRJ}_) \
  202. --entrypoint= \
  203. -v "${SCRIPT_DIR}/../_tmp_mariabackup":/backup \
  204. ${SQLIMAGE} mariabackup --prepare --target-dir=/backup 2> /dev/null ; then
  205. >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer MariaDB backup to remote"
  206. rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
  207. exit 1
  208. fi
  209. chown -R 999:999 "${SCRIPT_DIR}/../_tmp_mariabackup"
  210. echo -e "\033[1mSynchronizing MariaDB backup...\033[0m"
  211. rsync --delete --info=progress2 -aH -e "ssh -o StrictHostKeyChecking=no \
  212. -i \"${REMOTE_SSH_KEY}\" \
  213. -p ${REMOTE_SSH_PORT}" \
  214. "${SCRIPT_DIR}/../_tmp_mariabackup/" root@${REMOTE_SSH_HOST}:"${mountpoint}"
  215. ec=$?
  216. if [ ${ec} -ne 0 ] && [ ${ec} -ne 24 ]; then
  217. >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer MariaDB backup to remote"
  218. exit 1
  219. fi
  220. # Cleanup
  221. rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
  222. else
  223. echo -e "\033[1mSynchronizing ${vol} from local ${mountpoint}...\033[0m"
  224. rsync --delete --info=progress2 -aH -e "ssh -o StrictHostKeyChecking=no \
  225. -i \"${REMOTE_SSH_KEY}\" \
  226. -p ${REMOTE_SSH_PORT}" \
  227. "${mountpoint}/" 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 ${vol} from local ${mountpoint} to remote"
  231. exit 1
  232. fi
  233. fi
  234. echo -e "\e[32mCompleted\e[0m"
  235. done
  236. # Restart Dockerd on destination
  237. echo -ne "\033[1mRestarting Docker daemon on remote to detect new volumes... \033[0m"
  238. if ! ssh -o StrictHostKeyChecking=no \
  239. -i "${REMOTE_SSH_KEY}" \
  240. ${REMOTE_SSH_HOST} \
  241. -p ${REMOTE_SSH_PORT} \
  242. systemctl restart docker ; then
  243. >&2 echo -e "\e[31m[ERR]\e[0m - Could not restart Docker daemon on remote"
  244. exit 1
  245. fi
  246. echo "OK"
  247. echo -e "\e[33mPulling images on remote...\e[0m"
  248. echo -e "\e[33mProcess is NOT stuck! Please wait...\e[0m"
  249. if ! ssh -o StrictHostKeyChecking=no \
  250. -i "${REMOTE_SSH_KEY}" \
  251. ${REMOTE_SSH_HOST} \
  252. -p ${REMOTE_SSH_PORT} \
  253. $COMPOSE_COMMAND -f "${SCRIPT_DIR}/../docker-compose.yml" pull --no-parallel --quiet 2>&1 ; then
  254. >&2 echo -e "\e[31m[ERR]\e[0m - Could not pull images on remote"
  255. fi
  256. echo -e "\033[1mExecuting update script and forcing garbage cleanup on remote...\033[0m"
  257. if ! ssh -o StrictHostKeyChecking=no \
  258. -i "${REMOTE_SSH_KEY}" \
  259. ${REMOTE_SSH_HOST} \
  260. -p ${REMOTE_SSH_PORT} \
  261. ${SCRIPT_DIR}/../update.sh -f --gc ; then
  262. >&2 echo -e "\e[31m[ERR]\e[0m - Could not cleanup old images on remote"
  263. fi
  264. echo -e "\e[32mDone\e[0m"