Commit b52ea080 authored by Hamaker, Alec's avatar Hamaker, Alec
Browse files

Removed pull_bologna.sh (Stability issues)

parent 4b2d4e14
Loading
Loading
Loading
Loading

src/scripts/pull_bologna.sh

deleted100755 → 0
+0 −386
Original line number Diff line number Diff line
#!/usr/bin/env bash

RED=$'\e\033[0;31m'
GREEN=$'\e\033[0;32m'
YELLOW=$'\e\033[1;33m'
ORANGE=$'\e\033[0;33m'
NORM=$'\e\033[0m'
FILE_NAME=''

SRC_HOST=''
SRC_PORT=''
SRC_USR=''
SRC_PW=''
SRC_DB=''
SRC_TABLE=''

SRC_SPECIFIED=0

DST_HOST=''
DST_PORT=''
DST_USR=''
DST_PW=''
DST_DB=''

DST_SPECIFIED=0

DST_FILE=''
SRC_FILE=''

banner()
{
    echo "
    ${ORANGE}     .^!77???77!~^::.                                                          
        :7??????????????77!!~^^^:..                                                
        77777????????????????${YELLOW}YPPPP5YYJ?${ORANGE}7!^:::.........          .......            
        !!!!77777????????????${YELLOW}PPPPP555YPPJ${ORANGE}?????7777?!:.......~!:....:::~?!:         
        .~!!!!!!!777777?????${YELLOW}YPJJJY5YPJJY7${ORANGE}???????7?!         ~.        !!           
         .^~!!!!!!!!!!777777Y5?${YELLOW}P5PGPBYYY${ORANGE}?????????7.        ^.        ^!   .        
            .^~~!!!!!!!!!!!!?5?${YELLOW}555JJYJ5J7${ORANGE}????????!        :7.       .?~   .        
                ..:^~~~~~~~~!${YELLOW}Y5Y55PPPPPJ${ORANGE}!!!!!!!!!!!~~~~~~~!7!~~~~~~~!7!!!~:!!~~~^  
              ${YELLOW}:~7?JYY555YYYJJYGGPPPPPPPY!!!!!!!!!!777777777777777777!!!!!!!!!!!^.  
             ?GGGGGGGGGGGGGGGGGGGGGGGGGGGGGP5PGGGGGGGGGGGGGGGGGGGGGGPPP55YYJ?~:    
             YPP5555555555PPPPPPPPPPPPP5PP5?75PPPPPPPPPPPPPPGGGGGGGGGGGGGBBBBGP5?~ 
             .!?JJY5555555YY555555555555555PP555555555555555555555555PPPPPP555PPPGY
                  .:^~!?!:.77!!??!Y5YJ??777777????JJJJYYYYY5555Y77J777Y7!J5555555J~
                           ${NORM}~7!!7^ ^~:                  ....:::^. .7!~!7: .!!!!~^:  
                            :::.                                  .^^^.            "
    echo "$ORANGE"
    printf "                       ___                       __  __\n"
    printf "                      / _ \\ ___  ___ __ _ _ __  |  \\/  | __ _ _   _  ___ _ __\n"
    printf "                     | | | / __|/ __/ _\` | '__| | |\\/| |/ _\` | | | |/ _ \\ '__|\n"
    printf "                     | |_| \__ \ (_| (_| | |    | |  | | (_| | |_| |  __/ |\n"
    printf "                      \\___/|___/\\___\\__,_|_|    |_|  |_|\\__,_|\\__, |\\___|_|\n"
    printf "                                                              |___/\n"
    printf "                        My Bologna has a first name, it's O-S-C-A-R."
    echo "$NORM"
}

banner

src_not_specified()
{
    printf "%sSource not specified missing '$1'%s\n" "$RED" "$NORM"
}

dst_not_specified()
{
    printf "%sDestination not specified missing '$1'%s\n" "$RED" "$NORM"
}

src_specified()
{
    if [ "$SRC_DB" != "" ]; then
        if [ "$SRC_PW" != "" ]; then
            if [ "$SRC_USR" != "" ]; then
                if [ "$SRC_HOST" != "" ]; then
                    if [ "$SRC_PORT" != "" ]; then
                        if [ "$SRC_TABLE" != "" ]; then
                            SRC_SPECIFIED=1
                        else
                            SRC_SPECIFIED=0
                            src_not_specified "Table"
                        fi
                    else
                        SRC_SPECIFIED=0
                        src_not_specified "Port"
                    fi
                else
                    SRC_SPECIFIED=0
                    src_not_specified "Host"
                fi
            else
                SRC_SPECIFIED=0
                src_not_specified "User"
            fi
        else
            SRC_SPECIFIED=0
            src_not_specified "Password"
        fi
    else
        SRC_SPECIFIED=0
        src_not_specified "Database"
    fi
}

