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.

118 lines
3.3 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. ## XXXvlab: should be moved to lib
  58. CONFIG=$SERVICE_CONFIGSTORE/etc/odoo-server.conf
  59. ADMIN_PASSWORD=$(echo "$service_def" | shyaml -q get-value options.admin-password) || {
  60. if [ -e "$CONFIG" ]; then
  61. ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g')
  62. fi
  63. if [ -z "$ADMIN_PASSWORD" ]; then
  64. err "Could not find 'admin-password' in $SERVICE_NAME service definition nor in config file."
  65. exit 1
  66. fi
  67. }
  68. containers="$(get_running_containers_for_service "$SERVICE_NAME")"
  69. if [ -z "$containers" ]; then
  70. err "No containers running for service $DARKYELLOW$SERVICE_NAME$NORMAL."
  71. die "Please ensure that $DARKYELLOW$SERVICE_NAME$NORMAL is running before using '$exname'."
  72. fi
  73. ## XXXvlab: taking first container is probably not a good idea
  74. container="$(echo "$containers" | head -n 1)"
  75. ## XXXvlab: taking first ip is probably not a good idea
  76. read-0 container_network container_ip < <(get_container_network_ip "$container")
  77. DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  78. debug docker run --rm --network "$container_network" \
  79. "-v" "$host_path:/tmp/work" \
  80. "$DEFAULT_CURL_IMAGE" \
  81. -sS \
  82. -X POST \
  83. -F "master_pwd=<hidden>" \
  84. -F "backup_file=@/tmp/work/${source}" \
  85. -F "name=${dbname}" \
  86. http://${container_ip}:8069/web/database/restore
  87. docker run --rm --network "$container_network" \
  88. "-v" "$host_path:/tmp/work" \
  89. "$DEFAULT_CURL_IMAGE" \
  90. -sS \
  91. -X POST \
  92. -F "master_pwd=${ADMIN_PASSWORD}" \
  93. -F "backup_file=@/tmp/work/${basename}" \
  94. -F "name=${dbname}" \
  95. http://${container_ip}:8069/web/database/restore >/dev/null || {
  96. die "Querying odoo through curl was unsuccessfull."
  97. }
  98. info "Restored '$source' odoo database and filestore to '$dbname'."