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.

111 lines
3.0 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] SRC_FILE DBNAME"
  10. dbname=
  11. source=
  12. while [ "$1" ]; do
  13. case "$1" in
  14. "--help"|"-h")
  15. print_usage
  16. exit 0
  17. ;;
  18. --*|-*)
  19. err "Unexpected optional argument '$1'"
  20. print_usage
  21. exit 1
  22. ;;
  23. *)
  24. [ -z "$source" ] && { source=$1 ; shift ; continue ; }
  25. [ -z "$dbname" ] && { dbname=$1 ; shift ; continue ; }
  26. err "Unexpected positional argument '$1'"
  27. print_usage
  28. exit 1
  29. ;;
  30. esac
  31. shift
  32. done
  33. if [ -z "$source" ]; then
  34. err "You must provide a source filename name as first argument."
  35. print_usage
  36. exit 1
  37. fi
  38. if [ -z "$dbname" ]; then
  39. err "You must provide a database name as second argument."
  40. print_usage
  41. exit 1
  42. fi
  43. if ! [ -e "$source" ]; then
  44. err "File '$source' not found. Please provide an existing file as first argument."
  45. print_usage
  46. exit 1
  47. fi
  48. realpath=$(realpath "$source") || exit 1
  49. dirname="$(dirname "$realpath")" || exit 1
  50. basename=$(basename "$realpath") || exit 1
  51. host_path="$(get_host_path "$dirname")" || {
  52. die "Failed to find host path for local directory: $dirname"
  53. }
  54. set -e
  55. ## Ensure odoo is launched
  56. service_def=$(get_compose_service_def "$SERVICE_NAME")
  57. ADMIN_PASSWORD=$(echo "$service_def" | shyaml get-value options.admin-password) || {
  58. err "Could not find 'admin-password' in $SERVICE_NAME service definition."
  59. exit 1
  60. }
  61. containers="$(get_running_containers_for_service "$SERVICE_NAME")"
  62. if [ -z "$containers" ]; then
  63. err "No containers running for service $DARKYELLOW$SERVICE_NAME$NORMAL."
  64. die "Please ensure that $DARKYELLOW$SERVICE_NAME$NORMAL is running before using '$exname'."
  65. fi
  66. ## XXXvlab: taking first container is probably not a good idea
  67. container="$(echo "$containers" | head -n 1)"
  68. ## XXXvlab: taking first ip is probably not a good idea
  69. read-0 container_network container_ip < <(get_container_network_ip "$container")
  70. DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  71. debug docker run --network "$container_network" \
  72. "-v" "$host_path:/tmp/work" \
  73. "$DEFAULT_CURL_IMAGE" \
  74. -sS \
  75. -X POST \
  76. -F "master_pwd=<hidden>" \
  77. -F "backup_file=@/tmp/work/${source}" \
  78. -F "name=${dbname}" \
  79. http://${container_ip}:8069/web/database/restore
  80. docker run --network "$container_network" \
  81. "-v" "$host_path:/tmp/work" \
  82. "$DEFAULT_CURL_IMAGE" \
  83. -sS \
  84. -X POST \
  85. -F "master_pwd=${ADMIN_PASSWORD}" \
  86. -F "backup_file=@/tmp/work/${basename}" \
  87. -F "name=${dbname}" \
  88. http://${container_ip}:8069/web/database/restore >/dev/null || {
  89. die "Querying odoo through curl was unsuccessfull."
  90. }
  91. info "Restored '$source' odoo database and filestore to '$dbname'."