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.

844 lines
23 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. include docker
  10. [[ "${BASH_SOURCE[0]}" != "${0}" ]] && SOURCED=true
  11. version=0.1
  12. desc='Install backup'
  13. help=""
  14. docker:running-container-projects() {
  15. :cache: scope=session
  16. docker ps --format '{{.Label "com.docker.compose.project"}}' | sort | uniq
  17. }
  18. decorator._mangle_fn docker:running-container-projects
  19. ssh:mk-private-key() {
  20. local host="$1" service_name="$2"
  21. (
  22. settmpdir VPS_TMPDIR
  23. ssh-keygen -t rsa -N "" -f "$VPS_TMPDIR/rsync_rsa" -C "$service_name@$host" >/dev/null
  24. cat "$VPS_TMPDIR/rsync_rsa"
  25. )
  26. }
  27. mailcow:has-images-running() {
  28. local images
  29. images=$(docker ps --format '{{.Image}}' | sort | uniq)
  30. [[ $'\n'"$images" == *$'\n'"mailcow/"* ]]
  31. }
  32. mailcow:has-container-project-mentionning-mailcow() {
  33. local projects
  34. projects=$(docker:running-container-projects) || return 1
  35. [[ $'\n'"$projects"$'\n' == *mailcow* ]]
  36. }
  37. mailcow:has-running-containers() {
  38. mailcow:has-images-running ||
  39. mailcow:has-container-project-mentionning-mailcow
  40. }
  41. mailcow:get-root() {
  42. :cache: scope=session
  43. local dir
  44. for dir in {/opt{,/apps},/root}/mailcow-dockerized; do
  45. [ -d "$dir" ] || continue
  46. [ -r "$dir/mailcow.conf" ] || continue
  47. echo "$dir"
  48. return 0
  49. done
  50. return 1
  51. }
  52. decorator._mangle_fn mailcow:get-root
  53. compose:get-compose-yml() {
  54. :cache: scope=session
  55. local path
  56. [ -e "/etc/compose/local.conf" ] && . "/etc/compose/local.conf"
  57. path=${DEFAULT_COMPOSE_FILE:-/etc/compose/compose.yml}
  58. [ -e "$path" ] || return 1
  59. echo "$path"
  60. }
  61. decorator._mangle_fn compose:get-compose-yml
  62. compose:has-container-project-myc() {
  63. local projects
  64. projects=$(docker:running-container-projects) || return 1
  65. [[ $'\n'"$projects"$'\n' == *$'\n'"myc"$'\n'* ]]
  66. }
  67. type:is-mailcow() {
  68. mailcow:get-root >/dev/null ||
  69. mailcow:has-running-containers
  70. }
  71. type:is-compose() {
  72. compose:get-compose-yml >/dev/null &&
  73. compose:has-container-project-myc
  74. }
  75. vps:get-type() {
  76. :cache: scope=session
  77. local fn
  78. for fn in $(declare -F | cut -f 3 -d " " | egrep "^type:is-"); do
  79. "$fn" && {
  80. echo "${fn#type:is-}"
  81. return 0
  82. }
  83. done
  84. return 1
  85. }
  86. decorator._mangle_fn vps:get-type
  87. mirror-dir:sources() {
  88. :cache: scope=session
  89. if ! shyaml get-values default.sources < /etc/mirror-dir/config.yml; then
  90. err "Couldn't query 'default.sources' in '/etc/mirror-dir/config.yml'."
  91. return 1
  92. fi
  93. }
  94. decorator._mangle_fn mirror-dir:sources
  95. mirror-dir:check-add() {
  96. local elt="$1" sources
  97. sources=$(mirror-dir:sources) || return 1
  98. if [[ $'\n'"$sources"$'\n' == *$'\n'"$elt"$'\n'* ]]; then
  99. info "Volume $elt already in sources"
  100. else
  101. Elt "Adding directory $elt"
  102. sed -i "/sources:/a\ - \"${elt}\"" \
  103. /etc/mirror-dir/config.yml
  104. Feedback || return 1
  105. fi
  106. }
  107. mirror-dir:check-add-vol() {
  108. local elt="$1"
  109. mirror-dir:check-add "/var/lib/docker/volumes/*_${elt}-*/_data"
  110. }
  111. ## The first colon is to prevent auto-export of function from shlib
  112. : ; bash-bug-5() { { cat; } < <(e) >/dev/null; ! cat "$1"; } && bash-bug-5 <(e) 2>/dev/null &&
  113. export BASH_BUG_5=1 && unset -f bash_bug_5
  114. wrap() {
  115. local label="$1" code="$2"
  116. shift 2
  117. export VERBOSE=1
  118. interpreter=/bin/bash
  119. if [ -n "$BASH_BUG_5" ]; then
  120. (
  121. settmpdir tmpdir
  122. fname=${label##*/}
  123. e "$code" > "$tmpdir/$fname" &&
  124. chmod +x "$tmpdir/$fname" &&
  125. Wrap -vsd "$label" -- "$interpreter" "$tmpdir/$fname" "$@"
  126. )
  127. else
  128. Wrap -vsd "$label" -- "$interpreter" <(e "$code") "$@"
  129. fi
  130. }
  131. ping_check() {
  132. #global ignore_ping_check
  133. local host="$1"
  134. ip=$(getent ahosts "$host" | egrep "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s+" |
  135. head -n 1 | cut -f 1 -d " ") || return 1
  136. my_ip=$(curl -s myip.kal.fr)
  137. if [ "$ip" != "$my_ip" ]; then
  138. if [ -n "$ignore_ping_check" ]; then
  139. warn "IP of '$host' ($ip) doesn't match mine ($my_ip). Ignoring due to ``--ignore-ping-check`` option."
  140. else
  141. err "IP of '$host' ($ip) doesn't match mine ($my_ip). Use ``--ignore-ping-check`` to ignore check."
  142. return 1
  143. fi
  144. fi
  145. }
  146. mailcow:install-backup() {
  147. local BACKUP_SERVER="$1" ignore_ping_check="$2" mailcow_root DOMAIN
  148. ## find installation
  149. mailcow_root=$(mailcow:get-root) || {
  150. err "Couldn't find a valid mailcow root directory."
  151. return 1
  152. }
  153. ## check ok
  154. DOMAIN=$(cat "$mailcow_root/.env" | grep ^MAILCOW_HOSTNAME= | cut -f 2 -d =) || {
  155. err "Couldn't find MAILCOW_HOSTNAME in file \"$mailcow_root/.env\"."
  156. return 1
  157. }
  158. ping_check "$DOMAIN" || return 1
  159. MYSQL_ROOT_PASSWORD=$(cat "$mailcow_root/.env" | grep ^DBROOT= | cut -f 2 -d =) || {
  160. err "Couldn't find DBROOT in file \"$mailcow_root/.env\"."
  161. return 1
  162. }
  163. MYSQL_CONTAINER=${MYSQL_CONTAINER:-mailcowdockerized_mysql-mailcow_1}
  164. container_id=$(docker ps -f name="$MYSQL_CONTAINER" --format "{{.ID}}")
  165. if [ -z "$container_id" ]; then
  166. err "Couldn't find docker container named '$MYSQL_CONTAINER'."
  167. return 1
  168. fi
  169. export KEY_BACKUP_ID="mailcow"
  170. export MYSQL_ROOT_PASSWORD
  171. export MYSQL_CONTAINER
  172. export BACKUP_SERVER
  173. export DOMAIN
  174. wrap "Install rsync-backup on host" "
  175. cd /srv/charm-store/rsync-backup
  176. bash ./hooks/install.d/60-install.sh
  177. " || return 1
  178. wrap "Mysql dump install" "
  179. cd /srv/charm-store/mariadb
  180. bash ./hooks/install.d/60-backup.sh
  181. " || return 1
  182. ## Using https://github.com/mailcow/mailcow-dockerized/blob/master/helper-scripts/backup_and_restore.sh
  183. for elt in "vmail{,-attachments-vol}" crypt redis rspamd postfix; do
  184. mirror-dir:check-add-vol "$elt" || return 1
  185. done
  186. mirror-dir:check-add "$mailcow_root" || return 1
  187. mirror-dir:check-add "/var/backups/mysql" || return 1
  188. mirror-dir:check-add "/etc" || return 1
  189. dest="$BACKUP_SERVER"
  190. dest="${dest%/*}"
  191. ssh_options=()
  192. if [[ "$dest" == *":"* ]]; then
  193. port="${dest##*:}"
  194. dest="${dest%%:*}"
  195. ssh_options=(-p "$port")
  196. else
  197. port=""
  198. dest="${dest%%:*}"
  199. fi
  200. info "You can run this following command from an host having admin access to $dest:"
  201. echo " (Or send it to a backup admin of $dest)" >&2
  202. echo "ssh ${ssh_options[@]} myadmin@$dest ssh-key add '$(cat /var/lib/rsync/.ssh/id_rsa.pub)'"
  203. }
  204. compose:has_domain() {
  205. local compose_file="$1" host="$2" name conf relation relation_value domain server_aliases
  206. while read-0 name conf ; do
  207. name=$(e "$name" | shyaml get-value)
  208. if [[ "$name" =~ ^[a-zA-Z0-9-]+\.[a-zA-Z0-9-]+ ]]; then
  209. [ "$host" == "$name" ] && return 0
  210. fi
  211. rel=$(e "$conf" | shyaml -y get-value relations 2>/dev/null) || continue
  212. for relation in web-proxy publish-dir; do
  213. relation_value=$(e "$rel" | shyaml -y get-value "$relation" 2>/dev/null) || continue
  214. while read-0 label conf_relation; do
  215. domain=$(e "$conf_relation" | shyaml get-value "domain" 2>/dev/null) && {
  216. [ "$host" == "$domain" ] && return 0
  217. }
  218. server_aliases=$(e "$conf_relation" | shyaml get-values "server-aliases" 2>/dev/null) && {
  219. [[ $'\n'"$server_aliases" == *$'\n'"$host"$'\n'* ]] && return 0
  220. }
  221. done < <(e "$relation_value" | shyaml -y key-values-0)
  222. done
  223. done < <(shyaml -y key-values-0 < "$compose_file")
  224. return 1
  225. }
  226. compose:install-backup() {
  227. local BACKUP_SERVER="$1" service_name="$2" compose_file="$3" ignore_ping_check="$4" ignore_domain_check="$5"
  228. ## XXXvlab: far from perfect as it mimics and depends internal
  229. ## logic of current default way to get a domain in compose-core
  230. host=$(hostname)
  231. if ! compose:has_domain "$compose_file" "$host"; then
  232. if [ -n "$ignore_domain_check" ]; then
  233. warn "domain of '$host' not found in compose file '$compose_file'. Ignoring due to ``--ignore-domain-check`` option."
  234. else
  235. err "domain of '$host' not found in compose file '$compose_file'. Use ``--ignore-domain-check`` to ignore check."
  236. return 1
  237. fi
  238. fi
  239. ping_check "$host" || return 1
  240. if [ -e "/root/.ssh/rsync_rsa" ]; then
  241. warn "deleting private key in /root/.ssh/rsync_rsa, has we are not using it anymore."
  242. rm -fv /root/.ssh/rsync_rsa
  243. fi
  244. if [ -e "/root/.ssh/rsync_rsa.pub" ]; then
  245. warn "deleting public key in /root/.ssh/rsync_rsa.pub, has we are not using it anymore."
  246. rm -fv /root/.ssh/rsync_rsa.pub
  247. fi
  248. if service_cfg=$(cat "$compose_file" |
  249. shyaml get-value -y "$service_name" 2>/dev/null); then
  250. info "Entry for service ${DARKYELLOW}$service_name${NORMAL}" \
  251. "is already present in '$compose_file'."
  252. cfg=$(e "$service_cfg" | shyaml get-value -y options) || {
  253. err "No ${WHITE}options${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  254. "entry in '$compose_file'."
  255. return 1
  256. }
  257. private_key=$(e "$cfg" | shyaml get-value private-key) || return 1
  258. target=$(e "$cfg" | shyaml get-value target) || return 1
  259. if [ "$target" != "$BACKUP_SERVER" ]; then
  260. err "Existing backup target '$target' is different" \
  261. "from specified '$BACKUP_SERVER'"
  262. return 1
  263. fi
  264. else
  265. private_key=$(ssh:mk-private-key "$host" "$service_name")
  266. cat <<EOF >> "$compose_file"
  267. $service_name:
  268. options:
  269. ident: $host
  270. target: $BACKUP_SERVER
  271. private-key: |
  272. $(e "$private_key" | sed -r 's/^/ /g')
  273. EOF
  274. fi
  275. dest="$BACKUP_SERVER"
  276. dest="${dest%/*}"
  277. ssh_options=()
  278. if [[ "$dest" == *":"* ]]; then
  279. port="${dest##*:}"
  280. dest="${dest%%:*}"
  281. ssh_options=(-p "$port")
  282. else
  283. port=""
  284. dest="${dest%%:*}"
  285. fi
  286. info "You can run this following command from an host having admin access to $dest:"
  287. echo " (Or send it to a backup admin of $dest)" >&2
  288. public_key=$(ssh-keygen -y -f <(e "$private_key"$'\n'))
  289. echo "ssh ${ssh_options[@]} myadmin@$dest ssh-key add '$public_key compose@$host'"
  290. }
  291. backup-action() {
  292. local action="$1"
  293. shift
  294. vps_type=$(vps:get-type) || {
  295. err "Failed to get type of installation."
  296. return 1
  297. }
  298. if ! fn.exists "${vps_type}:${action}"; then
  299. err "type '${vps_type}' has no ${vps_type}:${action} implemented yet."
  300. return 1
  301. fi
  302. "${vps_type}:${action}" "$@"
  303. }
  304. compose:get_default_backup_host_ident() {
  305. local service_name="$1" ## Optional
  306. local compose_file service_cfg cfg target
  307. compose_file=$(compose:get-compose-yml)
  308. service_name="${service_name:-rsync-backup}"
  309. if ! service_cfg=$(cat "$compose_file" |
  310. shyaml get-value -y "$service_name" 2>/dev/null); then
  311. err "No service named '$service_name' found in 'compose.yml'."
  312. return 1
  313. fi
  314. cfg=$(e "$service_cfg" | shyaml get-value -y options) || {
  315. err "No ${WHITE}options${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  316. "entry in '$compose_file'."
  317. return 1
  318. }
  319. if ! target=$(e "$cfg" | shyaml get-value target); then
  320. err "No ${WHITE}options.target${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  321. "entry in '$compose_file'."
  322. fi
  323. if ! target=$(e "$cfg" | shyaml get-value target); then
  324. err "No ${WHITE}options.target${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  325. "entry in '$compose_file'."
  326. fi
  327. if ! ident=$(e "$cfg" | shyaml get-value ident); then
  328. err "No ${WHITE}options.ident${NORMAL} in ${DARKYELLOW}$service_name${NORMAL}'s" \
  329. "entry in '$compose_file'."
  330. fi
  331. echo "$target $ident"
  332. }
  333. mailcow:get_default_backup_host_ident() {
  334. local content cron_line ident found dest cmd_line
  335. if ! [ -e "/etc/cron.d/mirror-dir" ]; then
  336. err "No '/etc/cron.d/mirror-dir' found."
  337. return 1
  338. fi
  339. content=$(cat /etc/cron.d/mirror-dir) || {
  340. err "Can't read '/etc/cron.d/mirror-dir'."
  341. return 1
  342. }
  343. if ! cron_line=$(e "$content" | grep "mirror-dir backup"); then
  344. err "Can't find 'mirror-dir backup' line in '/etc/cron.d/mirror-dir'."
  345. return 1
  346. fi
  347. cron_line=${cron_line%|*}
  348. cmd_line=(${cron_line#*root})
  349. found=
  350. dest=
  351. for arg in "${cmd_line[@]}"; do
  352. [ -n "$found" ] && {
  353. dest="$arg"
  354. break
  355. }
  356. [ "$arg" == "-d" ] && {
  357. found=1
  358. }
  359. done
  360. if ! [[ "$dest" =~ ^[\'\"a-zA-Z0-9:/.-]+$ ]]; then
  361. err "Can't find valid destination in 'mirror-dir backup' arguments from '/etc/cron.d/mirror-dir'."
  362. return 1
  363. fi
  364. if [[ "$dest" == \"*\" ]] || [[ "$dest" == \'*\' ]]; then
  365. ## unquoting, the eval should be safe because of previous check
  366. dest=$(eval e "$dest")
  367. fi
  368. if [ -z "$dest" ]; then
  369. err "Can't find destination in 'mirror-dir backup' arguments from '/etc/cron.d/mirror-dir'."
  370. return 1
  371. fi
  372. ## looking for ident
  373. found=
  374. ident=
  375. for arg in "${cmd_line[@]}"; do
  376. [ -n "$found" ] && {
  377. ident="$arg"
  378. break
  379. }
  380. [ "$arg" == "-h" ] && {
  381. found=1
  382. }
  383. done
  384. if ! [[ "$ident" =~ ^[\'\"a-zA-Z0-9.-]+$ ]]; then
  385. err "Can't find valid identifier in 'mirror-dir backup' arguments from '/etc/cron.d/mirror-dir'."
  386. return 1
  387. fi
  388. if [[ "$ident" == \"*\" ]] || [[ "$ident" == \'*\' ]]; then
  389. ## unquoting, the eval should be safe because of previous check
  390. ident=$(eval e "$ident")
  391. fi
  392. if [ -z "$ident" ]; then
  393. err "Can't find destination in 'mirror-dir backup' arguments from '/etc/cron.d/mirror-dir'."
  394. return 1
  395. fi
  396. echo "$dest $ident"
  397. }
  398. compose:get_cron_docker_cmd() {
  399. local cron_line cmd_line docker_cmd
  400. if ! cron_line=$(docker exec myc_cron_1 cat /etc/cron.d/rsync-backup | grep "\* \* \*"); then
  401. err "Can't find cron_line in cron container."
  402. echo " Have you forgotten to run 'compose up' ?" >&2
  403. return 1
  404. fi
  405. cron_line=${cron_line%|*}
  406. cron_line=${cron_line%"2>&1"*}
  407. cmd_line="${cron_line#*root}"
  408. eval "args=($cmd_line)"
  409. ## should be last argument
  410. docker_cmd=$(echo ${args[@]: -1})
  411. if ! [[ "$docker_cmd" == "docker run --rm -e "* ]]; then
  412. echo "docker command found should start with 'docker run'." >&2
  413. echo "Here's command:" >&2
  414. echo " $docker_cmd" >&2
  415. return 1
  416. fi
  417. e "$docker_cmd"
  418. }
  419. compose:recover-target() {
  420. local backup_host="$1" ident="$2" src="$3" dst="$4" service_name="${5:-rsync-backup}"
  421. docker_image="myc_${service_name}"
  422. if ! docker_has_image "$docker_image"; then
  423. compose build "${service_name}" || {
  424. err "Couldn't find nor build image for service '$service_name'."
  425. return 1
  426. }
  427. fi
  428. dst="${dst%/}" ## remove final slash
  429. ssh_options=(-o StrictHostKeyChecking=no)
  430. if [[ "$backup_host" == *":"* ]]; then
  431. port="${backup_host##*:}"
  432. backup_host="${backup_host%%:*}"
  433. ssh_options+=(-p "$port")
  434. else
  435. port=""
  436. backup_host="${backup_host%%:*}"
  437. fi
  438. rsync_opts=(
  439. -e "ssh ${ssh_options[*]} -i /var/lib/rsync/.ssh/id_rsa -l rsync"
  440. -azvArH --delete --delete-excluded
  441. --partial --partial-dir .rsync-partial
  442. --numeric-ids
  443. )
  444. if [ "$DRY_RUN" ]; then
  445. rsync_opts+=("-n")
  446. fi
  447. cmd=(
  448. docker run --rm --entrypoint rsync \
  449. -v "/srv/datastore/config/${service_name}/var/lib/rsync":/var/lib/rsync \
  450. -v "${dst%/*}":/mnt/dest \
  451. "$docker_image" \
  452. "${rsync_opts[@]}" "$backup_host":"/var/mirror/$ident/$src" "/mnt/dest/${dst##*/}"
  453. )
  454. echo "${WHITE}Launching: ${NORMAL} ${cmd[@]}"
  455. "${cmd[@]}"
  456. }
  457. mailcow:recover-target() {
  458. local backup_host="$1" ident="$2" src="$3" dst="$4"
  459. dst="${dst%/}" ## remove final slash
  460. ssh_options=(-o StrictHostKeyChecking=no)
  461. if [[ "$backup_host" == *":"* ]]; then
  462. port="${backup_host##*:}"
  463. backup_host="${backup_host%%:*}"
  464. ssh_options+=(-p "$port")
  465. else
  466. port=""
  467. backup_host="${backup_host%%:*}"
  468. fi
  469. rsync_opts=(
  470. -e "ssh ${ssh_options[*]} -i /var/lib/rsync/.ssh/id_rsa -l rsync"
  471. -azvArH --delete --delete-excluded
  472. --partial --partial-dir .rsync-partial
  473. --numeric-ids
  474. )
  475. if [ "$DRY_RUN" ]; then
  476. rsync_opts+=("-n")
  477. fi
  478. cmd=(
  479. rsync "${rsync_opts[@]}" "$backup_host":"/var/mirror/$ident/$src" "${dst}"
  480. )
  481. echo "${WHITE}Launching: ${NORMAL} ${cmd[@]}"
  482. "${cmd[@]}"
  483. }
  484. [ "$SOURCED" ] && return 0
  485. ##
  486. ## Command line processing
  487. ##
  488. cmdline.spec.gnu
  489. cmdline.spec.reporting
  490. cmdline.spec.gnu install
  491. cmdline.spec::cmd:install:run() {
  492. :
  493. }
  494. cmdline.spec.gnu get-type
  495. cmdline.spec::cmd:get-type:run() {
  496. vps:get-type
  497. }
  498. cmdline.spec:install:cmd:backup:run() {
  499. : :posarg: BACKUP_SERVER 'Target backup server'
  500. : :optfla: --ignore-domain-check \
  501. "Allow to bypass the domain check in
  502. compose file (only used in compose
  503. installation)."
  504. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  505. local vps_type
  506. vps_type=$(vps:get-type) || {
  507. err "Failed to get type of installation."
  508. return 1
  509. }
  510. if ! fn.exists "${vps_type}:install-backup"; then
  511. err "type '${vps_type}' has no backup installation implemented yet."
  512. return 1
  513. fi
  514. opts=()
  515. [ "$opt_ignore_ping_check" ] &&
  516. opts+=("--ignore-ping-check")
  517. if [ "$vps_type" == "compose" ]; then
  518. [ "$opt_ignore_domain_check" ] &&
  519. opts+=("--ignore-domain-check")
  520. fi
  521. "cmdline.spec:install:cmd:${vps_type}-backup:run" "${opts[@]}" "$BACKUP_SERVER"
  522. }
  523. DEFAULT_BACKUP_SERVICE_NAME=rsync-backup
  524. cmdline.spec.gnu compose-backup
  525. cmdline.spec:install:cmd:compose-backup:run() {
  526. : :posarg: BACKUP_SERVER 'Target backup server'
  527. : :optval: --service-name,-s "YAML service name in compose
  528. file to check for existence of key.
  529. Defaults to '$DEFAULT_BACKUP_SERVICE_NAME'"
  530. : :optval: --compose-file,-f "Compose file location. Defaults to
  531. the value of '\$DEFAULT_COMPOSE_FILE'"
  532. : :optfla: --ignore-domain-check \
  533. "Allow to bypass the domain check in
  534. compose file."
  535. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  536. local service_name compose_file
  537. [ -e "/etc/compose/local.conf" ] && source /etc/compose/local.conf
  538. compose_file=${opt_compose_file:-$DEFAULT_COMPOSE_FILE}
  539. service_name=${opt_service_name:-$DEFAULT_BACKUP_SERVICE_NAME}
  540. if ! [ -e "$compose_file" ]; then
  541. err "Compose file not found in '$compose_file'."
  542. return 1
  543. fi
  544. compose:install-backup "$BACKUP_SERVER" "$service_name" "$compose_file" \
  545. "$opt_ignore_ping_check" "$opt_ignore_domain_check"
  546. }
  547. cmdline.spec:install:cmd:mailcow-backup:run() {
  548. : :posarg: BACKUP_SERVER 'Target backup server'
  549. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  550. "mailcow:install-backup" "$BACKUP_SERVER" "$opt_ignore_ping_check"
  551. }
  552. cmdline.spec.gnu backup
  553. cmdline.spec::cmd:backup:run() {
  554. local vps_type
  555. vps_type=$(vps:get-type) || {
  556. err "Failed to get type of installation."
  557. return 1
  558. }
  559. if ! fn.exists "cmdline.spec:backup:cmd:${vps_type}:run"; then
  560. err "type '${vps_type}' has no backup process implemented yet."
  561. return 1
  562. fi
  563. "cmdline.spec:backup:cmd:${vps_type}:run"
  564. }
  565. cmdline.spec:backup:cmd:mailcow:run() {
  566. local cmd_line cron_line cmd
  567. for f in mysql-backup mirror-dir; do
  568. [ -e "/etc/cron.d/$f" ] || {
  569. err "Can't find '/etc/cron.d/$f'."
  570. echo " Have you forgotten to run 'vps install backup BACKUP_HOST' ?" >&2
  571. return 1
  572. }
  573. if ! cron_line=$(cat "/etc/cron.d/$f" |
  574. grep -v "^#" | grep "\* \* \*"); then
  575. err "Can't find cron_line in '/etc/cron.d/$f'." \
  576. "Have you modified it ?"
  577. return 1
  578. fi
  579. cron_line=${cron_line%|*}
  580. cmd_line=(${cron_line#*root})
  581. if [ "$f" == "mirror-dir" ]; then
  582. cmd=()
  583. for arg in "${cmd_line[@]}"; do
  584. [ "$arg" != "-q" ] && cmd+=("$arg")
  585. done
  586. else
  587. cmd=("${cmd_line[@]}")
  588. fi
  589. code="${cmd[*]}"
  590. echo "${WHITE}Launching:${NORMAL} ${code}"
  591. {
  592. {
  593. (
  594. ## Some commands are using colors that are already
  595. ## set by this current program and will trickle
  596. ## down unwantedly
  597. ansi_color no
  598. eval "${code}"
  599. ) | sed -r "s/^/ ${GRAY}|${NORMAL} /g"
  600. set_errlvl "${PIPESTATUS[0]}"
  601. } 3>&1 1>&2 2>&3 | sed -r "s/^/ $DARKRED\!$NORMAL /g"
  602. set_errlvl "${PIPESTATUS[0]}"
  603. } 3>&1 1>&2 2>&3
  604. if [ "$?" != "0" ]; then
  605. err "Failed."
  606. return 1
  607. fi
  608. done
  609. info "Mysql backup and subsequent mirror-dir ${DARKGREEN}succeeded${NORMAL}."
  610. }
  611. set_errlvl() { return "${1:-1}"; }
  612. cmdline.spec:backup:cmd:compose:run() {
  613. local cron_line args
  614. docker_cmd=$(compose:get_cron_docker_cmd) || return 1
  615. echo "${WHITE}Launching:${NORMAL} docker exec -i myc_cron_1 $docker_cmd"
  616. {
  617. {
  618. eval "docker exec -i myc_cron_1 $docker_cmd" | sed -r "s/^/ ${GRAY}|${NORMAL} /g"
  619. set_errlvl "${PIPESTATUS[0]}"
  620. } 3>&1 1>&2 2>&3 | sed -r "s/^/ $DARKRED\!$NORMAL /g"
  621. set_errlvl "${PIPESTATUS[0]}"
  622. } 3>&1 1>&2 2>&3
  623. if [ "$?" != "0" ]; then
  624. err "Failed."
  625. return 1
  626. fi
  627. info "mirror-dir ${DARKGREEN}succeeded${NORMAL}."
  628. }
  629. cmdline.spec.gnu recover-target
  630. cmdline.spec::cmd:recover-target:run() {
  631. : :posarg: BACKUP_DIR 'Source directory on backup side'
  632. : :posarg: HOST_DIR 'Target directory on host side'
  633. : :optval: --backup-host,-B "The backup host"
  634. : :optfla: --dry-run,-n "Don't do anything, instead tell what it
  635. would do."
  636. ## if no backup host take the one by default
  637. backup_host="$opt_backup_host"
  638. if [ -z "$backup_host" ]; then
  639. backup_host_ident=$(backup-action get_default_backup_host_ident) || return 1
  640. read -r backup_host ident <<<"$backup_host_ident"
  641. fi
  642. if [[ "$BACKUP_DIR" == /* ]]; then
  643. err "BACKUP_DIR must be a relative path from the root of your backup."
  644. return 1
  645. fi
  646. REAL_HOST_DIR=$(realpath "$HOST_DIR") || {
  647. err "Can't find HOST_DIR '$HOST_DIR'."
  648. return 1
  649. }
  650. export DRY_RUN="${opt_dry_run}"
  651. backup-action recover-target "$backup_host" "$ident" "$BACKUP_DIR" "$REAL_HOST_DIR"
  652. }
  653. cmdline::parse "$@"