_cold-standby.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 docker-compose 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 docker-compose; 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. }
  102. preflight_local_checks
  103. preflight_remote_checks
  104. SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
  105. COMPOSE_FILE="${SCRIPT_DIR}/../docker-compose.yml"
  106. source "${SCRIPT_DIR}/../mailcow.conf"
  107. CMPS_PRJ=$(echo ${COMPOSE_PROJECT_NAME} | tr -cd 'A-Za-z-_')
  108. SQLIMAGE=$(grep -iEo '(mysql|mariadb)\:.+' "${COMPOSE_FILE}")
  109. echo
  110. echo -e "\033[1mFound compose project name ${CMPS_PRJ} for ${MAILCOW_HOSTNAME}\033[0m"
  111. echo -e "\033[1mFound SQL ${SQLIMAGE}\033[0m"
  112. echo
  113. # Make sure destination exists, rsync can fail under some circumstances
  114. echo -e "\033[1mPreparing remote...\033[0m"
  115. if ! ssh -o StrictHostKeyChecking=no \
  116. -i "${REMOTE_SSH_KEY}" \
  117. ${REMOTE_SSH_HOST} \
  118. -p ${REMOTE_SSH_PORT} \
  119. mkdir -p "${SCRIPT_DIR}/../" ; then
  120. >&2 echo -e "\e[31m[ERR]\e[0m - Could not prepare remote for mailcow base directory transfer"
  121. exit 1
  122. fi
  123. # Syncing the mailcow base directory
  124. echo -e "\033[1mSynchronizing mailcow base directory...\033[0m"
  125. rsync --delete -aH -e "ssh -o StrictHostKeyChecking=no \
  126. -i \"${REMOTE_SSH_KEY}\" \
  127. -p ${REMOTE_SSH_PORT}" \
  128. "${SCRIPT_DIR}/../" root@${REMOTE_SSH_HOST}:"${SCRIPT_DIR}/../"
  129. ec=$?
  130. if [ ${ec} -ne 0 ] && [ ${ec} -ne 24 ]; then
  131. >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer mailcow base directory to remote"
  132. exit 1
  133. fi
  134. # Trigger a Redis save for a consistent Redis copy
  135. echo -ne "\033[1mRunning redis-cli save... \033[0m"
  136. docker exec $(docker ps -qf name=redis-mailcow) redis-cli save
  137. # Syncing volumes related to compose project
  138. # Same here: make sure destination exists
  139. for vol in $(docker volume ls -qf name="${CMPS_PRJ}"); do
  140. mountpoint="$(docker inspect ${vol} | grep Mountpoint | cut -d '"' -f4)"
  141. echo -e "\033[1mCreating remote mountpoint ${mountpoint} for ${vol}...\033[0m"
  142. ssh -o StrictHostKeyChecking=no \
  143. -i "${REMOTE_SSH_KEY}" \
  144. ${REMOTE_SSH_HOST} \
  145. -p ${REMOTE_SSH_PORT} \
  146. mkdir -p "${mountpoint}"
  147. if [[ "${vol}" =~ "mysql-vol-1" ]]; then
  148. # Make sure a previous backup does not exist
  149. rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
  150. echo -e "\033[1mCreating consistent backup of MariaDB volume...\033[0m"
  151. if ! docker run --rm \
  152. --network $(docker network ls -qf name=${CMPS_PRJ}_) \
  153. -v $(docker volume ls -qf name=${CMPS_PRJ}_mysql-vol-1):/var/lib/mysql/:ro \
  154. --entrypoint= \
  155. -v "${SCRIPT_DIR}/../_tmp_mariabackup":/backup \
  156. ${SQLIMAGE} mariabackup --host mysql --user root --password ${DBROOT} --backup --target-dir=/backup 2>/dev/null ; then
  157. >&2 echo -e "\e[31m[ERR]\e[0m - Could not create MariaDB backup on source"
  158. rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
  159. exit 1
  160. fi
  161. if ! docker run --rm \
  162. --network $(docker network ls -qf name=${CMPS_PRJ}_) \
  163. --entrypoint= \
  164. -v "${SCRIPT_DIR}/../_tmp_mariabackup":/backup \
  165. ${SQLIMAGE} mariabackup --prepare --target-dir=/backup 2> /dev/null ; then
  166. >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer MariaDB backup to remote"
  167. rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
  168. exit 1
  169. fi
  170. chown -R 999:999 "${SCRIPT_DIR}/../_tmp_mariabackup"
  171. echo -e "\033[1mSynchronizing MariaDB backup...\033[0m"
  172. rsync --delete --info=progress2 -aH -e "ssh -o StrictHostKeyChecking=no \
  173. -i \"${REMOTE_SSH_KEY}\" \
  174. -p ${REMOTE_SSH_PORT}" \
  175. "${SCRIPT_DIR}/../_tmp_mariabackup/" root@${REMOTE_SSH_HOST}:"${mountpoint}"
  176. ec=$?
  177. if [ ${ec} -ne 0 ] && [ ${ec} -ne 24 ]; then
  178. >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer MariaDB backup to remote"
  179. exit 1
  180. fi
  181. # Cleanup
  182. rm -rf "${SCRIPT_DIR}/../_tmp_mariabackup/"
  183. else
  184. echo -e "\033[1mSynchronizing ${vol} from local ${mountpoint}...\033[0m"
  185. rsync --delete --info=progress2 -aH -e "ssh -o StrictHostKeyChecking=no \
  186. -i \"${REMOTE_SSH_KEY}\" \
  187. -p ${REMOTE_SSH_PORT}" \
  188. "${mountpoint}/" root@${REMOTE_SSH_HOST}:"${mountpoint}"
  189. ec=$?
  190. if [ ${ec} -ne 0 ] && [ ${ec} -ne 24 ]; then
  191. >&2 echo -e "\e[31m[ERR]\e[0m - Could not transfer ${vol} from local ${mountpoint} to remote"
  192. exit 1
  193. fi
  194. fi
  195. echo -e "\e[32mCompleted\e[0m"
  196. done
  197. # Restart Dockerd on destination
  198. echo -ne "\033[1mRestarting Docker daemon on remote to detect new volumes... \033[0m"
  199. if ! ssh -o StrictHostKeyChecking=no \
  200. -i "${REMOTE_SSH_KEY}" \
  201. ${REMOTE_SSH_HOST} \
  202. -p ${REMOTE_SSH_PORT} \
  203. systemctl restart docker ; then
  204. >&2 echo -e "\e[31m[ERR]\e[0m - Could not restart Docker daemon on remote"
  205. exit 1
  206. fi
  207. echo "OK"
  208. echo -e "\e[33mPulling images on remote...\e[0m"
  209. echo -e "\e[33mProcess is NOT stuck! Please wait...\e[0m"
  210. if ! ssh -o StrictHostKeyChecking=no \
  211. -i "${REMOTE_SSH_KEY}" \
  212. ${REMOTE_SSH_HOST} \
  213. -p ${REMOTE_SSH_PORT} \
  214. docker-compose -f "${SCRIPT_DIR}/../docker-compose.yml" pull --no-parallel --quiet 2>&1 ; then
  215. >&2 echo -e "\e[31m[ERR]\e[0m - Could not pull images on remote"
  216. fi
  217. echo -e "\033[1mExecuting update script and forcing garbage cleanup on remote...\033[0m"
  218. if ! ssh -o StrictHostKeyChecking=no \
  219. -i "${REMOTE_SSH_KEY}" \
  220. ${REMOTE_SSH_HOST} \
  221. -p ${REMOTE_SSH_PORT} \
  222. ${SCRIPT_DIR}/../update.sh -f --gc ; then
  223. >&2 echo -e "\e[31m[ERR]\e[0m - Could not cleanup old images on remote"
  224. fi
  225. echo -e "\e[32mDone\e[0m"