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

Transfer can now push and pull databases and tables

parent 4d4ee26c
Loading
Loading
Loading
Loading
+104 −5
Original line number Diff line number Diff line
@@ -67,6 +67,29 @@ usage()
    printf " --help | -h)\n\n"
}

list_conns()
{
    # Structure: IP_ADDRESS:PORT:DB_NAME:USERNAME:PASSWORD
    conns=$(cat "$CONN_FILE" | xargs)

    while read conn; do
        name=$(echo "$conn" | cut -d' ' -f1)
        cinfo=$(echo "$conn" | cut -d' ' -f2)
        ip=$(echo "$cinfo" | cut -d':' -f1)
        port=$(echo "$cinfo" | cut -d':' -f2)
        db=$(echo "$cinfo" | cut -d':' -f3)
        user=$(echo "$cinfo" | cut -d':' -f4)
        pass=$(echo "$cinfo" | cut -d':' -f5)

        printf "%s%s%s:\n" "$GREEN" "$name" "$NORM"
        printf "\tIP Address:    %s\n" "$ip"
        printf "\tPort Number:   %s\n" "$port"
        printf "\tDatabase Name: %s\n" "$db"
        printf "\tUser Name:     %s\n" "$user"
        printf "\tPassword:      %s\n\n" "$pass"
    done < "$CONN_FILE"
}

create_source()
{
    # Structure: IP_ADDRESS:PORT:DB_NAME:USERNAME:PASSWORD
@@ -175,7 +198,6 @@ create_source()
    else
        exit 0
    fi

}

extract_conn()
@@ -186,6 +208,9 @@ extract_conn()
            conn=$(grep "$SERVER_NAME" "$CONN_FILE" | cut -d' ' -f2)
            # Structure: IP_ADDRESS:PORT:DB_NAME:USERNAME:PASSWORD
            SERVER_IP=$(echo "$conn" | cut -d':' -f1)
            if [ "$SERVER_IP" == "localhost" ] || [ "$SERVER_IP" == "127.0.0.1" ] || [ "$SERVER_IP" == "0.0.0.0" ]; then
                SERVER_IP="host.docker.internal"
            fi
            SERVER_PORT=$(echo "$conn" | cut -d':' -f2)
            SERVER_DB_NAME=$(echo "$conn" | cut -d':' -f3)
            SERVER_USER=$(echo "$conn" | cut -d':' -f4)
@@ -216,7 +241,12 @@ verify_source()
        extract_conn
        info "Source '$SERVER_NAME' successfully identified."
        printf "Would you like to proceed with with the following information:\n"
        printf "Server IP address:  '%s%s%s'\n" "$GREEN" "$SERVER_IP" "$NORM"
        printf "Server IP address:  '%s%s%s'" "$GREEN" "$SERVER_IP" "$NORM"
        if [ "$SERVER_IP" == "host.docker.internal" ]; then
            printf " %s(Please note, this script uses docker! host.docker.internal is in reference to your machine.)%s\n" "$RED" "$NORM"
        else
            printf "\n"
        fi
        printf "Server Port number: '%s%s%s'\n" "$GREEN" "$SERVER_PORT" "$NORM"
        printf "Database Name:      '%s%s%s'\n" "$GREEN" "$SERVER_DB_NAME" "$NORM"
        printf "Database User:      '%s%s%s'\n" "$GREEN" "$SERVER_USER" "$NORM"
@@ -232,6 +262,7 @@ verify_source()
    else
        warn "Failed to find source: '$SERVER_NAME'"
        create_source
        verify_source
    fi
}

@@ -291,6 +322,11 @@ parse_args()
                shift 1
                ;;

            connections)
                list_conns
                exit 0
                ;;

            *)
                warn "Unknown argument '$RED$1$NORM' exiting."
                exit 1
