Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # define basic shell option`s
- #-----------------------------------------------------------------------------#
- #set -x # print trace of simple commands
- #set -v # print shell input lines as they are read
- set -e # stop script when command fails
- set -u # stop script on use undeclared variable
- set -f # disable filename expansion globbing
- # define magic variables
- #-----------------------------------------------------------------------------#
- declare -r FILE_NAME=$(basename "$0")
- declare -r VERSION="v1.0.0"
- declare -r -i SUCCESS=0
- declare -r -i NO_ARGS=84
- declare -r -i BAD_ARGS=85
- declare -r -i MISSING_ARGS=86
- # define communication functions
- #-----------------------------------------------------------------------------#
- function fc_no_args()
- {
- printf "Error: no arguments supplied\n"
- exit "$NO_ARGS"
- }
- function fc_bad_args()
- {
- printf "Error: wrong arguments supplied\n"
- exit "$BAD_ARGS"
- }
- function fc_missing_args()
- {
- printf "Error: missing arguments - \"%s\"\n" "$1"
- exit "$MISSING_ARGS"
- }
- # check script arguments
- #-----------------------------------------------------------------------------#
- if [ "${#}" -eq 0 ]; then
- fc_no_args
- fi
- while getopts "hVb:u:p:a:" G_OPTS; do
- case "$G_OPTS" in
- h)
- fc_help
- exit "$SUCCESS"
- ;;
- V)
- fc_version
- exit "$SUCCESS"
- ;;
- b)
- G_DATABASE_NAME="$OPTARG"
- ;;
- u)
- G_APPLICATION_USER="$OPTARG"
- ;;
- p)
- G_PASSWORD="$OPTARG"
- ;;
- a)
- G_POSTGRES_ADMIN="$OPTARG"
- ;;
- ?)
- fc_bad_args
- ;;
- esac
- done
- # program functions
- #-----------------------------------------------------------------------------#
- function create_n_dump()
- {
- # create local variables
- local REPORT_USER="$G_APPLICATION_USER""_report"
- local DB_NAME="$G_APPLICATION_USER""_""$G_DATABASE_NAME""_1067"
- local SOLUTION_PATH="/home/solution/""$G_APPLICATION_USER"
- # create needed users, roles and databases
- /usr/bin/psql -U "$G_POSTGRES_ADMIN" -c "CREATE USER $G_APPLICATION_USER CREATEDB LOGIN ENCRYPTED PASSWORD '$G_PASSWORD';" || true
- /usr/bin/psql -U "$G_POSTGRES_ADMIN" -c "CREATE ROLE $REPORT_USER LOGIN NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;" || true
- /usr/bin/psql -U "$G_POSTGRES_ADMIN" -c "CREATE DATABASE $G_APPLICATION_USER OWNER $G_APPLICATION_USER;" || true
- /usr/bin/psql -U "$G_POSTGRES_ADMIN" -c "CREATE DATABASE $DB_NAME OWNER $G_APPLICATION_USER;" || true
- # create .pgpass file
- echo '*:*:*:"$G_APPLICATION_USER":"$G_PASSWORD"' > "$SOLUTION_PATH"/.pgpass
- chmod 0600 "$SOLUTION_PATH"/.pgpass
- chown "$G_APPLICATION_USER":"$G_APPLICATION_USER" "$SOLUTION_PATH"/.pgpass
- # unzip and restore customer databases
- /bin/zcat /tmp/db_dump_recruitingapp_1067.sql.gz | /usr/bin/psql -U "$G_APPLICATION_USER" -d "$DB_NAME"
- }
- function main()
- {
- create_n_dump
- }
- # call default program/function flow
- #-----------------------------------------------------------------------------#
- main
- # script exit
- #-----------------------------------------------------------------------------#
- exit "$SUCCESS"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement