fork 0k-charms
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.

126 lines
3.4 KiB

  1. #!/bin/bash
  2. ## Load action gets a first argument a DIRECTORY holding the necessary files.
  3. ##
  4. ##
  5. if [ -z "$SERVICE_DATASTORE" ]; then
  6. echo "This script is meant to be run through 'compose' to work properly." >&2
  7. exit 1
  8. fi
  9. usage="$exname [-h|--help] [--force|-f] DBNAME DEST_FILENAME"
  10. dbname=
  11. output=
  12. while [ "$1" ]; do
  13. case "$1" in
  14. "--help"|"-h")
  15. print_usage
  16. exit 0
  17. ;;
  18. "--force"|"-f")
  19. force=yes
  20. ;;
  21. --*|-*)
  22. err "Unexpected optional argument '$1'"
  23. print_usage
  24. exit 1
  25. ;;
  26. *)
  27. [ -z "$dbname" ] && { dbname=$1 ; shift ; continue ; }
  28. [ -z "$output" ] && { output=$1 ; shift ; continue ; }
  29. err "Unexpected positional argument '$1'"
  30. print_usage
  31. exit 1
  32. ;;
  33. esac
  34. shift
  35. done
  36. if [ -z "$dbname" ]; then
  37. err "You must provide a database name as first argument."
  38. print_usage
  39. exit 1
  40. fi
  41. if [ -z "$output" ]; then
  42. err "You must provide a destination filename name as second argument."
  43. print_usage
  44. exit 1
  45. fi
  46. if [ -e "$output" -a -z "$force" ]; then
  47. err "File '$output' exists already. Force overwrite with -f or --force."
  48. print_usage
  49. exit 1
  50. fi
  51. realpath=$(realpath "$output") || exit 1
  52. dirname="$(dirname "$realpath")" || exit 1
  53. basename=$(basename "$realpath") || exit 1
  54. host_path="$(get_host_path "$dirname")" || {
  55. die "Failed to find host path for local directory: $dirname"
  56. }
  57. set -e
  58. ## Ensure odoo is launched
  59. service_def=$(get_compose_service_def "$SERVICE_NAME")
  60. ## XXXvlab: should be moved to lib
  61. CONFIG=$SERVICE_CONFIGSTORE/etc/odoo-server.conf
  62. ADMIN_PASSWORD=$(echo "$service_def" | shyaml -q get-value options.admin-password) || {
  63. if [ -e "$CONFIG" ]; then
  64. ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g')
  65. fi
  66. if [ -z "$ADMIN_PASSWORD" ]; then
  67. err "Could not find 'admin-password' in $SERVICE_NAME service definition nor in config file."
  68. exit 1
  69. fi
  70. }
  71. containers="$(get_running_containers_for_service "$SERVICE_NAME")"
  72. if [ -z "$containers" ]; then
  73. err "No containers running for service $DARKYELLOW$SERVICE_NAME$NORMAL."
  74. die "Please ensure that $DARKYELLOW$SERVICE_NAME$NORMAL is running before using '$exname'."
  75. fi
  76. ## XXXvlab: taking first container is probably not a good idea
  77. container="$(echo "$containers" | head -n 1)"
  78. ## XXXvlab: taking first ip is probably not a good idea
  79. read-0 container_network container_ip < <(get_container_network_ip "$container")
  80. DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  81. debug docker run --rm --network "$container_network" \
  82. "-v" "$host_path:/tmp/work" \
  83. "$DEFAULT_CURL_IMAGE" \
  84. -X POST \
  85. -F "master_pwd=<hidden>" \
  86. -F "name=${dbname}" \
  87. -F "backup_format=zip" \
  88. -o "/tmp/work/$basename" \
  89. http://${container_ip}:8069/web/database/backup
  90. docker run --rm --network "$container_network" \
  91. "-v" "$host_path:/tmp/work" \
  92. "$DEFAULT_CURL_IMAGE" \
  93. -sS \
  94. -X POST \
  95. -F "master_pwd=${ADMIN_PASSWORD}" \
  96. -F "name=${dbname}" \
  97. -F "backup_format=zip" \
  98. -o "/tmp/work/$basename" \
  99. http://${container_ip}:8069/web/database/backup || {
  100. die "Querying odoo through curl was unsuccessfull."
  101. }
  102. info "Saved '$dbname' odoo database and filestore to '$output'."