backup_and_restore.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #!/usr/bin/env bash
  2. DEBIAN_DOCKER_IMAGE="mailcow/backup:latest"
  3. if [[ ! -z ${MAILCOW_BACKUP_LOCATION} ]]; then
  4. BACKUP_LOCATION="${MAILCOW_BACKUP_LOCATION}"
  5. fi
  6. if [[ ! ${1} =~ (backup|restore) ]]; then
  7. echo "First parameter needs to be 'backup' or 'restore'"
  8. exit 1
  9. fi
  10. if [[ ${1} == "backup" && ! ${2} =~ (crypt|vmail|redis|rspamd|postfix|mysql|all|--delete-days) ]]; then
  11. echo "Second parameter needs to be 'vmail', 'crypt', 'redis', 'rspamd', 'postfix', 'mysql', 'all' or '--delete-days'"
  12. exit 1
  13. fi
  14. if [[ -z ${BACKUP_LOCATION} ]]; then
  15. while [[ -z ${BACKUP_LOCATION} ]]; do
  16. read -ep "Backup location (absolute path, starting with /): " BACKUP_LOCATION
  17. done
  18. fi
  19. if [[ ! ${BACKUP_LOCATION} =~ ^/ ]]; then
  20. echo "Backup directory needs to be given as absolute path (starting with /)."
  21. exit 1
  22. fi
  23. if [[ -f ${BACKUP_LOCATION} ]]; then
  24. echo "${BACKUP_LOCATION} is a file!"
  25. exit 1
  26. fi
  27. if [[ ! -d ${BACKUP_LOCATION} ]]; then
  28. echo "${BACKUP_LOCATION} is not a directory"
  29. read -p "Create it now? [y|N] " CREATE_BACKUP_LOCATION
  30. if [[ ! ${CREATE_BACKUP_LOCATION,,} =~ ^(yes|y)$ ]]; then
  31. exit 1
  32. else
  33. mkdir -p ${BACKUP_LOCATION}
  34. chmod 755 ${BACKUP_LOCATION}
  35. fi
  36. else
  37. if [[ ${1} == "backup" ]] && [[ -z $(echo $(stat -Lc %a ${BACKUP_LOCATION}) | grep -oE '[0-9][0-9][5-7]') ]]; then
  38. echo "${BACKUP_LOCATION} is not write-able for others, that's required for a backup."
  39. exit 1
  40. fi
  41. fi
  42. BACKUP_LOCATION=$(echo ${BACKUP_LOCATION} | sed 's#/$##')
  43. SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  44. COMPOSE_FILE=${SCRIPT_DIR}/../docker-compose.yml
  45. ENV_FILE=${SCRIPT_DIR}/../.env
  46. THREADS=$(echo ${THREADS:-1})
  47. if ! [[ "${THREADS}" =~ ^[1-9]+$ ]] ; then
  48. echo "Thread input is not a number!"
  49. exit 1
  50. elif [[ "${THREADS}" =~ ^[1-9]+$ ]] ; then
  51. echo "Using ${THREADS} Thread(s) for this run."
  52. echo "Notice: You can set the Thread count with the THREADS Variable before you run this script."
  53. fi
  54. if [ ! -f ${COMPOSE_FILE} ]; then
  55. echo "Compose file not found"
  56. exit 1
  57. fi
  58. if [ ! -f ${ENV_FILE} ]; then
  59. echo "Environment file not found"
  60. exit 1
  61. fi
  62. echo "Using ${BACKUP_LOCATION} as backup/restore location."
  63. echo
  64. source ${SCRIPT_DIR}/../mailcow.conf
  65. if [[ -z ${COMPOSE_PROJECT_NAME} ]]; then
  66. echo "Could not determine compose project name"
  67. exit 1
  68. else
  69. echo "Found project name ${COMPOSE_PROJECT_NAME}"
  70. CMPS_PRJ=$(echo ${COMPOSE_PROJECT_NAME} | tr -cd "[0-9A-Za-z-_]")
  71. fi
  72. if grep --help 2>&1 | head -n 1 | grep -q -i "busybox"; then
  73. >&2 echo -e "\e[31mBusyBox grep detected on local system, please install GNU grep\e[0m"
  74. exit 1
  75. fi
  76. function backup() {
  77. DATE=$(date +"%Y-%m-%d-%H-%M-%S")
  78. mkdir -p "${BACKUP_LOCATION}/mailcow-${DATE}"
  79. chmod 755 "${BACKUP_LOCATION}/mailcow-${DATE}"
  80. cp "${SCRIPT_DIR}/../mailcow.conf" "${BACKUP_LOCATION}/mailcow-${DATE}"
  81. for bin in docker; do
  82. if [[ -z $(which ${bin}) ]]; then
  83. >&2 echo -e "\e[31mCannot find ${bin} in local PATH, exiting...\e[0m"
  84. exit 1
  85. fi
  86. done
  87. while (( "$#" )); do
  88. case "$1" in
  89. vmail|all)
  90. docker run --name mailcow-backup --rm \
  91. -v ${BACKUP_LOCATION}/mailcow-${DATE}:/backup:z \
  92. -v $(docker volume ls -qf name=^${CMPS_PRJ}_vmail-vol-1$):/vmail:ro,z \
  93. ${DEBIAN_DOCKER_IMAGE} /bin/tar --warning='no-file-ignored' --use-compress-program="pigz --rsyncable -p ${THREADS}" -Pcvpf /backup/backup_vmail.tar.gz /vmail
  94. ;;&
  95. crypt|all)
  96. docker run --name mailcow-backup --rm \
  97. -v ${BACKUP_LOCATION}/mailcow-${DATE}:/backup:z \
  98. -v $(docker volume ls -qf name=^${CMPS_PRJ}_crypt-vol-1$):/crypt:ro,z \
  99. ${DEBIAN_DOCKER_IMAGE} /bin/tar --warning='no-file-ignored' --use-compress-program="pigz --rsyncable -p ${THREADS}" -Pcvpf /backup/backup_crypt.tar.gz /crypt
  100. ;;&
  101. redis|all)
  102. docker exec $(docker ps -qf name=redis-mailcow) redis-cli save
  103. docker run --name mailcow-backup --rm \
  104. -v ${BACKUP_LOCATION}/mailcow-${DATE}:/backup:z \
  105. -v $(docker volume ls -qf name=^${CMPS_PRJ}_redis-vol-1$):/redis:ro,z \
  106. ${DEBIAN_DOCKER_IMAGE} /bin/tar --warning='no-file-ignored' --use-compress-program="pigz --rsyncable -p ${THREADS}" -Pcvpf /backup/backup_redis.tar.gz /redis
  107. ;;&
  108. rspamd|all)
  109. docker run --name mailcow-backup --rm \
  110. -v ${BACKUP_LOCATION}/mailcow-${DATE}:/backup:z \
  111. -v $(docker volume ls -qf name=^${CMPS_PRJ}_rspamd-vol-1$):/rspamd:ro,z \
  112. ${DEBIAN_DOCKER_IMAGE} /bin/tar --warning='no-file-ignored' --use-compress-program="pigz --rsyncable -p ${THREADS}" -Pcvpf /backup/backup_rspamd.tar.gz /rspamd
  113. ;;&
  114. postfix|all)
  115. docker run --name mailcow-backup --rm \
  116. -v ${BACKUP_LOCATION}/mailcow-${DATE}:/backup:z \
  117. -v $(docker volume ls -qf name=^${CMPS_PRJ}_postfix-vol-1$):/postfix:ro,z \
  118. ${DEBIAN_DOCKER_IMAGE} /bin/tar --warning='no-file-ignored' --use-compress-program="pigz --rsyncable -p ${THREADS}" -Pcvpf /backup/backup_postfix.tar.gz /postfix
  119. ;;&
  120. mysql|all)
  121. SQLIMAGE=$(grep -iEo '(mysql|mariadb)\:.+' ${COMPOSE_FILE})
  122. if [[ -z "${SQLIMAGE}" ]]; then
  123. echo "Could not determine SQL image version, skipping backup..."
  124. shift
  125. continue
  126. else
  127. echo "Using SQL image ${SQLIMAGE}, starting..."
  128. docker run --name mailcow-backup --rm \
  129. --network $(docker network ls -qf name=^${CMPS_PRJ}_mailcow-network$) \
  130. -v $(docker volume ls -qf name=^${CMPS_PRJ}_mysql-vol-1$):/var/lib/mysql/:ro,z \
  131. -t --entrypoint= \
  132. --sysctl net.ipv6.conf.all.disable_ipv6=1 \
  133. -v ${BACKUP_LOCATION}/mailcow-${DATE}:/backup:z \
  134. ${SQLIMAGE} /bin/sh -c "mariabackup --host mysql --user root --password ${DBROOT} --backup --rsync --target-dir=/backup_mariadb ; \
  135. mariabackup --prepare --target-dir=/backup_mariadb ; \
  136. chown -R 999:999 /backup_mariadb ; \
  137. /bin/tar --warning='no-file-ignored' --use-compress-program='gzip --rsyncable' -Pcvpf /backup/backup_mariadb.tar.gz /backup_mariadb ;"
  138. fi
  139. ;;&
  140. --delete-days)
  141. shift
  142. if [[ "${1}" =~ ^[0-9]+$ ]]; then
  143. find ${BACKUP_LOCATION}/mailcow-* -maxdepth 0 -mmin +$((${1}*60*24)) -exec rm -rvf {} \;
  144. else
  145. echo "Parameter of --delete-days is not a number."
  146. fi
  147. ;;
  148. esac
  149. shift
  150. done
  151. }
  152. function restore() {
  153. for bin in docker; do
  154. if [[ -z $(which ${bin}) ]]; then
  155. >&2 echo -e "\e[31mCannot find ${bin} in local PATH, exiting...\e[0m"
  156. exit 1
  157. fi
  158. done
  159. if [ "${DOCKER_COMPOSE_VERSION}" == "native" ]; then
  160. COMPOSE_COMMAND="docker compose"
  161. elif [ "${DOCKER_COMPOSE_VERSION}" == "standalone" ]; then
  162. COMPOSE_COMMAND="docker-compose"
  163. else
  164. echo -e "\e[31mCan not read DOCKER_COMPOSE_VERSION variable from mailcow.conf! Is your mailcow up to date? Exiting...\e[0m"
  165. exit 1
  166. fi
  167. echo
  168. echo "Stopping watchdog-mailcow..."
  169. docker stop $(docker ps -qf name=watchdog-mailcow)
  170. echo
  171. RESTORE_LOCATION="${1}"
  172. shift
  173. while (( "$#" )); do
  174. case "$1" in
  175. vmail)
  176. docker stop $(docker ps -qf name=dovecot-mailcow)
  177. docker run -it --name mailcow-backup --rm \
  178. -v ${RESTORE_LOCATION}:/backup:z \
  179. -v $(docker volume ls -qf name=^${CMPS_PRJ}_vmail-vol-1$):/vmail:z \
  180. ${DEBIAN_DOCKER_IMAGE} /bin/tar --use-compress-program="pigz -d -p ${THREADS}" -Pxvf /backup/backup_vmail.tar.gz
  181. docker start $(docker ps -aqf name=dovecot-mailcow)
  182. echo
  183. echo "In most cases it is not required to run a full resync, you can run the command printed below at any time after testing wether the restore process broke a mailbox:"
  184. echo
  185. echo "docker exec $(docker ps -qf name=dovecot-mailcow) doveadm force-resync -A '*'"
  186. echo
  187. read -p "Force a resync now? [y|N] " FORCE_RESYNC
  188. if [[ ${FORCE_RESYNC,,} =~ ^(yes|y)$ ]]; then
  189. docker exec $(docker ps -qf name=dovecot-mailcow) doveadm force-resync -A '*'
  190. else
  191. echo "OK, skipped."
  192. fi
  193. ;;
  194. redis)
  195. docker stop $(docker ps -qf name=redis-mailcow)
  196. docker run -it --name mailcow-backup --rm \
  197. -v ${RESTORE_LOCATION}:/backup:z \
  198. -v $(docker volume ls -qf name=^${CMPS_PRJ}_redis-vol-1$):/redis:z \
  199. ${DEBIAN_DOCKER_IMAGE} /bin/tar --use-compress-program="pigz -d -p ${THREADS}" -Pxvf /backup/backup_redis.tar.gz
  200. docker start $(docker ps -aqf name=redis-mailcow)
  201. ;;
  202. crypt)
  203. docker stop $(docker ps -qf name=dovecot-mailcow)
  204. docker run -it --name mailcow-backup --rm \
  205. -v ${RESTORE_LOCATION}:/backup:z \
  206. -v $(docker volume ls -qf name=^${CMPS_PRJ}_crypt-vol-1$):/crypt:z \
  207. ${DEBIAN_DOCKER_IMAGE} /bin/tar --use-compress-program="pigz -d -p ${THREADS}" -Pxvf /backup/backup_crypt.tar.gz
  208. docker start $(docker ps -aqf name=dovecot-mailcow)
  209. ;;
  210. rspamd)
  211. docker stop $(docker ps -qf name=rspamd-mailcow)
  212. docker run -it --name mailcow-backup --rm \
  213. -v ${RESTORE_LOCATION}:/backup:z \
  214. -v $(docker volume ls -qf name=^${CMPS_PRJ}_rspamd-vol-1$):/rspamd:z \
  215. ${DEBIAN_DOCKER_IMAGE} /bin/tar --use-compress-program="pigz -d -p ${THREADS}" -Pxvf /backup/backup_rspamd.tar.gz
  216. docker start $(docker ps -aqf name=rspamd-mailcow)
  217. ;;
  218. postfix)
  219. docker stop $(docker ps -qf name=postfix-mailcow)
  220. docker run -it --name mailcow-backup --rm \
  221. -v ${RESTORE_LOCATION}:/backup:z \
  222. -v $(docker volume ls -qf name=^${CMPS_PRJ}_postfix-vol-1$):/postfix:z \
  223. ${DEBIAN_DOCKER_IMAGE} /bin/tar --use-compress-program="pigz -d -p ${THREADS}" -Pxvf /backup/backup_postfix.tar.gz
  224. docker start $(docker ps -aqf name=postfix-mailcow)
  225. ;;
  226. mysql|mariadb)
  227. SQLIMAGE=$(grep -iEo '(mysql|mariadb)\:.+' ${COMPOSE_FILE})
  228. if [[ -z "${SQLIMAGE}" ]]; then
  229. echo "Could not determine SQL image version, skipping restore..."
  230. shift
  231. continue
  232. elif [ ! -f "${RESTORE_LOCATION}/mailcow.conf" ]; then
  233. echo "Could not find the corresponding mailcow.conf in ${RESTORE_LOCATION}, skipping restore."
  234. echo "If you lost that file, copy the last working mailcow.conf file to ${RESTORE_LOCATION} and restart the restore process."
  235. shift
  236. continue
  237. else
  238. read -p "mailcow will be stopped and the currently active mailcow.conf will be modified to use the DB parameters found in ${RESTORE_LOCATION}/mailcow.conf - do you want to proceed? [Y|n] " MYSQL_STOP_MAILCOW
  239. if [[ ${MYSQL_STOP_MAILCOW,,} =~ ^(no|n|N)$ ]]; then
  240. echo "OK, skipped."
  241. shift
  242. continue
  243. else
  244. echo "Stopping mailcow..."
  245. ${COMPOSE_COMMAND} -f ${COMPOSE_FILE} --env-file ${ENV_FILE} down
  246. fi
  247. #docker stop $(docker ps -qf name=mysql-mailcow)
  248. if [[ -d "${RESTORE_LOCATION}/mysql" ]]; then
  249. docker run --name mailcow-backup --rm \
  250. -v $(docker volume ls -qf name=^${CMPS_PRJ}_mysql-vol-1$):/var/lib/mysql/:rw,z \
  251. --entrypoint= \
  252. -v ${RESTORE_LOCATION}/mysql:/backup:z \
  253. ${SQLIMAGE} /bin/bash -c "shopt -s dotglob ; /bin/rm -rf /var/lib/mysql/* ; rsync -avh --usermap=root:mysql --groupmap=root:mysql /backup/ /var/lib/mysql/"
  254. elif [[ -f "${RESTORE_LOCATION}/backup_mysql.gz" ]]; then
  255. docker run \
  256. -it --name mailcow-backup --rm \
  257. -v $(docker volume ls -qf name=^${CMPS_PRJ}_mysql-vol-1$):/var/lib/mysql/:z \
  258. --entrypoint= \
  259. -u mysql \
  260. -v ${RESTORE_LOCATION}:/backup:z \
  261. ${SQLIMAGE} /bin/sh -c "mysqld --skip-grant-tables & \
  262. until mysqladmin ping; do sleep 3; done && \
  263. echo Restoring... && \
  264. gunzip < backup/backup_mysql.gz | mysql -uroot && \
  265. mysql -uroot -e SHUTDOWN;"
  266. elif [[ -f "${RESTORE_LOCATION}/backup_mariadb.tar.gz" ]]; then
  267. docker run --name mailcow-backup --rm \
  268. -v $(docker volume ls -qf name=^${CMPS_PRJ}_mysql-vol-1$):/backup_mariadb/:rw,z \
  269. --entrypoint= \
  270. -v ${RESTORE_LOCATION}:/backup:z \
  271. ${SQLIMAGE} /bin/bash -c "shopt -s dotglob ; \
  272. /bin/rm -rf /backup_mariadb/* ; \
  273. /bin/tar -Pxvzf /backup/backup_mariadb.tar.gz"
  274. fi
  275. echo "Modifying mailcow.conf..."
  276. source ${RESTORE_LOCATION}/mailcow.conf
  277. sed -i --follow-symlinks "/DBNAME/c\DBNAME=${DBNAME}" ${SCRIPT_DIR}/../mailcow.conf
  278. sed -i --follow-symlinks "/DBUSER/c\DBUSER=${DBUSER}" ${SCRIPT_DIR}/../mailcow.conf
  279. sed -i --follow-symlinks "/DBPASS/c\DBPASS=${DBPASS}" ${SCRIPT_DIR}/../mailcow.conf
  280. sed -i --follow-symlinks "/DBROOT/c\DBROOT=${DBROOT}" ${SCRIPT_DIR}/../mailcow.conf
  281. source ${SCRIPT_DIR}/../mailcow.conf
  282. echo "Starting mailcow..."
  283. ${COMPOSE_COMMAND} -f ${COMPOSE_FILE} --env-file ${ENV_FILE} up -d
  284. #docker start $(docker ps -aqf name=mysql-mailcow)
  285. fi
  286. ;;
  287. esac
  288. shift
  289. done
  290. echo
  291. echo "Starting watchdog-mailcow..."
  292. docker start $(docker ps -aqf name=watchdog-mailcow)
  293. }
  294. if [[ ${1} == "backup" ]]; then
  295. backup ${@,,}
  296. elif [[ ${1} == "restore" ]]; then
  297. i=1
  298. declare -A FOLDER_SELECTION
  299. if [[ $(find ${BACKUP_LOCATION}/mailcow-* -maxdepth 1 -type d 2> /dev/null| wc -l) -lt 1 ]]; then
  300. echo "Selected backup location has no subfolders"
  301. exit 1
  302. fi
  303. for folder in $(ls -d ${BACKUP_LOCATION}/mailcow-*/); do
  304. echo "[ ${i} ] - ${folder}"
  305. FOLDER_SELECTION[${i}]="${folder}"
  306. ((i++))
  307. done
  308. echo
  309. input_sel=0
  310. while [[ ${input_sel} -lt 1 || ${input_sel} -gt ${i} ]]; do
  311. read -p "Select a restore point: " input_sel
  312. done
  313. i=1
  314. echo
  315. declare -A FILE_SELECTION
  316. RESTORE_POINT="${FOLDER_SELECTION[${input_sel}]}"
  317. if [[ -z $(find "${FOLDER_SELECTION[${input_sel}]}" -maxdepth 1 \( -type d -o -type f \) -regex ".*\(redis\|rspamd\|mariadb\|mysql\|crypt\|vmail\|postfix\).*") ]]; then
  318. echo "No datasets found"
  319. exit 1
  320. fi
  321. echo "[ 0 ] - all"
  322. # find all files in folder with *.gz extension, print their base names, remove backup_, remove .tar (if present), remove .gz
  323. FILE_SELECTION[0]=$(find "${FOLDER_SELECTION[${input_sel}]}" -maxdepth 1 \( -type d -o -type f \) \( -name '*.gz' -o -name 'mysql' \) -printf '%f\n' | sed 's/backup_*//' | sed 's/\.[^.]*$//' | sed 's/\.[^.]*$//')
  324. for file in $(ls -f "${FOLDER_SELECTION[${input_sel}]}"); do
  325. if [[ ${file} =~ vmail ]]; then
  326. echo "[ ${i} ] - Mail directory (/var/vmail)"
  327. FILE_SELECTION[${i}]="vmail"
  328. ((i++))
  329. elif [[ ${file} =~ crypt ]]; then
  330. echo "[ ${i} ] - Crypt data"
  331. FILE_SELECTION[${i}]="crypt"
  332. ((i++))
  333. elif [[ ${file} =~ redis ]]; then
  334. echo "[ ${i} ] - Redis DB"
  335. FILE_SELECTION[${i}]="redis"
  336. ((i++))
  337. elif [[ ${file} =~ rspamd ]]; then
  338. echo "[ ${i} ] - Rspamd data"
  339. FILE_SELECTION[${i}]="rspamd"
  340. ((i++))
  341. elif [[ ${file} =~ postfix ]]; then
  342. echo "[ ${i} ] - Postfix data"
  343. FILE_SELECTION[${i}]="postfix"
  344. ((i++))
  345. elif [[ ${file} =~ mysql ]] || [[ ${file} =~ mariadb ]]; then
  346. echo "[ ${i} ] - SQL DB"
  347. FILE_SELECTION[${i}]="mysql"
  348. ((i++))
  349. fi
  350. done
  351. echo
  352. input_sel=-1
  353. while [[ ${input_sel} -lt 0 || ${input_sel} -gt ${i} ]]; do
  354. read -p "Select a dataset to restore: " input_sel
  355. done
  356. echo "Restoring ${FILE_SELECTION[${input_sel}]} from ${RESTORE_POINT}..."
  357. restore "${RESTORE_POINT}" ${FILE_SELECTION[${input_sel}]}
  358. fi