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.

124 lines
2.7 KiB

  1. #!/bin/bash
  2. ## compose: no-hooks
  3. if [ -z "$SERVICE_DATASTORE" ]; then
  4. echo "This script is meant to be run through 'compose' to work properly." >&2
  5. exit 1
  6. fi
  7. version=0.1
  8. usage="$exname [-h|--help] [--force|-f] [DBNAME]"
  9. help="
  10. USAGE:
  11. $usage
  12. DESCRIPTION:
  13. Outputs to stdout the odoo zip dump of DBNAME. If DBNAME is not
  14. provided, it'll take the default odoo database from the
  15. ${DARKCYAN}postgres-database${NORMAL} relation of current service.
  16. EXAMPLES:
  17. $exname > dump.zip
  18. $exname odoo2 > odoo2.zip
  19. "
  20. dbname=
  21. output=
  22. while [ "$1" ]; do
  23. case "$1" in
  24. "--help"|"-h")
  25. print_help >&2
  26. exit 0
  27. ;;
  28. "--force"|"-f")
  29. force=yes
  30. ;;
  31. --*|-*)
  32. err "Unexpected optional argument '$1'"
  33. print_usage >&2
  34. exit 1
  35. ;;
  36. *)
  37. [ -z "$dbname" ] && { dbname=$1 ; shift ; continue ; }
  38. err "Unexpected positional argument '$1'"
  39. print_usage >&2
  40. exit 1
  41. ;;
  42. esac
  43. shift
  44. done
  45. if [ -z "$dbname" ]; then
  46. dbname=$(relation:get "$SERVICE_NAME:postgres-database" dbname) || {
  47. err "Couldn't retrieve information of" \
  48. "${DARKYELLOW}$SERVICE_NAME${NORMAL}-->${DARKCYAN}postgres-database${NORMAL}."
  49. exit 1
  50. }
  51. fi
  52. . $CHARM_PATH/lib/common
  53. set -e
  54. ADMIN_PASSWORD=$(odoo:get-admin-password) || {
  55. err "Couldn't retrieve admin password for $SERVICE_NAME."
  56. exit 1
  57. }
  58. container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 8069 4) || {
  59. err "Please ensure that $DARKYELLOW$SERVICE_NAME$NORMAL is running before using '$exname'."
  60. exit 1
  61. }
  62. container_ip=${container_network_ip##*:}
  63. container_network=${container_network_ip%%:*}
  64. DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  65. check_output() {
  66. local chars
  67. read -n 2 -r chars
  68. if [ "$chars" != "PK" ]; then
  69. out=$(cat)
  70. errmsg=$(echo "$out" | grep "alert-danger")
  71. errmsg=${errmsg#*>}
  72. errmsg=${errmsg%%<*}
  73. if [ -n "$errmsg" ]; then
  74. errmsg=$(echo "$errmsg" | recode html..utf8)
  75. die "$errmsg"
  76. fi
  77. err "Unexpected output not matching ZIP signature. Dump probably failed."
  78. exit 1
  79. fi
  80. {
  81. echo -n "$chars"
  82. cat
  83. }
  84. }
  85. cmd=(
  86. docker run --rm --network "$container_network"
  87. "$DEFAULT_CURL_IMAGE"
  88. -sS
  89. -X POST
  90. -F "master_pwd=${ADMIN_PASSWORD}"
  91. -F "name=${dbname}"
  92. -F "backup_format=zip"
  93. http://${container_ip}:8069/web/database/backup
  94. )
  95. ## XXXvlab: contains password, left only for advanced debug
  96. #debug "${cmd[@]}"
  97. set -o pipefail
  98. "${cmd[@]}" | check_output &&
  99. info "Requested odoo '$dbname' dump and outputted result to stdout."