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.

119 lines
3.1 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. ADMIN_PASSWORD=$(echo "$service_def" | shyaml get-value options.admin-password) || {
  61. err "Could not find 'admin-password' in $SERVICE_NAME service definition."
  62. exit 1
  63. }
  64. containers="$(get_running_containers_for_service "$SERVICE_NAME")"
  65. if [ -z "$containers" ]; then
  66. err "No containers running for service $DARKYELLOW$SERVICE_NAME$NORMAL."
  67. die "Please ensure that $DARKYELLOW$SERVICE_NAME$NORMAL is running before using '$exname'."
  68. fi
  69. ## XXXvlab: taking first container is probably not a good idea
  70. container="$(echo "$containers" | head -n 1)"
  71. ## XXXvlab: taking first ip is probably not a good idea
  72. read-0 container_network container_ip < <(get_container_network_ip "$container")
  73. DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  74. debug docker run --network "$container_network" \
  75. "-v" "$host_path:/tmp/work" \
  76. "$DEFAULT_CURL_IMAGE" \
  77. -X POST \
  78. -F "master_pwd=<hidden>" \
  79. -F "name=${dbname}" \
  80. -F "backup_format=zip" \
  81. -o "/tmp/work/$basename" \
  82. http://${container_ip}:8069/web/database/backup
  83. docker run --network "$container_network" \
  84. "-v" "$host_path:/tmp/work" \
  85. "$DEFAULT_CURL_IMAGE" \
  86. -sS \
  87. -X POST \
  88. -F "master_pwd=${ADMIN_PASSWORD}" \
  89. -F "name=${dbname}" \
  90. -F "backup_format=zip" \
  91. -o "/tmp/work/$basename" \
  92. http://${container_ip}:8069/web/database/backup || {
  93. die "Querying odoo through curl was unsuccessfull."
  94. }
  95. info "Saved '$dbname' odoo database and filestore to '$output'."