70 lines
1.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] [--with-postgis] [--with-unaccent] SOURCE DBNAME"
  10. dbname=
  11. postgis=
  12. while [ "$1" ]; do
  13. case "$1" in
  14. "--help"|"-h")
  15. print_usage
  16. exit 0
  17. ;;
  18. "--with-postgis")
  19. postgis=true
  20. ;;
  21. "--with-unaccent")
  22. unaccent=true
  23. ;;
  24. *)
  25. [ -z "$SOURCE" ] && { SOURCE=$1 ; shift ; continue ; }
  26. [ -z "$dbname" ] && { dbname=$1 ; shift ; continue ; }
  27. err "Unexpected argument '$1'."
  28. exit 1
  29. ;;
  30. esac
  31. shift
  32. done
  33. if [ -z "$SOURCE" ]; then
  34. err "You must provide a source file as first argument."
  35. print_usage
  36. exit 1
  37. fi
  38. if [ -z "$dbname" ]; then
  39. err "You must provide a destination database name as second argument."
  40. print_usage
  41. exit 1
  42. fi
  43. if [[ "$dbname" == *"@"* ]]; then
  44. IFS="@" read user dbname < <(echo "$dbname")
  45. fi
  46. . "$CHARM_PATH/lib/common"
  47. ## This can work only if ~/.pgpass is correctly created by init.
  48. set -e
  49. db_drop "$dbname"
  50. POSTGIS=$postgis UNACCENT=$unaccent db_create "$dbname"
  51. ddb "$dbname" > /dev/null < <(get_file "$SOURCE")
  52. [ "$user" ] &&
  53. db_grant_rights "$dbname" "$user"
  54. info "Loaded '$SOURCE' into database '$dbname'."