Advertisement
Guest User

Untitled

a guest
Mar 8th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # define basic shell option`s
  4. #-----------------------------------------------------------------------------#
  5. #set -x # print trace of simple commands
  6. #set -v # print shell input lines as they are read
  7. set -e # stop script when command fails
  8. set -u # stop script on use undeclared variable
  9. set -f # disable filename expansion globbing
  10.  
  11. # define magic variables
  12. #-----------------------------------------------------------------------------#
  13. declare -r FILE_NAME=$(basename "$0")
  14. declare -r VERSION="v1.0.0"
  15. declare -r -i SUCCESS=0
  16. declare -r -i NO_ARGS=84
  17. declare -r -i BAD_ARGS=85
  18. declare -r -i MISSING_ARGS=86
  19.  
  20. # define communication functions
  21. #-----------------------------------------------------------------------------#
  22. function fc_no_args()
  23. {
  24. printf "Error: no arguments supplied\n"
  25. exit "$NO_ARGS"
  26. }
  27.  
  28. function fc_bad_args()
  29. {
  30. printf "Error: wrong arguments supplied\n"
  31. exit "$BAD_ARGS"
  32. }
  33.  
  34. function fc_missing_args()
  35. {
  36. printf "Error: missing arguments - \"%s\"\n" "$1"
  37. exit "$MISSING_ARGS"
  38. }
  39.  
  40. # check script arguments
  41. #-----------------------------------------------------------------------------#
  42. if [ "${#}" -eq 0 ]; then
  43. fc_no_args
  44. fi
  45.  
  46. while getopts "hVb:u:p:a:" G_OPTS; do
  47. case "$G_OPTS" in
  48. h)
  49. fc_help
  50. exit "$SUCCESS"
  51. ;;
  52. V)
  53. fc_version
  54. exit "$SUCCESS"
  55. ;;
  56. b)
  57. G_DATABASE_NAME="$OPTARG"
  58. ;;
  59. u)
  60. G_APPLICATION_USER="$OPTARG"
  61. ;;
  62. p)
  63. G_PASSWORD="$OPTARG"
  64. ;;
  65. a)
  66. G_POSTGRES_ADMIN="$OPTARG"
  67. ;;
  68. ?)
  69. fc_bad_args
  70. ;;
  71. esac
  72. done
  73.  
  74. # program functions
  75. #-----------------------------------------------------------------------------#
  76. function create_n_dump()
  77. {
  78. # create local variables
  79. local REPORT_USER="$G_APPLICATION_USER""_report"
  80. local DB_NAME="$G_APPLICATION_USER""_""$G_DATABASE_NAME""_1067"
  81. local SOLUTION_PATH="/home/solution/""$G_APPLICATION_USER"
  82.  
  83. # create needed users, roles and databases
  84. /usr/bin/psql -U "$G_POSTGRES_ADMIN" -c "CREATE USER $G_APPLICATION_USER CREATEDB LOGIN ENCRYPTED PASSWORD '$G_PASSWORD';" || true
  85. /usr/bin/psql -U "$G_POSTGRES_ADMIN" -c "CREATE ROLE $REPORT_USER LOGIN NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;" || true
  86. /usr/bin/psql -U "$G_POSTGRES_ADMIN" -c "CREATE DATABASE $G_APPLICATION_USER OWNER $G_APPLICATION_USER;" || true
  87. /usr/bin/psql -U "$G_POSTGRES_ADMIN" -c "CREATE DATABASE $DB_NAME OWNER $G_APPLICATION_USER;" || true
  88.  
  89. # create .pgpass file
  90. echo '*:*:*:"$G_APPLICATION_USER":"$G_PASSWORD"' > "$SOLUTION_PATH"/.pgpass
  91. chmod 0600 "$SOLUTION_PATH"/.pgpass
  92. chown "$G_APPLICATION_USER":"$G_APPLICATION_USER" "$SOLUTION_PATH"/.pgpass
  93.  
  94. # unzip and restore customer databases
  95. /bin/zcat /tmp/db_dump_recruitingapp_1067.sql.gz | /usr/bin/psql -U "$G_APPLICATION_USER" -d "$DB_NAME"
  96. }
  97.  
  98. function main()
  99. {
  100. create_n_dump
  101. }
  102.  
  103. # call default program/function flow
  104. #-----------------------------------------------------------------------------#
  105. main
  106.  
  107. # script exit
  108. #-----------------------------------------------------------------------------#
  109. exit "$SUCCESS"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement