You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

536 lines
14 KiB

  1. #!/bin/bash
  2. . /etc/shlib
  3. include common
  4. include parse
  5. include cmdline
  6. include config
  7. include cache
  8. include fn
  9. [[ "${BASH_SOURCE[0]}" != "${0}" ]] && SOURCED=true
  10. version=0.1
  11. desc='Install backup'
  12. help=""
  13. docker:running-container-projects() {
  14. :cache: scope=session
  15. docker ps --format '{{.Label "com.docker.compose.project"}}' | sort | uniq
  16. }
  17. decorator._mangle_fn docker:running-container-projects
  18. ssh:mk-private-key() {
  19. local host="$1" service_name="$2"
  20. (
  21. settmpdir VPS_TMPDIR
  22. ssh-keygen -t rsa -N "" -f "$VPS_TMPDIR/rsync_rsa" -C "$service_name@$host" >/dev/null
  23. cat "$VPS_TMPDIR/rsync_rsa"
  24. )
  25. }
  26. mailcow:has-images-running() {
  27. local images
  28. images=$(docker ps --format '{{.Image}}' | sort | uniq)
  29. [[ $'\n'"$images" == *$'\n'"mailcow/"* ]]
  30. }
  31. mailcow:has-container-project-mentionning-mailcow() {
  32. local projects
  33. projects=$(docker:running-container-projects) || return 1
  34. [[ $'\n'"$projects"$'\n' == *mailcow* ]]
  35. }
  36. mailcow:has-running-containers() {
  37. mailcow:has-images-running ||
  38. mailcow:has-container-project-mentionning-mailcow
  39. }
  40. mailcow:get-root() {
  41. :cache: scope=session
  42. local dir
  43. for dir in {/opt{,/apps},/root}/mailcow-dockerized; do
  44. [ -d "$dir" ] || continue
  45. [ -r "$dir/mailcow.conf" ] || continue
  46. echo "$dir"
  47. return 0
  48. done
  49. return 1
  50. }
  51. decorator._mangle_fn mailcow:get-root
  52. compose:get-compose-yml() {
  53. :cache: scope=session
  54. local path
  55. [ -e "/etc/compose/local.conf" ] && . "/etc/compose/local.conf"
  56. path=${DEFAULT_COMPOSE_FILE:-/etc/compose/compose.yml}
  57. [ -e "$path" ] || return 1
  58. echo "$path"
  59. }
  60. decorator._mangle_fn compose:get-compose-yml
  61. compose:has-container-project-myc() {
  62. local projects
  63. projects=$(docker:running-container-projects) || return 1
  64. [[ $'\n'"$projects"$'\n' == *$'\n'"myc"$'\n'* ]]
  65. }
  66. type:is-mailcow() {
  67. mailcow:get-root >/dev/null ||
  68. mailcow:has-running-containers
  69. }
  70. type:is-compose() {
  71. compose:get-compose-yml >/dev/null &&
  72. compose:has-container-project-myc
  73. }
  74. vps:get-type() {
  75. local fn
  76. for fn in $(declare -F | cut -f 3 -d " " | egrep "^type:is-"); do
  77. "$fn" && {
  78. echo "${fn#type:is-}"
  79. return 0
  80. }
  81. done
  82. return 1
  83. }
  84. mirror-dir:sources() {
  85. :cache: scope=session
  86. if ! shyaml get-values default.sources < /etc/mirror-dir/config.yml; then
  87. err "Couldn't query 'default.sources' in '/etc/mirror-dir/config.yml'."
  88. return 1
  89. fi
  90. }
  91. decorator._mangle_fn mirror-dir:sources
  92. mirror-dir:check-add() {
  93. local elt="$1" sources
  94. sources=$(mirror-dir:sources) || return 1
  95. if [[ $'\n'"$sources"$'\n' == *$'\n'"$elt"$'\n'* ]]; then
  96. info "Volume $elt already in sources"
  97. else
  98. Elt "Adding directory $elt"
  99. sed -i "/sources:/a\ - \"${elt}\"" \
  100. /etc/mirror-dir/config.yml
  101. Feedback || return 1
  102. fi
  103. }
  104. mirror-dir:check-add-vol() {
  105. local elt="$1"
  106. mirror-dir:check-add "/var/lib/docker/volumes/*_${elt}-*/_data"
  107. }
  108. ## The first colon is to prevent auto-export of function from shlib
  109. : ; bash-bug-5() { { cat; } < <(e) >/dev/null; ! cat "$1"; } && bash-bug-5 <(e) 2>/dev/null &&
  110. export BASH_BUG_5=1 && unset -f bash_bug_5
  111. wrap() {
  112. local label="$1" code="$2"
  113. shift 2
  114. export VERBOSE=1
  115. interpreter=/bin/bash
  116. if [ -n "$BASH_BUG_5" ]; then
  117. (
  118. settmpdir tmpdir
  119. fname=${label##*/}
  120. e "$code" > "$tmpdir/$fname" &&
  121. chmod +x "$tmpdir/$fname" &&
  122. Wrap -vsd "$label" -- "$interpreter" "$tmpdir/$fname" "$@"
  123. )
  124. else
  125. Wrap -vsd "$label" -- "$interpreter" <(e "$code") "$@"
  126. fi
  127. }
  128. mailcow:install-backup() {
  129. local BACKUP_SERVER="$1" mailcow_root DOMAIN
  130. ## find installation
  131. mailcow_root=$(mailcow:get-root) || {
  132. err "Couldn't find a valid mailcow root directory."
  133. return 1
  134. }
  135. ## check ok
  136. DOMAIN=$(cat "$mailcow_root/.env" | grep ^MAILCOW_HOSTNAME= | cut -f 2 -d =) || {
  137. err "Couldn't find MAILCOW_HOSTNAME in file \"$mailcow_root/.env\"."
  138. return 1
  139. }
  140. MYSQL_ROOT_PASSWORD=$(cat "$mailcow_root/.env" | grep ^DBROOT= | cut -f 2 -d =) || {
  141. err "Couldn't find DBROOT in file \"$mailcow_root/.env\"."
  142. return 1
  143. }
  144. MYSQL_CONTAINER=${MYSQL_CONTAINER:-mailcowdockerized_mysql-mailcow_1}
  145. container_id=$(docker ps -f name="$MYSQL_CONTAINER" --format "{{.ID}}")
  146. if [ -z "$container_id" ]; then
  147. err "Couldn't find docker container named '$MYSQL_CONTAINER'."
  148. return 1
  149. fi
  150. export MYSQL_ROOT_PASSWORD
  151. export MYSQL_CONTAINER
  152. export BACKUP_SERVER
  153. export DOMAIN
  154. wrap "Install rsync-backup on host" "
  155. cd /srv/charm-store/rsync-backup
  156. bash ./hooks/install.d/60-install.sh
  157. " || return 1
  158. wrap "Mysql dump install" "
  159. cd /srv/charm-store/mariadb
  160. bash ./hooks/install.d/60-backup.sh
  161. " || return 1
  162. ## Using https://github.com/mailcow/mailcow-dockerized/blob/master/helper-scripts/backup_and_restore.sh
  163. for elt in "vmail{,-attachments-vol}" crypt redis rspamd postfix; do
  164. mirror-dir:check-add-vol "$elt" || return 1
  165. done
  166. mirror-dir:check-add "$mailcow_root" || return 1
  167. mirror-dir:check-add "/var/backups/mysql" || return 1
  168. mirror-dir:check-add "/etc" || return 1
  169. dest="$BACKUP_SERVER"
  170. dest="${dest%/*}"
  171. ssh_options=()
  172. if [[ "$dest" == *":"* ]]; then
  173. port="${dest##*:}"
  174. dest="${dest%%:*}"
  175. ssh_options=(-p "$port")
  176. else
  177. port=""
  178. dest="${dest%%:*}"
  179. fi
  180. info "You can run this following from an host having admin access to $dest:"
  181. echo "ssh ${ssh_options[@]} rsync@$dest ssh-key add \"$(cat /var/lib/rsync/.ssh/id_rsa.pub)\""
  182. }
  183. compose:install-backup() {
  184. local BACKUP_SERVER="$1" service_name="$2" compose_file="$3" force="$4"
  185. ## XXXvlab: far from perfect as it mimics and depends internal
  186. ## logic of current default way to get a domain in compose-core
  187. host=$(hostname)
  188. if ! egrep "^$host:" "$compose_file" >/dev/null &&
  189. ! egrep "^\s+domain:\s+$host\s*$" "$compose_file" >/dev/null; then
  190. err "Can't find domain '$host' in compose file '$compose_file'."
  191. return 1
  192. fi
  193. ip=$(getent ahosts "$host" | egrep "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s+" |
  194. head -n 1 | cut -f 1 -d " ") || return 1
  195. my_ip=$(curl -s myip.kal.fr)
  196. if [ "$ip" != "$my_ip" ]; then
  197. if [ -n "$force" ]; then
  198. warn "IP of '$host' ($ip) doesn't match mine ($my_ip). Ignoring due to ``-f`` option."
  199. else
  200. err "IP of '$host' ($ip) doesn't match mine ($my_ip). Use ``-f`` to force."
  201. return 1
  202. fi
  203. fi
  204. if [ -e "/root/.ssh/rsync_rsa" ]; then
  205. warn "deleting private key in /root/.ssh/rsync_rsa, has we are not using it anymore."
  206. rm -fv /root/.ssh/rsync_rsa
  207. fi
  208. if [ -e "/root/.ssh/rsync_rsa.pub" ]; then
  209. warn "deleting public key in /root/.ssh/rsync_rsa.pub, has we are not using it anymore."
  210. rm -fv /root/.ssh/rsync_rsa.pub
  211. fi
  212. if service_cfg=$(cat "$compose_file" |
  213. shyaml get-value -y "$service_name" 2>/dev/null); then
  214. info "Entry for service ${DARKYELLOW}$service_name${NORMAL}" \
  215. "is already present in '$compose_file'."
  216. cfg=$(e "$service_cfg" | shyaml get-value -y options) || {
  217. err "No ${WHITE}options${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  218. "entry in '$compose_file'."
  219. return 1
  220. }
  221. private_key=$(e "$cfg" | shyaml get-value private-key)
  222. target=$(e "$cfg" | shyaml get-value target)
  223. if [ "$target" != "$BACKUP_SERVER" ]; then
  224. err "Existing backup target '$target' is different" \
  225. "from specified '$BACKUP_SERVER'"
  226. return 1
  227. fi
  228. else
  229. private_key=$(ssh:mk-private-key "$host" "$service_name")
  230. cat <<EOF >> "$compose_file"
  231. $service_name:
  232. options:
  233. ident: $host
  234. target: $BACKUP_SERVER
  235. private-key: |
  236. $(e "$private_key" | sed -r 's/^/ /g')
  237. EOF
  238. fi
  239. dest="$BACKUP_SERVER"
  240. dest="${dest%/*}"
  241. ssh_options=()
  242. ssh_options=()
  243. if [[ "$dest" == *":"* ]]; then
  244. port="${dest##*:}"
  245. dest="${dest%%:*}"
  246. ssh_options=(-p "$port")
  247. else
  248. port=""
  249. dest="${dest%%:*}"
  250. fi
  251. info "You can run this following from an host having admin access to $dest:"
  252. public_key=$(ssh-keygen -y -f <(e "$private_key"$'\n'))
  253. echo "ssh ${ssh_options[@]} rsync@$dest ssh-key add '$public_key ${service_name}@$host'"
  254. }
  255. [ "$SOURCED" ] && return 0
  256. ##
  257. ## Command line processing
  258. ##
  259. cmdline.spec.gnu
  260. cmdline.spec.reporting
  261. cmdline.spec.gnu install
  262. cmdline.spec::cmd:install:run() {
  263. :
  264. }
  265. cmdline.spec.gnu get-type
  266. cmdline.spec::cmd:get-type:run() {
  267. vps:get-type
  268. }
  269. cmdline.spec:install:cmd:backup:run() {
  270. : :posarg: BACKUP_SERVER 'Target backup server'
  271. local vps_type
  272. vps_type=$(vps:get-type) || {
  273. err "Failed to get type of installation."
  274. return 1
  275. }
  276. if ! fn.exists "${vps_type}:install-backup"; then
  277. err "type '${vps_type}' has no backup installation implemented yet."
  278. return 1
  279. fi
  280. "cmdline.spec:install:cmd:$vps_type-backup:run" "$BACKUP_SERVER"
  281. }
  282. DEFAULT_BACKUP_SERVICE_NAME=rsync-backup
  283. cmdline.spec:install:cmd:compose-backup:run() {
  284. : :posarg: BACKUP_SERVER 'Target backup server'
  285. : :optval: --service-name,-s "YAML service name in compose
  286. file to check for existence of key.
  287. Defaults to '$DEFAULT_BACKUP_SERVICE_NAME'"
  288. : :optval: --compose-file,-f "Compose file location. Defaults to
  289. the value of '\$DEFAULT_COMPOSE_FILE'"
  290. : :optval: --force,-F "Compose file location. Defaults to
  291. the value of '\$DEFAULT_COMPOSE_FILE'"
  292. local service_name compose_file
  293. [ -e "/etc/compose/local.conf" ] && source /etc/compose/local.conf
  294. compose_file=${opt_compose_file:-$DEFAULT_COMPOSE_FILE}
  295. service_name=${opt_service_name:-$DEFAULT_BACKUP_SERVICE_NAME}
  296. if ! [ -e "$compose_file" ]; then
  297. err "Compose file not found in '$compose_file'."
  298. return 1
  299. fi
  300. compose:install-backup "$BACKUP_SERVER" "$service_name" "$compose_file" "$opt_force"
  301. }
  302. cmdline.spec:install:cmd:mailcow-backup:run() {
  303. : :posarg: BACKUP_SERVER 'Target backup server'
  304. "mailcow:install-backup" "$BACKUP_SERVER"
  305. }
  306. cmdline.spec.gnu backup
  307. cmdline.spec::cmd:backup:run() {
  308. local vps_type
  309. vps_type=$(vps:get-type) || {
  310. err "Failed to get type of installation."
  311. return 1
  312. }
  313. if ! fn.exists "cmdline.spec:backup:cmd:${vps_type}:run"; then
  314. err "type '${vps_type}' has no backup process implemented yet."
  315. return 1
  316. fi
  317. "cmdline.spec:backup:cmd:${vps_type}:run"
  318. }
  319. cmdline.spec:backup:cmd:mailcow:run() {
  320. local cmd_line cron_line cmd
  321. for f in mysql-backup mirror-dir; do
  322. [ -e "/etc/cron.d/$f" ] || {
  323. err "Can't find '/etc/cron.d/$f'." \
  324. "Have you forgotten to run 'vps install backup BACKUP_HOST' ?"
  325. return 1
  326. }
  327. if ! cron_line=$(cat "/etc/cron.d/$f" |
  328. grep -v "^#" | grep "\* \* \*"); then
  329. err "Can't find cron_line in '/etc/cron.d/$f'." \
  330. "Have you modified it ?"
  331. return 1
  332. fi
  333. cron_line=${cron_line%|*}
  334. cmd_line=(${cron_line#*root})
  335. if [ "$f" == "mirror-dir" ]; then
  336. cmd=()
  337. for arg in "${cmd_line[@]}"; do
  338. [ "$arg" != "-q" ] && cmd+=("$arg")
  339. done
  340. else
  341. cmd=("${cmd_line[@]}")
  342. fi
  343. code="${cmd[*]}"
  344. echo "${WHITE}Launching:${NORMAL} ${code}"
  345. {
  346. {
  347. (
  348. ## Some commands are using colors that are already
  349. ## set by this current program and will trickle
  350. ## down unwantedly
  351. ansi_color no
  352. eval "${code}"
  353. ) | sed -r "s/^/ ${GRAY}|${NORMAL} /g"
  354. set_errlvl "${PIPESTATUS[0]}"
  355. } 3>&1 1>&2 2>&3 | sed -r "s/^/ $DARKRED\!$NORMAL /g"
  356. set_errlvl "${PIPESTATUS[0]}"
  357. } 3>&1 1>&2 2>&3
  358. if [ "$?" != "0" ]; then
  359. err "Failed."
  360. return 1
  361. fi
  362. done
  363. info "Mysql backup and subsequent mirror-dir ${DARKGREEN}succeeded${NORMAL}."
  364. }
  365. set_errlvl() { return "${1:-1}"; }
  366. cmdline.spec:backup:cmd:compose:run() {
  367. local cron_line args
  368. if ! cron_line=$(docker exec myc_cron_1 cat /etc/cron.d/rsync-backup | grep "\* \* \*"); then
  369. err "Can't find cron_line in cron container." \
  370. "Have you forgotten to run 'compose up' ?"
  371. exit 1
  372. fi
  373. cron_line=${cron_line%|*}
  374. cron_line=${cron_line%"2>&1"*}
  375. cmd_line="${cron_line#*root}"
  376. eval "args=($cmd_line)"
  377. ## should be last argument
  378. docker_cmd=$(echo ${args[@]: -1})
  379. if ! [[ "$docker_cmd" == "docker run --rm -e "* ]]; then
  380. echo "docker command found should start with 'docker run'." >&2
  381. echo "Here's command:" >&2
  382. echo " $docker_cmd" >&2
  383. exit 1
  384. fi
  385. echo "${WHITE}Launching:${NORMAL} docker exec -i myc_cron_1 $docker_cmd"
  386. {
  387. {
  388. eval "docker exec -i myc_cron_1 $docker_cmd" | sed -r "s/^/ ${GRAY}|${NORMAL} /g"
  389. set_errlvl "${PIPESTATUS[0]}"
  390. } 3>&1 1>&2 2>&3 | sed -r "s/^/ $DARKRED\!$NORMAL /g"
  391. set_errlvl "${PIPESTATUS[0]}"
  392. } 3>&1 1>&2 2>&3
  393. if [ "$?" != "0" ]; then
  394. err "Failed."
  395. return 1
  396. fi
  397. info "mirror-dir ${DARKGREEN}succeeded${NORMAL}."
  398. }
  399. cmdline::parse "$@"