dst_specified()
{
    if [ "$DST_DB" != "" ]; then
        if [ "$DST_PW" != "" ]; then
            if [ "$DST_USR" != "" ]; then
                if [ "$DST_HOST" != "" ]; then
                    if [ "$DST_PORT" != "" ]; then
                        DST_SPECIFIED=1
                    else
                        DST_SPECIFIED=0
                        dst_not_specified "Port"
                    fi
                else
                    DST_SPECIFIED=0
                    dst_not_specified "Host"
                fi
            else
                DST_SPECIFIED=0
                dst_not_specified "User"
            fi
        else
            DST_SPECIFIED=0
            dst_not_specified "Password"
        fi
    else
        DST_SPECIFIED=0
        dst_not_specified "Database"
    fi
}

usage()
{
    printf "%susage:%s" "$RED" "$NORM"
    printf "./pull_bologna.sh\n\n"

    printf "Print this help message.\n"
    printf " --help | -h)\n\n"

    printf "Specfifies connection information for the source.\n"
    printf "\t --source-host | -sh | --src-host)\n"
    printf "\t --source-port | -sp | --src-port)\n"
    printf "\t --source-user | -su | --src-user | --src-usr)\n"
    printf "\t --source-password | --source-pass | --source-pw | --src-pw | -spw)\n"
    printf "\t --source-db | --source-data-base | --src-db | -sd)\n"
    printf "\t --source-table | -st | --src-table)\n\n"


    printf "Specfifies connection information for the destination.\n"
    printf "\t --destination-host | -dh | --dst-host)\n"
    printf "\t --destination-port | -dp | --dst-port)\n"
    printf "\t --destination-user | -du | --dst-user | --dst-usr)\n"
    printf "\t --destination-password | --destination-pass | --destination-pw | --dst-pw | -dpw)\n"
    printf "\t --destination-db | --destination-data-base | --dst-db | -dd)\n\n"

    printf "Use a connection file for connection information. If a connection\n"
    printf "file is not found by the specified name, pull_bologna will\n"
    printf "create one for you.\n"
    printf "\t--source | -s)\n"
    printf "\t--destination | -d\n"
}

parse_src_connection_file()
{
    SRC_HOST=$(awk 'BEGIN {FS=":"}; {print $1}' "$SRC_FILE")
    SRC_PORT=$(awk 'BEGIN {FS=":"}; {print $2}' "$SRC_FILE")
    SRC_DB=$(awk 'BEGIN {FS=":"}; {print $3}' "$SRC_FILE")
    SRC_USR=$(awk 'BEGIN {FS=":"}; {print $4}' "$SRC_FILE")
    SRC_PW=$(awk 'BEGIN {FS=":"}; {print $5}' "$SRC_FILE")
}

parse_dst_connection_file()
{
    DST_HOST=$(awk 'BEGIN {FS=":"}; {print $1}' "$DST_FILE")
    DST_PORT=$(awk 'BEGIN {FS=":"}; {print $2}' "$DST_FILE")
    DST_DB=$(awk 'BEGIN {FS=":"}; {print $3}' "$DST_FILE")
    DST_USR=$(awk 'BEGIN {FS=":"}; {print $4}' "$DST_FILE")
    DST_PW=$(awk 'BEGIN {FS=":"}; {print $5}' "$DST_FILE")
}

