update.sh 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. #!/usr/bin/env bash
  2. ############## Begin Function Section ##############
  3. SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  4. MAILCOW_CONF="${SCRIPT_DIR}/mailcow.conf"
  5. # Ensure the script is run from the directory that contains mailcow.conf
  6. if [ ! -f "${PWD}/mailcow.conf" ]; then
  7. if [ -f "${SCRIPT_DIR}/mailcow.conf" ]; then
  8. echo -e "\e[33mPlease run this script directly from the mailcow installation directory:\e[0m"
  9. echo -e " \e[36mcd ${SCRIPT_DIR} && ./update.sh\e[0m"
  10. exit 1
  11. else
  12. echo -e "\e[31mmailcow.conf not found in current directory or script directory (\e[36m${SCRIPT_DIR}\e[31m).\e[0m"
  13. echo -e "\e[33mRun this script directly from your mailcow installation directory.\e[0m"
  14. exit 1
  15. fi
  16. fi
  17. BRANCH="$(cd "${SCRIPT_DIR}" && git rev-parse --abbrev-ref HEAD)"
  18. MODULE_DIR="${SCRIPT_DIR}/_modules"
  19. if [[ ! -d "${MODULE_DIR}" || -z "$(ls -A "${MODULE_DIR}")" ]]; then
  20. echo -e "\e[33m_modules is missing or empty – fetching all Modules from origin/${BRANCH}…\e[0m"
  21. git fetch origin "${BRANCH}"
  22. git checkout "origin/${BRANCH}" -- _modules
  23. echo -e "\e[33mDone. Please restart the script...\e[0m"
  24. exit 2
  25. fi
  26. source _modules/scripts/core.sh
  27. source _modules/scripts/ipv6_controller.sh
  28. source _modules/scripts/new_options.sh
  29. source _modules/scripts/migrate_options.sh
  30. ############## End Function Section ##############
  31. # Check permissions
  32. if [ "$(id -u)" -ne "0" ]; then
  33. echo "You need to be root"
  34. exit 1
  35. fi
  36. # Run pre-update-hook
  37. if [ -f "${SCRIPT_DIR}/pre_update_hook.sh" ]; then
  38. bash "${SCRIPT_DIR}/pre_update_hook.sh"
  39. fi
  40. # Exit on error and pipefail
  41. set -o pipefail
  42. # Setting high dc timeout
  43. export COMPOSE_HTTP_TIMEOUT=600
  44. # Add /opt/bin to PATH
  45. PATH=$PATH:/opt/bin
  46. umask 0022
  47. # Unset COMPOSE_COMMAND and DOCKER_COMPOSE_VERSION Variable to be on the newest state.
  48. unset COMPOSE_COMMAND
  49. unset DOCKER_COMPOSE_VERSION
  50. get_installed_tools
  51. get_docker_version
  52. export LC_ALL=C
  53. DATE=$(date +%Y-%m-%d_%H_%M_%S)
  54. BRANCH="$(cd "${SCRIPT_DIR}"; git rev-parse --abbrev-ref HEAD)"
  55. while (($#)); do
  56. case "${1}" in
  57. --check|-c)
  58. echo "Checking remote code for updates..."
  59. LATEST_REV=$(git ls-remote --exit-code --refs --quiet https://github.com/mailcow/mailcow-dockerized "${BRANCH}" | cut -f1)
  60. if [ "$?" -ne 0 ]; then
  61. echo "A problem occurred while trying to fetch the latest revision from github."
  62. exit 99
  63. fi
  64. if [[ -z $(git log HEAD --pretty=format:"%H" | grep "${LATEST_REV}") ]]; then
  65. echo -e "Updated code is available.\nThe changes can be found here: https://github.com/mailcow/mailcow-dockerized/commits/master"
  66. git log --date=short --pretty=format:"%ad - %s" "$(git rev-parse --short HEAD)"..origin/master
  67. exit 0
  68. else
  69. echo "No updates available."
  70. exit 3
  71. fi
  72. ;;
  73. --check-tags)
  74. echo "Checking remote tags for updates..."
  75. LATEST_TAG_REV=$(git ls-remote --exit-code --quiet --tags origin | tail -1 | cut -f1)
  76. if [ "$?" -ne 0 ]; then
  77. echo "A problem occurred while trying to fetch the latest tag from github."
  78. exit 99
  79. fi
  80. if [[ -z $(git log HEAD --pretty=format:"%H" | grep "${LATEST_TAG_REV}") ]]; then
  81. echo -e "New tag is available.\nThe changes can be found here: https://github.com/mailcow/mailcow-dockerized/releases/latest"
  82. exit 0
  83. else
  84. echo "No updates available."
  85. exit 3
  86. fi
  87. ;;
  88. --ours)
  89. MERGE_STRATEGY=ours
  90. ;;
  91. --skip-start)
  92. SKIP_START=y
  93. ;;
  94. --skip-ping-check)
  95. SKIP_PING_CHECK=y
  96. ;;
  97. --stable)
  98. CURRENT_BRANCH="$(cd "${SCRIPT_DIR}"; git rev-parse --abbrev-ref HEAD)"
  99. NEW_BRANCH="master"
  100. ;;
  101. --gc)
  102. echo -e "\e[32mCollecting garbage...\e[0m"
  103. docker_garbage
  104. exit 0
  105. ;;
  106. --nightly)
  107. CURRENT_BRANCH="$(cd "${SCRIPT_DIR}"; git rev-parse --abbrev-ref HEAD)"
  108. NEW_BRANCH="nightly"
  109. ;;
  110. --prefetch)
  111. echo -e "\e[32mPrefetching images...\e[0m"
  112. prefetch_images
  113. exit 0
  114. ;;
  115. -f|--force)
  116. echo -e "\e[32mRunning in forced mode...\e[0m"
  117. FORCE=y
  118. ;;
  119. -d|--dev)
  120. echo -e "\e[32mRunning in Developer mode...\e[0m"
  121. DEV=y
  122. ;;
  123. --legacy)
  124. CURRENT_BRANCH="$(cd "${SCRIPT_DIR}"; git rev-parse --abbrev-ref HEAD)"
  125. NEW_BRANCH="legacy"
  126. ;;
  127. --help|-h)
  128. echo './update.sh [-c|--check, --check-tags, --ours, --gc, --nightly, --prefetch, --skip-start, --skip-ping-check, --stable, --legacy, -f|--force, -d|--dev, -h|--help]
  129. -c|--check - Check for updates and exit (exit codes => 0: update available, 3: no updates)
  130. --check-tags - Check for newer tags and exit (exit codes => 0: newer tag available, 3: no newer tag)
  131. --ours - Use merge strategy option "ours" to solve conflicts in favor of non-mailcow code (local changes over remote changes), not recommended!
  132. --gc - Run garbage collector to delete old image tags
  133. --nightly - Switch your mailcow updates to the unstable (nightly) branch. FOR TESTING PURPOSES ONLY!!!!
  134. --prefetch - Only prefetch new images and exit (useful to prepare updates)
  135. --skip-start - Do not start mailcow after update
  136. --skip-ping-check - Skip ICMP Check to public DNS resolvers (Use it only if you'\''ve blocked any ICMP Connections to your mailcow machine)
  137. --stable - Switch your mailcow updates to the stable (master) branch. Default unless you changed it with --nightly or --legacy.
  138. --legacy - Switch your mailcow updates to the legacy branch. The legacy branch will only receive security updates until February 2026.
  139. -f|--force - Force update, do not ask questions
  140. -d|--dev - Enables Developer Mode (No Checkout of update.sh for tests)
  141. '
  142. exit 0
  143. esac
  144. shift
  145. done
  146. [[ ! -f mailcow.conf ]] && { echo -e "\e[31mmailcow.conf is missing! Is mailcow installed?\e[0m"; exit 1;}
  147. chmod 600 mailcow.conf
  148. source mailcow.conf
  149. get_compose_type
  150. DOTS=${MAILCOW_HOSTNAME//[^.]};
  151. if [ ${#DOTS} -lt 1 ]; then
  152. echo -e "\e[31mMAILCOW_HOSTNAME (${MAILCOW_HOSTNAME}) is not a FQDN!\e[0m"
  153. sleep 1
  154. echo "Please change it to a FQDN and redeploy the stack with $COMPOSE_COMMAND up -d"
  155. exit 1
  156. elif [[ "${MAILCOW_HOSTNAME: -1}" == "." ]]; then
  157. echo "MAILCOW_HOSTNAME (${MAILCOW_HOSTNAME}) is ending with a dot. This is not a valid FQDN!"
  158. exit 1
  159. elif [ ${#DOTS} -eq 1 ]; then
  160. echo -e "\e[33mMAILCOW_HOSTNAME (${MAILCOW_HOSTNAME}) does not contain a Subdomain. This is not fully tested and may cause issues.\e[0m"
  161. echo "Find more information about why this message exists here: https://github.com/mailcow/mailcow-dockerized/issues/1572"
  162. read -r -p "Do you want to proceed anyway? [y/N] " response
  163. if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  164. echo "OK. Proceeding."
  165. else
  166. echo "OK. Exiting."
  167. exit 1
  168. fi
  169. fi
  170. detect_bad_asn
  171. if [[ ("${SKIP_PING_CHECK}" == "y") ]]; then
  172. echo -e "\e[32mSkipping Ping Check...\e[0m"
  173. else
  174. echo -en "Checking internet connection... "
  175. if ! check_online_status; then
  176. echo -e "\e[31mfailed\e[0m"
  177. exit 1
  178. else
  179. echo -e "\e[32mOK\e[0m"
  180. fi
  181. fi
  182. if ! [ "$NEW_BRANCH" ]; then
  183. echo -e "\e[33mDetecting which build your mailcow runs on...\e[0m"
  184. sleep 1
  185. if [ "${BRANCH}" == "master" ]; then
  186. echo -e "\e[32mYou are receiving stable updates (master).\e[0m"
  187. echo -e "\e[33mTo change that run the update.sh Script one time with the --nightly parameter to switch to nightly builds.\e[0m"
  188. elif [ "${BRANCH}" == "nightly" ]; then
  189. echo -e "\e[31mYou are receiving unstable updates (nightly). These are for testing purposes only!!!\e[0m"
  190. sleep 1
  191. echo -e "\e[33mTo change that run the update.sh Script one time with the --stable parameter to switch to stable builds.\e[0m"
  192. elif [ "${BRANCH}" == "legacy" ]; then
  193. echo -e "\e[31mYou are receiving legacy updates. The legacy branch will only receive security updates until February 2026.\e[0m"
  194. sleep 1
  195. echo -e "\e[33mTo change that run the update.sh Script one time with the --stable parameter to switch to stable builds.\e[0m"
  196. else
  197. echo -e "\e[33mYou are receiving updates from an unsupported branch.\e[0m"
  198. sleep 1
  199. echo -e "\e[33mThe mailcow stack might still work but it is recommended to switch to the master branch (stable builds).\e[0m"
  200. echo -e "\e[33mTo change that run the update.sh Script one time with the --stable parameter to switch to stable builds.\e[0m"
  201. fi
  202. elif [ "$FORCE" ]; then
  203. echo -e "\e[31mYou are running in forced mode!\e[0m"
  204. echo -e "\e[31mA Branch Switch can only be performed manually (monitored).\e[0m"
  205. echo -e "\e[31mPlease rerun the update.sh Script without the --force/-f parameter.\e[0m"
  206. sleep 1
  207. elif [ "$NEW_BRANCH" == "master" ] && [ "$CURRENT_BRANCH" != "master" ]; then
  208. echo -e "\e[33mYou are about to switch your mailcow updates to the stable (master) branch.\e[0m"
  209. sleep 1
  210. echo -e "\e[33mBefore you do: Please take a backup of all components to ensure that no data is lost...\e[0m"
  211. sleep 1
  212. echo -e "\e[31mWARNING: Please see on GitHub or ask in the community if a switch to master is stable or not.
  213. In some rear cases an update back to master can destroy your mailcow configuration such as database upgrade, etc.
  214. Normally an upgrade back to master should be safe during each full release.
  215. Check GitHub for Database changes and update only if there similar to the full release!\e[0m"
  216. read -r -p "Are you sure you that want to continue upgrading to the stable (master) branch? [y/N] " response
  217. if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  218. echo "OK. If you prepared yourself for that please run the update.sh Script with the --stable parameter again to trigger this process here."
  219. exit 0
  220. fi
  221. BRANCH="$NEW_BRANCH"
  222. DIFF_DIRECTORY=update_diffs
  223. DIFF_FILE="${DIFF_DIRECTORY}/diff_before_upgrade_to_master_$(date +"%Y-%m-%d-%H-%M-%S")"
  224. mv diff_before_upgrade* "${DIFF_DIRECTORY}/" 2> /dev/null
  225. if ! git diff-index --quiet HEAD; then
  226. echo -e "\e[32mSaving diff to ${DIFF_FILE}...\e[0m"
  227. mkdir -p "${DIFF_DIRECTORY}"
  228. git diff "${BRANCH}" --stat > "${DIFF_FILE}"
  229. git diff "${BRANCH}" >> "${DIFF_FILE}"
  230. fi
  231. echo -e "\e[32mSwitching Branch to ${BRANCH}...\e[0m"
  232. git fetch origin
  233. git checkout -f "${BRANCH}"
  234. elif [ "$NEW_BRANCH" == "nightly" ] && [ "$CURRENT_BRANCH" != "nightly" ]; then
  235. echo -e "\e[33mYou are about to switch your mailcow Updates to the unstable (nightly) branch.\e[0m"
  236. sleep 1
  237. echo -e "\e[33mBefore you do: Please take a backup of all components to ensure that no Data is lost...\e[0m"
  238. sleep 1
  239. echo -e "\e[31mWARNING: A switch to nightly is possible any time. But a switch back (to master) isn't.\e[0m"
  240. read -r -p "Are you sure you that want to continue upgrading to the unstable (nightly) branch? [y/N] " response
  241. if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  242. echo "OK. If you prepared yourself for that please run the update.sh Script with the --nightly parameter again to trigger this process here."
  243. exit 0
  244. fi
  245. BRANCH=$NEW_BRANCH
  246. DIFF_DIRECTORY=update_diffs
  247. DIFF_FILE=${DIFF_DIRECTORY}/diff_before_upgrade_to_nightly_$(date +"%Y-%m-%d-%H-%M-%S")
  248. mv diff_before_upgrade* ${DIFF_DIRECTORY}/ 2> /dev/null
  249. if ! git diff-index --quiet HEAD; then
  250. echo -e "\e[32mSaving diff to ${DIFF_FILE}...\e[0m"
  251. mkdir -p ${DIFF_DIRECTORY}
  252. git diff "${BRANCH}" --stat > "${DIFF_FILE}"
  253. git diff "${BRANCH}" >> "${DIFF_FILE}"
  254. fi
  255. git fetch origin
  256. git checkout -f "${BRANCH}"
  257. elif [ "$NEW_BRANCH" == "legacy" ] && [ "$CURRENT_BRANCH" != "legacy" ]; then
  258. echo -e "\e[33mYou are about to switch your mailcow Updates to the legacy branch.\e[0m"
  259. sleep 1
  260. echo -e "\e[33mBefore you do: Please take a backup of all components to ensure that no Data is lost...\e[0m"
  261. sleep 1
  262. echo -e "\e[31mWARNING: A switch to stable or nightly is possible any time.\e[0m"
  263. read -r -p "Are you sure you want to continue upgrading to the legacy branch? [y/N] " response
  264. if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  265. echo "OK. If you prepared yourself for that please run the update.sh Script with the --legacy parameter again to trigger this process here."
  266. exit 0
  267. fi
  268. BRANCH=$NEW_BRANCH
  269. DIFF_DIRECTORY=update_diffs
  270. DIFF_FILE=${DIFF_DIRECTORY}/diff_before_upgrade_to_legacy_$(date +"%Y-%m-%d-%H-%M-%S")
  271. mv diff_before_upgrade* ${DIFF_DIRECTORY}/ 2> /dev/null
  272. if ! git diff-index --quiet HEAD; then
  273. echo -e "\e[32mSaving diff to ${DIFF_FILE}...\e[0m"
  274. mkdir -p ${DIFF_DIRECTORY}
  275. git diff "${BRANCH}" --stat > "${DIFF_FILE}"
  276. git diff "${BRANCH}" >> "${DIFF_FILE}"
  277. fi
  278. git fetch origin
  279. git checkout -f "${BRANCH}"
  280. fi
  281. if [ ! "$DEV" ]; then
  282. EXIT_COUNT=0
  283. echo -e "\e[32mChecking for newer update script...\e[0m"
  284. SHA1_1="$(sha1sum update.sh)"
  285. git fetch origin
  286. git checkout "origin/${BRANCH}" -- update.sh
  287. SHA1_2="$(sha1sum update.sh)"
  288. if [[ "${SHA1_1}" != "${SHA1_2}" ]]; then
  289. chmod +x update.sh
  290. EXIT_COUNT+=1
  291. fi
  292. MODULE_DIR="$(dirname "$0")/_modules"
  293. echo -e "\e[32mChecking for updates in _modules...\e[0m"
  294. if [ ! -d "${MODULE_DIR}" ] || [ -z "$(ls -A "${MODULE_DIR}")" ]; then
  295. echo -e "\e[33m_modules missing or empty — fetching from origin...\e[0m"
  296. git checkout "origin/${BRANCH}" -- _modules
  297. else
  298. OLD_SUM="$(find "${MODULE_DIR}" -type f -exec sha1sum {} \; | sort | sha1sum)"
  299. git fetch origin
  300. git checkout "origin/${BRANCH}" -- _modules
  301. NEW_SUM="$(find "${MODULE_DIR}" -type f -exec sha1sum {} \; | sort | sha1sum)"
  302. if [[ "${OLD_SUM}" != "${NEW_SUM}" ]]; then
  303. EXIT_COUNT+=1
  304. fi
  305. fi
  306. if [ ${EXIT_COUNT} -ge 1 ]; then
  307. echo "Changes for the update Script, please run this script again, exiting!"
  308. exit 2
  309. fi
  310. fi
  311. if [ ! "$FORCE" ]; then
  312. read -r -p "Are you sure you want to update mailcow: dockerized? All containers will be stopped. [y/N] " response
  313. if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  314. echo "OK, exiting."
  315. exit 0
  316. fi
  317. detect_major_update
  318. fi
  319. echo -e "\e[32mValidating docker-compose stack configuration...\e[0m"
  320. sed -i 's/HTTPS_BIND:-:/HTTPS_BIND:-/g' docker-compose.yml
  321. sed -i 's/HTTP_BIND:-:/HTTP_BIND:-/g' docker-compose.yml
  322. if ! $COMPOSE_COMMAND config -q; then
  323. echo -e "\e[31m\nOh no, something went wrong. Please check the error message above.\e[0m"
  324. exit 1
  325. fi
  326. echo -e "\e[32mChecking for conflicting bridges...\e[0m"
  327. MAILCOW_BRIDGE=$($COMPOSE_COMMAND config | grep -i com.docker.network.bridge.name | cut -d':' -f2)
  328. while read NAT_ID; do
  329. iptables -t nat -D POSTROUTING "$NAT_ID"
  330. done < <(iptables -L -vn -t nat --line-numbers | grep "$IPV4_NETWORK" | grep -E 'MASQUERADE.*all' | grep -v "${MAILCOW_BRIDGE}" | cut -d' ' -f1)
  331. DIFF_DIRECTORY=update_diffs
  332. DIFF_FILE=${DIFF_DIRECTORY}/diff_before_update_$(date +"%Y-%m-%d-%H-%M-%S")
  333. mv diff_before_update* ${DIFF_DIRECTORY}/ 2> /dev/null
  334. if ! git diff-index --quiet HEAD; then
  335. echo -e "\e[32mSaving diff to ${DIFF_FILE}...\e[0m"
  336. mkdir -p ${DIFF_DIRECTORY}
  337. git diff --stat > "${DIFF_FILE}"
  338. git diff >> "${DIFF_FILE}"
  339. fi
  340. echo -e "\e[32mPrefetching images...\e[0m"
  341. prefetch_images
  342. echo -e "\e[32mStopping mailcow...\e[0m"
  343. sleep 2
  344. MAILCOW_CONTAINERS=($($COMPOSE_COMMAND ps -q))
  345. $COMPOSE_COMMAND down
  346. echo -e "\e[32mChecking for remaining containers...\e[0m"
  347. sleep 2
  348. for container in "${MAILCOW_CONTAINERS[@]}"; do
  349. docker rm -f "$container" 2> /dev/null
  350. done
  351. configure_ipv6
  352. [[ -f data/conf/nginx/ZZZ-ejabberd.conf ]] && rm data/conf/nginx/ZZZ-ejabberd.conf
  353. migrate_config_options
  354. adapt_new_options
  355. if [ ! "$DEV" ]; then
  356. DEFAULT_REPO="https://github.com/mailcow/mailcow-dockerized"
  357. CURRENT_REPO=$(git config --get remote.origin.url)
  358. if [ "$CURRENT_REPO" != "$DEFAULT_REPO" ]; then
  359. echo "The Repository currently used is not the default mailcow Repository."
  360. echo "Currently Repository: $CURRENT_REPO"
  361. echo "Default Repository: $DEFAULT_REPO"
  362. if [ ! "$FORCE" ]; then
  363. read -r -p "Should it be changed back to default? [y/N] " repo_response
  364. if [[ "$repo_response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  365. git remote set-url origin $DEFAULT_REPO
  366. fi
  367. else
  368. echo "Running in forced mode... setting Repo to default!"
  369. git remote set-url origin $DEFAULT_REPO
  370. fi
  371. fi
  372. fi
  373. if [ ! "$DEV" ]; then
  374. echo -e "\e[32mCommitting current status...\e[0m"
  375. [[ -z "$(git config user.name)" ]] && git config user.name moo
  376. [[ -z "$(git config user.email)" ]] && git config user.email moo@cow.moo
  377. [[ ! -z $(git ls-files data/conf/rspamd/override.d/worker-controller-password.inc) ]] && git rm data/conf/rspamd/override.d/worker-controller-password.inc
  378. git add -u
  379. git commit -am "Before update on ${DATE}" > /dev/null
  380. echo -e "\e[32mFetching updated code from remote...\e[0m"
  381. git fetch origin #${BRANCH}
  382. echo -e "\e[32mMerging local with remote code (recursive, strategy: \"${MERGE_STRATEGY:-theirs}\", options: \"patience\"...\e[0m"
  383. git config merge.defaultToUpstream true
  384. git merge -X"${MERGE_STRATEGY:-theirs}" -Xpatience -m "After update on ${DATE}"
  385. # Need to use a variable to not pass return codes of if checks
  386. MERGE_RETURN=$?
  387. if [[ ${MERGE_RETURN} == 128 ]]; then
  388. echo -e "\e[31m\nOh no, what happened?\n=> You most likely added files to your local mailcow instance that were now added to the official mailcow repository. Please move them to another location before updating mailcow.\e[0m"
  389. exit 1
  390. elif [[ ${MERGE_RETURN} == 1 ]]; then
  391. echo -e "\e[93mPotential conflict, trying to fix...\e[0m"
  392. git status --porcelain | grep -E "UD|DU" | awk '{print $2}' | xargs rm -v
  393. git add -A
  394. git commit -m "After update on ${DATE}" > /dev/null
  395. git checkout .
  396. echo -e "\e[32mRemoved and recreated files if necessary.\e[0m"
  397. elif [[ ${MERGE_RETURN} != 0 ]]; then
  398. echo -e "\e[31m\nOh no, something went wrong. Please check the error message above.\e[0m"
  399. echo
  400. echo "Run $COMPOSE_COMMAND up -d to restart your stack without updates or try again after fixing the mentioned errors."
  401. exit 1
  402. fi
  403. else
  404. echo -e "\e[33mDEVELOPER MODE: Not creating a git diff and commiting it to prevent development stuff within a backup diff...\e[0m"
  405. fi
  406. echo -e "\e[32mFetching new images, if any...\e[0m"
  407. sleep 2
  408. $COMPOSE_COMMAND pull
  409. # Fix missing SSL, does not overwrite existing files
  410. [[ ! -d data/assets/ssl ]] && mkdir -p data/assets/ssl
  411. cp -n -d data/assets/ssl-example/*.pem data/assets/ssl/
  412. echo -e "Checking IPv6 settings... "
  413. if grep -q 'SYSCTL_IPV6_DISABLED=1' mailcow.conf; then
  414. echo
  415. echo '!! IMPORTANT !!'
  416. echo
  417. echo 'SYSCTL_IPV6_DISABLED was removed due to complications. IPv6 can be disabled by editing "docker-compose.yml" and setting "enable_ipv6: true" to "enable_ipv6: false".'
  418. echo "This setting will only be active after a complete shutdown of mailcow by running $COMPOSE_COMMAND down followed by $COMPOSE_COMMAND up -d."
  419. echo
  420. echo '!! IMPORTANT !!'
  421. echo
  422. read -p "Press any key to continue..." < /dev/tty
  423. fi
  424. # Checking for old project name bug
  425. sed -i --follow-symlinks 's#COMPOSEPROJECT_NAME#COMPOSE_PROJECT_NAME#g' mailcow.conf
  426. # Fix Rspamd maps
  427. if [ -f data/conf/rspamd/custom/global_from_blacklist.map ]; then
  428. mv data/conf/rspamd/custom/global_from_blacklist.map data/conf/rspamd/custom/global_smtp_from_blacklist.map
  429. fi
  430. if [ -f data/conf/rspamd/custom/global_from_whitelist.map ]; then
  431. mv data/conf/rspamd/custom/global_from_whitelist.map data/conf/rspamd/custom/global_smtp_from_whitelist.map
  432. fi
  433. # Fix deprecated metrics.conf
  434. if [ -f "data/conf/rspamd/local.d/metrics.conf" ]; then
  435. if [ ! -z "$(git diff --name-only origin/master data/conf/rspamd/local.d/metrics.conf)" ]; then
  436. echo -e "\e[33mWARNING\e[0m - Please migrate your customizations of data/conf/rspamd/local.d/metrics.conf to actions.conf and groups.conf after this update."
  437. echo "The deprecated configuration file metrics.conf will be moved to metrics.conf_deprecated after updating mailcow."
  438. fi
  439. mv data/conf/rspamd/local.d/metrics.conf data/conf/rspamd/local.d/metrics.conf_deprecated
  440. fi
  441. # Set app_info.inc.php
  442. if [ ${BRANCH} == "master" ]; then
  443. mailcow_git_version=$(git describe --tags $(git rev-list --tags --max-count=1))
  444. elif [ ${BRANCH} == "nightly" ]; then
  445. mailcow_git_version=$(git rev-parse --short $(git rev-parse @{upstream}))
  446. mailcow_last_git_version=""
  447. else
  448. mailcow_git_version=$(git rev-parse --short HEAD)
  449. mailcow_last_git_version=""
  450. fi
  451. mailcow_git_commit=$(git rev-parse "origin/${BRANCH}")
  452. mailcow_git_commit_date=$(git log -1 --format=%ci @{upstream} )
  453. if [ $? -eq 0 ]; then
  454. echo '<?php' > data/web/inc/app_info.inc.php
  455. echo ' $MAILCOW_GIT_VERSION="'$mailcow_git_version'";' >> data/web/inc/app_info.inc.php
  456. echo ' $MAILCOW_LAST_GIT_VERSION="";' >> data/web/inc/app_info.inc.php
  457. echo ' $MAILCOW_GIT_OWNER="mailcow";' >> data/web/inc/app_info.inc.php
  458. echo ' $MAILCOW_GIT_REPO="mailcow-dockerized";' >> data/web/inc/app_info.inc.php
  459. echo ' $MAILCOW_GIT_URL="https://github.com/mailcow/mailcow-dockerized";' >> data/web/inc/app_info.inc.php
  460. echo ' $MAILCOW_GIT_COMMIT="'$mailcow_git_commit'";' >> data/web/inc/app_info.inc.php
  461. echo ' $MAILCOW_GIT_COMMIT_DATE="'$mailcow_git_commit_date'";' >> data/web/inc/app_info.inc.php
  462. echo ' $MAILCOW_BRANCH="'$BRANCH'";' >> data/web/inc/app_info.inc.php
  463. echo ' $MAILCOW_UPDATEDAT='$(date +%s)';' >> data/web/inc/app_info.inc.php
  464. echo '?>' >> data/web/inc/app_info.inc.php
  465. else
  466. echo '<?php' > data/web/inc/app_info.inc.php
  467. echo ' $MAILCOW_GIT_VERSION="'$mailcow_git_version'";' >> data/web/inc/app_info.inc.php
  468. echo ' $MAILCOW_LAST_GIT_VERSION="";' >> data/web/inc/app_info.inc.php
  469. echo ' $MAILCOW_GIT_OWNER="mailcow";' >> data/web/inc/app_info.inc.php
  470. echo ' $MAILCOW_GIT_REPO="mailcow-dockerized";' >> data/web/inc/app_info.inc.php
  471. echo ' $MAILCOW_GIT_URL="https://github.com/mailcow/mailcow-dockerized";' >> data/web/inc/app_info.inc.php
  472. echo ' $MAILCOW_GIT_COMMIT="";' >> data/web/inc/app_info.inc.php
  473. echo ' $MAILCOW_GIT_COMMIT_DATE="";' >> data/web/inc/app_info.inc.php
  474. echo ' $MAILCOW_BRANCH="'$BRANCH'";' >> data/web/inc/app_info.inc.php
  475. echo ' $MAILCOW_UPDATEDAT='$(date +%s)';' >> data/web/inc/app_info.inc.php
  476. echo '?>' >> data/web/inc/app_info.inc.php
  477. echo -e "\e[33mCannot determine current git repository version...\e[0m"
  478. fi
  479. if [[ ${SKIP_START} == "y" ]]; then
  480. echo -e "\e[33mNot starting mailcow, please run \"$COMPOSE_COMMAND up -d --remove-orphans\" to start mailcow.\e[0m"
  481. else
  482. echo -e "\e[32mStarting mailcow...\e[0m"
  483. sleep 2
  484. $COMPOSE_COMMAND up -d --remove-orphans
  485. fi
  486. echo -e "\e[32mCollecting garbage...\e[0m"
  487. docker_garbage
  488. # Run post-update-hook
  489. if [ -f "${SCRIPT_DIR}/post_update_hook.sh" ]; then
  490. bash "${SCRIPT_DIR}/post_update_hook.sh"
  491. fi
  492. # echo "In case you encounter any problem, hard-reset to a state before updating mailcow:"
  493. # echo
  494. # git reflog --color=always | grep "Before update on "
  495. # echo
  496. # echo "Use \"git reset --hard hash-on-the-left\" and run $COMPOSE_COMMAND up -d afterwards."