_cold-standby.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. if grep --help 2>&1 | head -n 1 | grep -q -i "busybox"; then
  69. echo -e "\e[31mBusyBox grep detected on local system, please install GNU grep\e[0m"
  70. exit 1
  71. fi
  72. }
  73. function preflight_remote_checks() {
  74. if ! ssh -o StrictHostKeyChecking=no \
  75. -i "${REMOTE_SSH_KEY}" \
  76. ${REMOTE_SSH_HOST} \
  77. -p ${REMOTE_SSH_PORT} \
  78. rsync --version > /dev/null ; then
  79. >&2 echo -e "\e[31mCould not verify connection to ${REMOTE_SSH_HOST}\e[0m"
  80. >&2 echo -e "\e[31mPlease check the output above (is rsync >= 3.1.0 installed on the remote system?)\e[0m"
  81. exit 1
  82. fi
  83. if ssh -o StrictHostKeyChecking=no \
  84. -i "${REMOTE_SSH_KEY}" \
  85. ${REMOTE_SSH_HOST} \
  86. -p ${REMOTE_SSH_PORT} \
  87. grep --help 2>&1 | head -n 1 | grep -q -i "busybox" ; then
  88. >&2 echo -e "\e[31mBusyBox grep detected on remote system ${REMOTE_SSH_HOST}, please install GNU grep\e[0m"
  89. exit 1
  90. fi
  91. for bin in rsync docker; do
  92. if ! ssh -o StrictHostKeyChecking=no \
  93. -i "${REMOTE_SSH_KEY}" \
  94. ${REMOTE_SSH_HOST} \
  95. -p ${REMOTE_SSH_PORT} \
  96. which ${bin} > /dev/null ; then
  97. >&2 echo -e "\e[31mCannot find ${bin} in remote PATH, exiting...\e[0m"
  98. exit 1
  99. fi
  100. done
  101. ssh -o StrictHostKeyChecking=no \
  102. -i "${REMOTE_SSH_KEY}" \
  103. ${REMOTE_SSH_HOST} \
  104. -p ${REMOTE_SSH_PORT} \
  105. "bash -s" << "EOF"
  106. if docker compose > /dev/null 2>&1; then
  107. exit 0
  108. elif docker-compose version --short | grep "^2." > /dev/null 2>&1; then
  109. exit 1
  110. else
  111. exit 2
  112. fi
  113. EOF
  114. if [ $? = 0 ]; then
  115. COMPOSE_COMMAND="docker compose"
  116. echo "DEBUG: Using native docker compose on remote"
  117. elif [ $? = 1 ]; then
  118. COMPOSE_COMMAND="docker-compose"
  119. echo "DEBUG: Using standalone docker compose on remote"
  120. else
  121. echo -e "\e[31mCannot find any Docker Compose on remote, exiting...\e[0m"
  122. exit 1
  123. fi
  124. }
  125. SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
  126. source "${SCRIPT_DIR}/../mailcow.conf"
  127. COMPOSE_FILE="${SCRIPT_DIR}/../docker-compose.yml"
  128. CMPS_PRJ=$(echo ${COMPOSE_PROJECT_NAME} | tr -cd 'A-Za-z-_')
  129. SQLIMAGE=$(grep -iEo '(mysql|mariadb)\:.+' "${COMPOSE_FILE}")
  130. preflight_local_checks
  131. preflight_remote_checks
  132. echo
  133. echo -e "\033[1mFound compose project name ${CMPS_PRJ} for ${MAILCOW_HOSTNAME}\033[0m"
  134. echo -e "\033[1mFound SQL ${SQLIMAGE}\033[0m"
  135. echo
  136. # Make sure destination exists, rsync can fail under some circumstances
  137. echo -e "\033[1mPreparing remote...\033[0m"
  138. if ! ssh -o StrictHostKeyChecking=no \
  139. -i "${REMOTE_SSH_KEY}" \
  140. ${REMOTE_SSH_HOST} \
  141. -p ${REMOTE_SSH_PORT} \
  142. mkdir -p "${SCRIPT_DIR}/../" ; then
  143. >&2 echo -e "\e[31m[ERR]\e[0m - Could not prepare remote for mailcow base directory transfer"
  144. exit 1
  145. fi
  146. # Syncing the mailcow base directory
  147. echo -e "\033[1mSynchronizing mailcow base directory...\033[0m"
  148. rsync --delete -aH -e "ssh -o StrictHostKeyChecking=no \
  149. -i \"${REMOTE_SSH_KEY}\" \
  150. -p ${REMOTE_SSH_PORT}" \
  151. "${SCRIPT_DIR}/../" root@${REMOTE_SSH_HOST}:"${SCRIPT_DIR}/../"
  152. ec=$?
  153. if [ ${ec} -ne 0 ] && [ ${ec} -ne 24 ]; then
  154. >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer mailcow base directory to remote"
  155. exit 1
  156. fi
  157. # Trigger a Redis save for a consistent Redis copy
  158. echo -ne "\033[1mRunning redis-cli save... \033[0m"
  159. docker exec $(docker ps -qf name=redis-mailcow) redis-cli save
  160. # Syncing volumes related to compose project
  161. # Same here: make sure destination exists
  162. for vol in $(docker volume ls -qf name="${CMPS_PRJ}"); do
  163. mountpoint="$(docker inspect ${vol} | grep Mountpoint | cut -d '"' -f4)"
  164. echo -e "\033[1mCreating remote mountpoint ${mountpoint} for ${vol}...\033[0m"
  165. ssh -o StrictHostKeyChecking=no \
  166. -i "${REMOTE_SSH_KEY}" \
  167. ${REMOTE_SSH_HOST} \
  168. -p ${REMOTE_SSH_PORT} \
  169. mkdir -p "${mountpoint}"
  170. if [[ "${vol}" =~ "mysql-vol-1" ]]; then
  171. # Make sure a previous backup does not exist
  172. rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
  173. echo -e "\033[1mCreating consistent backup of MariaDB volume...\033[0m"
  174. if ! docker run --rm \
  175. --network $(docker network ls -qf name=${CMPS_PRJ}_) \
  176. -v $(docker volume ls -qf name=${CMPS_PRJ}_mysql-vol-1):/var/lib/mysql/:ro \
  177. --entrypoint= \
  178. -v "${SCRIPT_DIR}/../_tmp_mariabackup":/backup \
  179. ${SQLIMAGE} mariabackup --host mysql --user root --password ${DBROOT} --backup --target-dir=/backup 2>/dev/null ; then
  180. >&2 echo -e "\e[31m[ERR]\e[0m - Could not create MariaDB backup on source"
  181. rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
  182. exit 1
  183. fi
  184. if ! docker run --rm \
  185. --network $(docker network ls -qf name=${CMPS_PRJ}_) \
  186. --entrypoint= \
  187. -v "${SCRIPT_DIR}/../_tmp_mariabackup":/backup \
  188. ${SQLIMAGE} mariabackup --prepare --target-dir=/backup 2> /dev/null ; then
  189. >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer MariaDB backup to remote"
  190. rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
  191. exit 1
  192. fi
  193. chown -R 999:999 "${SCRIPT_DIR}/../_tmp_mariabackup"
  194. echo -e "\033[1mSynchronizing MariaDB backup...\033[0m"
  195. rsync --delete --info=progress2 -aH -e "ssh -o StrictHostKeyChecking=no \
  196. -i \"${REMOTE_SSH_KEY}\" \
  197. -p ${REMOTE_SSH_PORT}" \
  198. "${SCRIPT_DIR}/../_tmp_mariabackup/" root@${REMOTE_SSH_HOST}:"${mountpoint}"
  199. ec=$?
  200. if [ ${ec} -ne 0 ] && [ ${ec} -ne 24 ]; then
  201. >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer MariaDB backup to remote"
  202. exit 1
  203. fi
  204. # Cleanup
  205. rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
  206. else
  207. echo -e "\033[1mSynchronizing ${vol} from local ${mountpoint}...\033[0m"
  208. rsync --delete --info=progress2 -aH -e "ssh -o StrictHostKeyChecking=no \
  209. -i \"${REMOTE_SSH_KEY}\" \
  210. -p ${REMOTE_SSH_PORT}" \
  211. "${mountpoint}/" root@${REMOTE_SSH_HOST}:"${mountpoint}"
  212. ec=$?
  213. if [ ${ec} -ne 0 ] && [ ${ec} -ne 24 ]; then
  214. >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer ${vol} from local ${mountpoint} to remote"
  215. exit 1
  216. fi
  217. fi
  218. echo -e "\e[32mCompleted\e[0m"
  219. done
  220. # Restart Dockerd on destination
  221. echo -ne "\033[1mRestarting Docker daemon on remote to detect new volumes... \033[0m"
  222. if ! ssh -o StrictHostKeyChecking=no \
  223. -i "${REMOTE_SSH_KEY}" \
  224. ${REMOTE_SSH_HOST} \
  225. -p ${REMOTE_SSH_PORT} \
  226. systemctl restart docker ; then
  227. >&2 echo -e "\e[31m[ERR]\e[0m - Could not restart Docker daemon on remote"
  228. exit 1
  229. fi
  230. echo "OK"
  231. echo -e "\e[33mPulling images on remote...\e[0m"
  232. echo -e "\e[33mProcess is NOT stuck! Please wait...\e[0m"
  233. if ! ssh -o StrictHostKeyChecking=no \
  234. -i "${REMOTE_SSH_KEY}" \
  235. ${REMOTE_SSH_HOST} \
  236. -p ${REMOTE_SSH_PORT} \
  237. ${COMPOSE_COMMAND} -f "${SCRIPT_DIR}/../docker-compose.yml" pull --no-parallel --quiet 2>&1 ; then
  238. >&2 echo -e "\e[31m[ERR]\e[0m - Could not pull images on remote"
  239. fi
  240. echo -e "\033[1mExecuting update script and forcing garbage cleanup on remote...\033[0m"
  241. if ! ssh -o StrictHostKeyChecking=no \
  242. -i "${REMOTE_SSH_KEY}" \
  243. ${REMOTE_SSH_HOST} \
  244. -p ${REMOTE_SSH_PORT} \
  245. ${SCRIPT_DIR}/../update.sh -f --gc ; then
  246. >&2 echo -e "\e[31m[ERR]\e[0m - Could not cleanup old images on remote"
  247. fi
  248. echo -e "\e[32mDone\e[0m"