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.

1142 lines
32 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_url() {
  399. local service="$1"
  400. (
  401. set -o pipefail
  402. cat "/var/lib/compose/relations/myc/${service}-frontend/web-proxy/data" |
  403. shyaml get-value url
  404. ) || {
  405. err "Failed querying ${service} to frontend relation to get url."
  406. return 1
  407. }
  408. }
  409. export -f compose:get_url
  410. compose:psql() {
  411. local dbname="$1"
  412. docker exec -i myc_postgres_1 psql -U postgres "$dbname"
  413. }
  414. export -f compose:psql
  415. cyclos:set_root_url() {
  416. local dbname="$1" cyclos_service="$2" url
  417. url=$(compose:get_url "${cyclos_service}") || return 1
  418. echo "UPDATE configurations SET root_url = '$url';" |
  419. compose:psql "$dbname" || {
  420. err "Failed to set cyclos url value in '$dbname' database."
  421. return 1
  422. }
  423. }
  424. export -f cyclos:set_root_url
  425. compose:get_cron_docker_cmd() {
  426. local cron_line cmd_line docker_cmd
  427. if ! cron_line=$(docker exec myc_cron_1 cat /etc/cron.d/rsync-backup | grep "\* \* \*"); then
  428. err "Can't find cron_line in cron container."
  429. echo " Have you forgotten to run 'compose up' ?" >&2
  430. return 1
  431. fi
  432. cron_line=${cron_line%|*}
  433. cron_line=${cron_line%"2>&1"*}
  434. cmd_line="${cron_line#*root}"
  435. eval "args=($cmd_line)"
  436. ## should be last argument
  437. docker_cmd=$(echo ${args[@]: -1})
  438. if ! [[ "$docker_cmd" == "docker run --rm -e "* ]]; then
  439. echo "docker command found should start with 'docker run'." >&2
  440. echo "Here's command:" >&2
  441. echo " $docker_cmd" >&2
  442. return 1
  443. fi
  444. e "$docker_cmd"
  445. }
  446. compose:recover-target() {
  447. local backup_host="$1" ident="$2" src="$3" dst="$4" service_name="${5:-rsync-backup}"
  448. docker_image="myc_${service_name}"
  449. if ! docker_has_image "$docker_image"; then
  450. compose build "${service_name}" || {
  451. err "Couldn't find nor build image for service '$service_name'."
  452. return 1
  453. }
  454. fi
  455. dst="${dst%/}" ## remove final slash
  456. ssh_options=(-o StrictHostKeyChecking=no)
  457. if [[ "$backup_host" == *":"* ]]; then
  458. port="${backup_host##*:}"
  459. backup_host="${backup_host%%:*}"
  460. ssh_options+=(-p "$port")
  461. else
  462. port=""
  463. backup_host="${backup_host%%:*}"
  464. fi
  465. rsync_opts=(
  466. -e "ssh ${ssh_options[*]} -i /var/lib/rsync/.ssh/id_rsa -l rsync"
  467. -azvArH --delete --delete-excluded
  468. --partial --partial-dir .rsync-partial
  469. --numeric-ids
  470. )
  471. if [ "$DRY_RUN" ]; then
  472. rsync_opts+=("-n")
  473. fi
  474. cmd=(
  475. docker run --rm --entrypoint rsync \
  476. -v "/srv/datastore/config/${service_name}/var/lib/rsync":/var/lib/rsync \
  477. -v "${dst%/*}":/mnt/dest \
  478. "$docker_image" \
  479. "${rsync_opts[@]}" "$backup_host":"/var/mirror/$ident/$src" "/mnt/dest/${dst##*/}"
  480. )
  481. echo "${WHITE}Launching: ${NORMAL} ${cmd[@]}"
  482. "${cmd[@]}"
  483. }
  484. mailcow:recover-target() {
  485. local backup_host="$1" ident="$2" src="$3" dst="$4"
  486. dst="${dst%/}" ## remove final slash
  487. ssh_options=(-o StrictHostKeyChecking=no)
  488. if [[ "$backup_host" == *":"* ]]; then
  489. port="${backup_host##*:}"
  490. backup_host="${backup_host%%:*}"
  491. ssh_options+=(-p "$port")
  492. else
  493. port=""
  494. backup_host="${backup_host%%:*}"
  495. fi
  496. rsync_opts=(
  497. -e "ssh ${ssh_options[*]} -i /var/lib/rsync/.ssh/id_rsa -l rsync"
  498. -azvArH --delete --delete-excluded
  499. --partial --partial-dir .rsync-partial
  500. --numeric-ids
  501. )
  502. if [ "$DRY_RUN" ]; then
  503. rsync_opts+=("-n")
  504. fi
  505. cmd=(
  506. rsync "${rsync_opts[@]}" "$backup_host":"/var/mirror/$ident/$src" "${dst}"
  507. )
  508. echo "${WHITE}Launching: ${NORMAL} ${cmd[@]}"
  509. "${cmd[@]}"
  510. }
  511. [ "$SOURCED" ] && return 0
  512. ##
  513. ## Command line processing
  514. ##
  515. cmdline.spec.gnu
  516. cmdline.spec.reporting
  517. cmdline.spec.gnu install
  518. cmdline.spec::cmd:install:run() {
  519. :
  520. }
  521. cmdline.spec.gnu get-type
  522. cmdline.spec::cmd:get-type:run() {
  523. vps:get-type
  524. }
  525. cmdline.spec:install:cmd:backup:run() {
  526. : :posarg: BACKUP_SERVER 'Target backup server'
  527. : :optfla: --ignore-domain-check \
  528. "Allow to bypass the domain check in
  529. compose file (only used in compose
  530. installation)."
  531. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  532. local vps_type
  533. vps_type=$(vps:get-type) || {
  534. err "Failed to get type of installation."
  535. return 1
  536. }
  537. if ! fn.exists "${vps_type}:install-backup"; then
  538. err "type '${vps_type}' has no backup installation implemented yet."
  539. return 1
  540. fi
  541. opts=()
  542. [ "$opt_ignore_ping_check" ] &&
  543. opts+=("--ignore-ping-check")
  544. if [ "$vps_type" == "compose" ]; then
  545. [ "$opt_ignore_domain_check" ] &&
  546. opts+=("--ignore-domain-check")
  547. fi
  548. "cmdline.spec:install:cmd:${vps_type}-backup:run" "${opts[@]}" "$BACKUP_SERVER"
  549. }
  550. DEFAULT_BACKUP_SERVICE_NAME=rsync-backup
  551. cmdline.spec.gnu compose-backup
  552. cmdline.spec:install:cmd:compose-backup:run() {
  553. : :posarg: BACKUP_SERVER 'Target backup server'
  554. : :optval: --service-name,-s "YAML service name in compose
  555. file to check for existence of key.
  556. Defaults to '$DEFAULT_BACKUP_SERVICE_NAME'"
  557. : :optval: --compose-file,-f "Compose file location. Defaults to
  558. the value of '\$DEFAULT_COMPOSE_FILE'"
  559. : :optfla: --ignore-domain-check \
  560. "Allow to bypass the domain check in
  561. compose file."
  562. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  563. local service_name compose_file
  564. [ -e "/etc/compose/local.conf" ] && source /etc/compose/local.conf
  565. compose_file=${opt_compose_file:-$DEFAULT_COMPOSE_FILE}
  566. service_name=${opt_service_name:-$DEFAULT_BACKUP_SERVICE_NAME}
  567. if ! [ -e "$compose_file" ]; then
  568. err "Compose file not found in '$compose_file'."
  569. return 1
  570. fi
  571. compose:install-backup "$BACKUP_SERVER" "$service_name" "$compose_file" \
  572. "$opt_ignore_ping_check" "$opt_ignore_domain_check"
  573. }
  574. cmdline.spec:install:cmd:mailcow-backup:run() {
  575. : :posarg: BACKUP_SERVER 'Target backup server'
  576. : :optfla: --ignore-ping-check "Allow to bypass the ping check of host."
  577. "mailcow:install-backup" "$BACKUP_SERVER" "$opt_ignore_ping_check"
  578. }
  579. cmdline.spec.gnu backup
  580. cmdline.spec::cmd:backup:run() {
  581. local vps_type
  582. vps_type=$(vps:get-type) || {
  583. err "Failed to get type of installation."
  584. return 1
  585. }
  586. if ! fn.exists "cmdline.spec:backup:cmd:${vps_type}:run"; then
  587. err "type '${vps_type}' has no backup process implemented yet."
  588. return 1
  589. fi
  590. "cmdline.spec:backup:cmd:${vps_type}:run"
  591. }
  592. cmdline.spec:backup:cmd:mailcow:run() {
  593. local cmd_line cron_line cmd
  594. for f in mysql-backup mirror-dir; do
  595. [ -e "/etc/cron.d/$f" ] || {
  596. err "Can't find '/etc/cron.d/$f'."
  597. echo " Have you forgotten to run 'vps install backup BACKUP_HOST' ?" >&2
  598. return 1
  599. }
  600. if ! cron_line=$(cat "/etc/cron.d/$f" |
  601. grep -v "^#" | grep "\* \* \*"); then
  602. err "Can't find cron_line in '/etc/cron.d/$f'." \
  603. "Have you modified it ?"
  604. return 1
  605. fi
  606. cron_line=${cron_line%|*}
  607. cmd_line=(${cron_line#*root})
  608. if [ "$f" == "mirror-dir" ]; then
  609. cmd=()
  610. for arg in "${cmd_line[@]}"; do
  611. [ "$arg" != "-q" ] && cmd+=("$arg")
  612. done
  613. else
  614. cmd=("${cmd_line[@]}")
  615. fi
  616. code="${cmd[*]}"
  617. echo "${WHITE}Launching:${NORMAL} ${code}"
  618. {
  619. {
  620. (
  621. ## Some commands are using colors that are already
  622. ## set by this current program and will trickle
  623. ## down unwantedly
  624. ansi_color no
  625. eval "${code}"
  626. ) | sed -r "s/^/ ${GRAY}|${NORMAL} /g"
  627. set_errlvl "${PIPESTATUS[0]}"
  628. } 3>&1 1>&2 2>&3 | sed -r "s/^/ $DARKRED\!$NORMAL /g"
  629. set_errlvl "${PIPESTATUS[0]}"
  630. } 3>&1 1>&2 2>&3
  631. if [ "$?" != "0" ]; then
  632. err "Failed."
  633. return 1
  634. fi
  635. done
  636. info "Mysql backup and subsequent mirror-dir ${DARKGREEN}succeeded${NORMAL}."
  637. }
  638. set_errlvl() { return "${1:-1}"; }
  639. cmdline.spec:backup:cmd:compose:run() {
  640. local cron_line args
  641. docker_cmd=$(compose:get_cron_docker_cmd) || return 1
  642. echo "${WHITE}Launching:${NORMAL} docker exec -i myc_cron_1 $docker_cmd"
  643. {
  644. {
  645. eval "docker exec -i myc_cron_1 $docker_cmd" | sed -r "s/^/ ${GRAY}|${NORMAL} /g"
  646. set_errlvl "${PIPESTATUS[0]}"
  647. } 3>&1 1>&2 2>&3 | sed -r "s/^/ $DARKRED\!$NORMAL /g"
  648. set_errlvl "${PIPESTATUS[0]}"
  649. } 3>&1 1>&2 2>&3
  650. if [ "$?" != "0" ]; then
  651. err "Failed."
  652. return 1
  653. fi
  654. info "mirror-dir ${DARKGREEN}succeeded${NORMAL}."
  655. }
  656. cmdline.spec.gnu recover-target
  657. cmdline.spec::cmd:recover-target:run() {
  658. : :posarg: BACKUP_DIR 'Source directory on backup side'
  659. : :posarg: HOST_DIR 'Target directory on host side'
  660. : :optval: --backup-host,-B "The backup host"
  661. : :optfla: --dry-run,-n "Don't do anything, instead tell what it
  662. would do."
  663. ## if no backup host take the one by default
  664. backup_host="$opt_backup_host"
  665. if [ -z "$backup_host" ]; then
  666. backup_host_ident=$(backup-action get_default_backup_host_ident) || return 1
  667. read -r backup_host ident <<<"$backup_host_ident"
  668. fi
  669. if [[ "$BACKUP_DIR" == /* ]]; then
  670. err "BACKUP_DIR must be a relative path from the root of your backup."
  671. return 1
  672. fi
  673. REAL_HOST_DIR=$(realpath "$HOST_DIR") || {
  674. err "Can't find HOST_DIR '$HOST_DIR'."
  675. return 1
  676. }
  677. export DRY_RUN="${opt_dry_run}"
  678. backup-action recover-target "$backup_host" "$ident" "$BACKUP_DIR" "$REAL_HOST_DIR"
  679. }
  680. cmdline.spec.gnu odoo
  681. cmdline.spec::cmd:odoo:run() {
  682. :
  683. }
  684. cmdline.spec.gnu restart
  685. cmdline.spec:odoo:cmd:restart:run() {
  686. : :optval: --service,-s "The service (defaults to 'odoo')"
  687. local out odoo_service
  688. odoo_service="${opt_service:-odoo}"
  689. if ! out=$(docker restart "myc_${odoo_service}_1" 2>&1); then
  690. if [[ "$out" == *"no matching entries in passwd file" ]]; then
  691. warn "Catched docker bug. Restarting once more."
  692. if ! out=$(docker restart "myc_${odoo_service}_1"); then
  693. err "Can't restart container myc_${odoo_service}_1 (restarted twice)."
  694. echo " output:" >&2
  695. echo "$out" | prefix " ${GRAY}|${NORMAL} " >&2
  696. exit 1
  697. fi
  698. else
  699. err "Couldn't restart container myc_${odoo_service}_1 (and no restart bug detected)."
  700. exit 1
  701. fi
  702. fi
  703. info "Container myc_${odoo_service}_1 was ${DARKGREEN}successfully${NORMAL} restarted."
  704. }
  705. cmdline.spec.gnu restore
  706. cmdline.spec:odoo:cmd:restore:run() {
  707. : :posarg: ZIP_DUMP_LOCATION 'Source odoo dump file to restore
  708. (can be a local file or an url)'
  709. : :optval: --service,-s "The service (defaults to 'odoo')"
  710. : :optval: --database,-d 'Target database (default if not specified)'
  711. local out
  712. odoo_service="${opt_service:-odoo}"
  713. if [[ "$ZIP_DUMP_LOCATION" == "http://"* ]]; then
  714. settmpdir ZIP_TMP_LOCATION
  715. tmp_location="$ZIP_TMP_LOCATION/dump.zip"
  716. curl -k -s -L "$ZIP_DUMP_LOCATION" > "$tmp_location" || {
  717. err "Couldn't get '$ZIP_DUMP_LOCATION'."
  718. exit 1
  719. }
  720. if [[ "$(dd if="$tmp_location" count=2 bs=1 2>/dev/null)" != "PK" ]]; then
  721. err "Download doesn't seem to be a zip file."
  722. dd if="$tmp_location" count=1 bs=256 | hd | prefix " ${GRAY}|${NORMAL} " >&2
  723. exit 1
  724. fi
  725. info "Successfully downloaded '$ZIP_DUMP_LOCATION'"
  726. echo " in '$tmp_location'." >&2
  727. ZIP_DUMP_LOCATION="$tmp_location"
  728. fi
  729. [ -e "$ZIP_DUMP_LOCATION" ] || {
  730. err "No file '$ZIP_DUMP_LOCATION' found." >&2
  731. exit 1
  732. }
  733. #cmdline.spec:odoo:cmd:restart:run --service "$odoo_service" || exit 1
  734. msg_dbname=default
  735. [ -n "$opt_database" ] && msg_dbname="'$opt_database'"
  736. compose --no-hooks drop "$odoo_service" $opt_database || {
  737. err "Error dropping $msg_dbname database of service ${DARKYELLOW}$odoo_service${NORMAL}."
  738. exit 1
  739. }
  740. compose --no-hooks load "$odoo_service" $opt_database < "$ZIP_DUMP_LOCATION" || {
  741. err "Error restoring service ${DARKYELLOW}$odoo_service${NORMAL} to $msg_dbname database."
  742. exit 1
  743. }
  744. info "Successfully restored ${DARKYELLOW}$odoo_service${NORMAL} to $msg_dbname database."
  745. ## Restart odoo, ensure there is no bugs lingering on it.
  746. cmdline.spec:odoo:cmd:restart:run --service "$odoo_service" || exit 1
  747. }
  748. cmdline.spec.gnu dump
  749. cmdline.spec:odoo:cmd:dump:run() {
  750. : :posarg: DUMP_ZIPFILE 'Target path to store odoo dump zip file.'
  751. : :optval: --database,-d 'Target database (default if not specified)'
  752. : :optval: --service,-s "The service (defaults to 'odoo')"
  753. odoo_service="${opt_service:-odoo}"
  754. msg_dbname=default
  755. [ -n "$opt_database" ] && msg_dbname="'$opt_database'"
  756. compose --no-hooks save "$odoo_service" $opt_database > "$DUMP_ZIPFILE" || {
  757. err "Error dumping ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database to '$DUMP_ZIPFILE'."
  758. exit 1
  759. }
  760. info "Successfully dumped ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database to '$DUMP_ZIPFILE'."
  761. }
  762. cmdline.spec.gnu drop
  763. cmdline.spec:odoo:cmd:drop:run() {
  764. : :optval: --database,-d 'Target database (default if not specified)'
  765. : :optval: --service,-s "The service (defaults to 'odoo')"
  766. odoo_service="${opt_service:-odoo}"
  767. msg_dbname=default
  768. [ -n "$opt_database" ] && msg_dbname="'$opt_database'"
  769. compose --no-hooks drop "$odoo_service" $opt_database || {
  770. err "Error dropping ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database."
  771. exit 1
  772. }
  773. info "Successfully dropped ${DARKYELLOW}$odoo_service${NORMAL}'s $msg_dbname database."
  774. }
  775. cmdline.spec.gnu set-cyclos-url
  776. cmdline.spec:odoo:cmd:set-cyclos-url:run() {
  777. : :optval: --database,-d "Target database ('odoo' if not specified)"
  778. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  779. local URL
  780. dbname=${opt_database:-odoo}
  781. cyclos_service="${opt_service:-cyclos}"
  782. URL=$(compose:get_url "${cyclos_service}") || exit 1
  783. echo "UPDATE res_company SET cyclos_server_url = '$URL/api' WHERE id=1;" |
  784. compose:psql "$dbname" || {
  785. err "Failed to set cyclos url value in '$dbname' database."
  786. exit 1
  787. }
  788. }
  789. cmdline.spec.gnu cyclos
  790. cmdline.spec::cmd:cyclos:run() {
  791. :
  792. }
  793. cmdline.spec:cyclos:cmd:dump:run() {
  794. : :posarg: DUMP_GZFILE 'Target path to store odoo dump gz file.'
  795. : :optval: --database,-d "Target database ('cyclos' if not specified)"
  796. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  797. cyclos_service="${opt_service:-cyclos}"
  798. cyclos_database="${opt_database:-cyclos}"
  799. Wrap -d "stop container 'myc_${cyclos_service}_1'" -- \
  800. docker stop "myc_${cyclos_service}_1" || exit 1
  801. Wrap -d "Dump postgres database '${cyclos_database}'." -- \
  802. pgm cp "$cyclos_database" "$DUMP_GZFILE" || exit 1
  803. Wrap -d "start container 'myc_${cyclos_service}_1'." -- \
  804. docker start "myc_${cyclos_service}_1" || exit 1
  805. }
  806. cmdline.spec.gnu restore
  807. cmdline.spec:cyclos:cmd:restore:run() {
  808. : :posarg: GZ_DUMP_LOCATION 'Source cyclos dump file to restore
  809. (can be a local file or an url)'
  810. : :optval: --service,-s "The service (defaults to 'cyclos')"
  811. : :optval: --database,-d 'Target database (default if not specified)'
  812. local out
  813. cyclos_service="${opt_service:-cyclos}"
  814. cyclos_database="${opt_database:-cyclos}"
  815. if [[ "$GZ_DUMP_LOCATION" == "http://"* ]]; then
  816. settmpdir GZ_TMP_LOCATION
  817. tmp_location="$GZ_TMP_LOCATION/dump.gz"
  818. Wrap -d "get '$GZ_DUMP_LOCATION'" <<EOF
  819. curl -k -s -L "$GZ_DUMP_LOCATION" > "$tmp_location" || exit 1
  820. if [[ "\$(dd if="$tmp_location" count=2 bs=1 2>/dev/null |
  821. hexdump -v -e "/1 \"%02x\"")" != "1f8b" ]]; then
  822. err "Download doesn't seem to be a gzip file."
  823. dd if="$tmp_location" count=1 bs=256 | hd | prefix " ${GRAY}|${NORMAL} " >&2
  824. exit 1
  825. fi
  826. EOF
  827. GZ_DUMP_LOCATION="$tmp_location"
  828. fi
  829. [ -e "$GZ_DUMP_LOCATION" ] || {
  830. err "No file '$GZ_DUMP_LOCATION' found." >&2
  831. exit 1
  832. }
  833. Wrap -d "stop container 'myc_${cyclos_service}_1'" -- \
  834. docker stop "myc_${cyclos_service}_1" || exit 1
  835. ## XXXvlab: making the assumption that the postgres username should
  836. ## be the same as the cyclos service selected (which is the default,
  837. ## but not always the case).
  838. Wrap -d "restore postgres database '${cyclos_database}'." -- \
  839. pgm cp -f "$GZ_DUMP_LOCATION" "${cyclos_service}@${cyclos_database}" || exit 1
  840. ## ensure that the database is not locked
  841. ## XXXvlab: 70 is uid of user postgres, this avoids the docker bug
  842. ## but introduce hardwritten value
  843. Wrap -d "check and remove database lock if any" <<EOF
  844. echo "delete from database_lock;" | compose:psql "${cyclos_database}" || exit 1
  845. EOF
  846. Wrap -d "set root url" -- \
  847. cyclos:set_root_url "$cyclos_database" "$cyclos_service" || exit 1
  848. Wrap -d "start container 'myc_${cyclos_service}_1'." -- \
  849. docker start "myc_${cyclos_service}_1" || exit 1
  850. }
  851. cmdline.spec.gnu set-root-url
  852. cmdline.spec:cyclos:cmd:set-root-url:run() {
  853. : :optval: --database,-d "Target database ('cyclos' if not specified)"
  854. : :optval: --service,-s "The cyclos service name (defaults to 'cyclos')"
  855. local URL
  856. cyclos_database=${opt_database:-cyclos}
  857. cyclos_service="${opt_service:-cyclos}"
  858. Wrap -d "stop container 'myc_${cyclos_service}_1'" -- \
  859. docker stop "myc_${cyclos_service}_1" || exit 1
  860. Wrap -d "set root url" -- \
  861. cyclos:set_root_url "$cyclos_database" "$cyclos_service" || exit 1
  862. Wrap -d "start container 'myc_${cyclos_service}_1'." -- \
  863. docker start "myc_${cyclos_service}_1" || exit 1
  864. }
  865. cmdline::parse "$@"