@@ -339,21 +375,84 @@ go()
    # -U "$DST_USR" -d "$DST_DB" -f "$SRC_TABLE".sql

    if [ "$PUSH" == 0 ]  && [ "$TABLE" == 1 ]; then # if we are pulling a table
        echo "pg_dump --host $SERVER_IP --port $SERVER_PORT --format plain --verbose --file $SOURCE_NAME.sql --table $SOURCE_NAME $SERVER_DB_NAME"
        echo "pg_dump --host $SERVER_IP --port $SERVER_PORT -U $SERVER_USER -d $SERVER_DB_NAME --verbose --table $SOURCE_NAME -Fp > $SOURCE_NAME.sql"

        docker run -i -e PGPASSWORD="$SERVER_PASS" postgres:alpine \
            pg_dump --host "$SERVER_IP" --port "$SERVER_PORT" -U "$SERVER_USER" -d "$SERVER_DB_NAME"\
            --verbose --table "$SOURCE_NAME" -Fp > "$SOURCE_NAME".sql

        if [ "$?" == 0 ]; then
            info "Successfully retrieved table dump for '$RED$SOURCE_NAME$NORM'"
            info "Successfully retrieved table dump for '$GREEN$SOURCE_NAME$NORM'"
        else
            warn "Failed to retrieve table dump for '$RED$SOURCE_NAME$NORM'"
            rm "$SOURCE_NAME".sql
        fi
    fi


    if [ "$PUSH" == 0 ]  && [ "$TABLE" == 0 ]; then # if we are pulling a database
        echo "pg_dump --host $SERVER_IP --port $SERVER_PORT -U $SERVER_USER -d $SOURCE_NAME --verbose --format=custom > $SOURCE_NAME$(date +%F).backup"

        docker run -i -e PGPASSWORD="$SERVER_PASS" postgres:alpine \
            pg_dump --host "$SERVER_IP" --port "$SERVER_PORT" -U "$SERVER_USER" -d "$SOURCE_NAME"\
            --verbose --format=custom > "$SOURCE_NAME"_"$(date +%F)".backup

        if [ "$?" == 0 ]; then
            info "Successfully retrieved database dump for '$GREEN$SOURCE_NAME$NORM'"
        else
            warn "Failed to retrieve database dump for '$RED$SOURCE_NAME$NORM'"
            rm "$SOURCE_NAME$(date +%F)".backup
        fi
    fi


    if [ "$PUSH" == 1 ]  && [ "$TABLE" == 0 ]; then # if we are pushing a database

        # Ensure we are using the correct file...
        file=$(ls -la ./"$SOURCE_NAME"*.*)
        printf "Would you continue the database restoration with the following file:\n%s%s%s\n" "$GREEN" "$file" "$NORM"
        read -rp "(y/n) " input
        if [ "$input" != "y" ]; then
            exit 0
        fi

        file=$(ls ./"$SOURCE_NAME"*.*)

        echo "pg_restore --host $SERVER_IP --port $SERVER_PORT -U $SERVER_USER -d $SERVER_DB_NAME --verbose --clean --if-exists $file"
        docker run -i -e PGPASSWORD="$SERVER_PASS" --mount type=bind,source="$(pwd)"/"$file",target=/"$file" postgres:alpine \
            pg_restore --host $SERVER_IP --port "$SERVER_PORT" -U "$SERVER_USER" -d "$SOURCE_NAME"\
            --verbose --clean --if-exists "$file"

        if [ "$?" == 0 ]; then
            info "Successfully pushed database dump for '$GREEN$SOURCE_NAME$NORM'"
        else
            warn "Failed to retrieve table dump for '$GREEN$SOURCE_NAME$NORM'"
            warn "Failed to push database dump for '$RED$SOURCE_NAME$NORM'"
        fi
    fi

    if [ "$PUSH" == 1 ]  && [ "$TABLE" == 1 ]; then # if we are pushing a table

        # Ensure we are using the correct file...
        file=$(ls -la ./"$SOURCE_NAME"*.*)
        printf "Would you continue the table restoration with the following file:\n%s%s%s\n" "$GREEN" "$file" "$NORM"
        read -rp "(y/n) " input
        if [ "$input" != "y" ]; then
            exit 0
        fi

        file=$(ls ./"$SOURCE_NAME"*.*)

        echo "pg_restore --host $SERVER_IP --port $SERVER_PORT -U $SERVER_USER -d $SERVER_DB_NAME --verbose --clean --if-exists $file"
        docker run -i -e PGPASSWORD="$SERVER_PASS" --mount type=bind,source="$(pwd)"/"$file",target=/"$file" postgres:alpine \
            psql --host $SERVER_IP --port "$SERVER_PORT" -U "$SERVER_USER" -d "$SERVER_DB_NAME"\
            -f "$file"

        if [ "$?" == 0 ]; then
            info "Successfully pushed table '$GREEN$SOURCE_NAME$NORM'"
        else
            warn "Failed to push table '$RED$SOURCE_NAME$NORM'"
        fi
    fi
}

banner