parse_args()
{
    ARGS=()
    while test $# -gt 0
    do
        case "$1" in
            --help | -h)
                usage
                exit 0
                ;;
            --source-host | -sh | --src-host)
                shift
                SRC_HOST="$1"
                ;;
            --source-port | -sp | --src-port)
                shift
                SRC_PORT="$1"
                ;;
            --source-user | -su | --src-user | --src-usr)
                shift
                SRC_USR="$1"
                ;;
            --source-password | --source-pass | --source-pw | --src-pw | -spw)
                shift
                SRC_PW="$1"
                ;;
            --source-db | --source-data-base | --src-db | -sd)
                shift
                SRC_DB="$1"
                ;;
            --source-table | -st | --src-table)
                shift
                SRC_TABLE="$1"
                ;;


            --source | -s)
                shift

                SRC_FILE="bin/"$(ls -la bin | awk '{print $NF}' |\
                    grep -i "^\..*$1" | head -n 1)
                if [ ! -f "$SRC_FILE" ]; then
                    printf "%sSource connection file '%s' could not be found.%s\n"\
                        "$RED" "$1" "$NORM"
                    read -p \
                        "Create a new connection file named '.$1.conn'?(y/n)"\
                        input
                    if [ $(echo "$input" | tr '[:upper:]' '[:lower:]') == "n" ]; then
                        exit 0
                    fi
                    echo "host:port:database:user:password" > bin/.$1.conn
                    "${EDITOR:-vi}" "bin/.$1.conn"
                    SRC_FILE="bin/.$1.conn"
                fi
                parse_src_connection_file
                ;;

            --destination | -d)
                shift
                DST_FILE="bin/"$(ls -la bin | awk '{print $NF}' |\
                    grep -i "^\..*$1" | head -n 1)
                if [ ! -f "$DST_FILE" ]; then
                    printf "%sDestination connection file '%s' could not be found.%s\n"\
                        "$RED" "$1" "$NORM"
                    read -p \
                        "Create a new connection file named '.$1.conn'?(y/n)"\
                        input
                    if [ $(echo "$input" | tr '[:upper:]' '[:lower:]') == "n" ]; then
                        exit 0
                    fi
                    echo "host:port:database:user:password" > bin/.$1.conn
                    "${EDITOR:-vi}" "bin/.$1.conn"
                    DST_FILE="bin/.$1.conn"
                fi
                parse_dst_connection_file
                ;;


            --destination-host | -dh | --dst-host)
                shift
                DST_HOST="$1"
                ;;
            --destination-port | -dp | --dst-port)
                shift
                DST_PORT="$1"
                ;;
            --destination-user | -du | --dst-user | --dst-usr)
                shift
                DST_USR="$1"
                ;;
            --destination-password | --destination-pass | --destination-pw | --dst-pw | -dpw)
                shift
                DST_PW="$1"
                ;;
            --destination-db | --destination-data-base | --dst-db | -dd)
                shift
                DST_DB="$1"
                ;;

            --* | -*)
                echo "$RED"Option \""$1"\" not recognized"$NORM"
                exit 1
                ;;
            *)
                ARGS+=("$1")
                ;;
        esac
        shift
    done
    # if there are no args
    if [ "${#ARGS[@]}" -eq 0 ]; then
        if [ "$SRC_TABLE" != "" ]; then
            ARGS+=("$SRC_TABLE")
        else
            src_not_specified "Source Table"
            usage
        fi
    fi
    for arg in "${ARGS[@]}"; do
        go "$arg"
    done
}

go()
{
    # call src_specified to make sure the source has been specified.
    src_specified &> /dev/null
    if [ "$SRC_SPECIFIED" -eq 1 ]; then
        printf "%sSource connection information found.%s\n" "$GREEN" "$NORM"
        printf "Continue with following connection information:\n"
        printf "\tSource Host:     %s\n" "$SRC_HOST"
        printf "\tSource Port:     %s\n" "$SRC_PORT"
        printf "\tSource User:     %s\n" "$SRC_USR"
        printf "\tSource Database: %s\n" "$SRC_DB"
        printf "\tSource Table:    %s\n" "$SRC_TABLE"
        read -p "(y/n)" input
        if [ $(echo "$input" | tr '[:upper:]' '[:lower:]') == "n" ]; then
            exit 0
        fi
    else
        printf "%sMissing source connection information.%s\n" "$RED" "$NORM"
        printf "\tSource Host: %s\n" "$SRC_HOST"
        printf "\tSource Port: %s\n" "$SRC_PORT"
        printf "\tSource User: %s\n" "$SRC_USR"
        printf "\tSource Database: %s\n" "$SRC_DB"
        printf "\tSource Table: %s\n" "$SRC_TABLE"
        exit 3

    fi
    for i in "$ARGS"; do
        FILE_NAME=$1
        # if the sql file does not already exist...
        if [ ! -f "$1".sql ]; then
            pid=$!
            i=1
            sp="/-\|"
            echo -n ''
            echo "${YELLOW}Pulling Table $1...${NORM}  "
            while ps -p $pid > /dev/null
            do
                printf "\b${sp:i++%${#sp}:1}"
                sleep .25
            done

            if [ -f "$1".sql ]; then
                echo "${GREEN}Dump aquired file '$1.sql'${NORM}"
            else
                echo "${RED}Dump FAILED to aquired file '$1.sql'${NORM}"
                exit 1
            fi

        fi

        # call to see if the dst is specified
        dst_specified &> /dev/null
        if [ "$DST_SPECIFIED" -eq 1 ]; then
            export PGPASSWORD="$DST_PW"
            psql -h "$DST_HOST" -p "$DST_PORT"\
                -U "$DST_USR" -d "$DST_DB" -f "$SRC_TABLE".sql
        else
            printf "%sMissing desination connection information.%s\n" "$RED" "$NORM"
            printf "\tDestination Host: %s\n" "$DST_HOST"
            printf "\tDestination Port: %s\n" "$DST_PORT"
            printf "\tDestination User: %s\n" "$DST_USR"
            printf "\tDestination Database: %s\n" "$DST_DB"
            printf "\tDestination Table: %s\n" "$DST_TABLE"
            exit 4
        fi

        shift
    done
}

if [ "$#" -eq 0 ]; then
    usage
else
    parse_args "$@"
fi