#!/bin/bash
export AWS_DEFAULT_REGION=eu-central-1
SRC=                    # source - ip address or hostname db instance
USERNAME_SRC=           # source - login, need full access to all db
PW_SRC=                 # source - password ^^^ user
PORT=5432
INC=1

# you need create replication instance with name - replication-instance-1
arn_rep_ins=$(aws dms describe-replication-instances --filter="Name=replication-instance-id,Values=replication-instance-1" --query="ReplicationInstances[0].ReplicationInstanceArn" | tr -d '"')

# if you need migrate over 8 db, you need create two replication instance with name - replication-instance-2 and uncomment this line
#REP_INSTANCE_ARN2=$(aws dms describe-replication-instances --filter="Name=replication-instance-id,Values=replication-instance-2" --query="ReplicationInstances[0].ReplicationInstanceArn" | tr -d '"')

# valid values: full-load | cdc | full-load-and-cdc
MIGRATION_TYPE=full-load-and-cdc

DST=                    # destination - ip address or hostname db instance
USERNAME_DST=           # destination - login, need full access to all db
PW_DST=                 # destination - password ^^^ user

# db_list should contain a list of db that must be migrated
cat db_list | while read line
do
    array[i]="$line"

# if you need migrate over 8 db, uncomment this lines
#    if (( "$INC" >= 8 )); then
#        arn_rep_ins=$REP_INSTANCE_ARN2
#    else 
#        arn_rep_ins=$REP_INSTANCE_ARN1
#    fi

    echo "Create src endpoint"
    aws dms create-endpoint --endpoint-identifier source-endpoint-${array[i]//_/-} \
                            --endpoint-type source --engine-name postgres \
                            --username $USERNAME_SRC --password $PW_SRC \
                            --server-name $SRC --port $PORT \
                            --database-name ${array[i]}

    echo "Set source endpoint arn"
    source_endpoint_arn=$(aws dms describe-endpoints --filter="Name=endpoint-id,Values=source-endpoint-${array[i]//_/-}" --query="Endpoints[0].EndpointArn" | tr -d '"')

    echo "Test connection src endpoint"
    aws dms test-connection --replication-instance-arn ${arn_rep_ins} \
                            --endpoint-arn $source_endpoint_arn

    echo "Create dst endpoint"
    aws dms create-endpoint --endpoint-identifier dest-endpoint-${array[i]//_/-} \
                            --endpoint-type target \
                            --engine-name postgres \
                            --username $USERNAME_DST \
                            --password $PW_DST \
                            --server-name $DST \
                            --port $PORT --database-name ${array[i]}

    echo "Set dst endpoint arn"
    target_endpoint_arn=$(aws dms describe-endpoints --filter="Name=endpoint-id,Values=dest-endpoint-${array[i]//_/-}" --query="Endpoints[0].EndpointArn" | tr -d '"')
 
    echo "Test connection dst endpoint"
    aws dms test-connection --replication-instance-arn ${arn_rep_ins} \
                            --endpoint-arn $target_endpoint_arn

    while [ "$(aws dms describe-connections --filter "Name=endpoint-arn,Values=$source_endpoint_arn,$target_endpoint_arn" --query="Connections[0].Status" | tr -d '"')" = "testing" ]; do sleep 2; done;
    while [ "$(aws dms describe-connections --filter "Name=endpoint-arn,Values=$source_endpoint_arn,$target_endpoint_arn" --query="Connections[1].Status" | tr -d '"')" = "testing" ]; do sleep 2; done;

    echo "aws dms describe-connections --filter Name=endpoint-arn,Values=$source_endpoint_arn,$target_endpoint_arn --query=Connections[0].Status"
    result_test1=$(aws dms describe-connections --filter "Name=endpoint-arn,Values=$source_endpoint_arn,$target_endpoint_arn" --query="Connections[0].Status" | tr -d '"')
    echo "aws dms describe-connections --filter Name=endpoint-arn,Values=$source_endpoint_arn,$target_endpoint_arn --query=Connections[1].Status"
    result_test2=$(aws dms describe-connections --filter "Name=endpoint-arn,Values=$source_endpoint_arn,$target_endpoint_arn" --query="Connections[1].Status" | tr -d '"')
    echo -e "Test1 - $result_test1 \nTest2 - $result_test2"
    
    if [ $result_test1 = "successful" ] && [ $result_test2 = "successful" ]; then
        echo "Create a replication task"
        aws dms create-replication-task --replication-task-identifier task-"${array[i]//_/-}" \
                                        --source-endpoint-arn $source_endpoint_arn \
                                        --target-endpoint-arn $target_endpoint_arn \
                                        --replication-instance-arn ${arn_rep_ins} \
                                        --migration-type $MIGRATION_TYPE \
                                        --replication-task-settings 'file://task-settings.json' \
                                        --table-mappings 'file://table-mappings.json'
    
        echo "Check status task"
        status_repl_task=$(aws dms describe-replication-tasks --filters "Name=replication-task-id,Values=task-${array[i]//_/-}" --query="ReplicationTasks[0].Status"  | tr -d '"')
        
        echo Status - "$status_repl_task" 

        while [ "$(aws dms describe-replication-tasks --filters "Name=replication-task-id,Values=task-${array[i]//_/-}" --query="ReplicationTasks[0].Status" | tr -d '"')" != "ready" ]; do sleep 2; done;

        replication_task_arn=$(aws dms describe-replication-tasks --filters "Name=replication-task-id,Values=task-${array[i]//_/-}" --query="ReplicationTasks[0].ReplicationTaskArn" | tr -d '"')
        
        echo "Start replication task"
        aws dms start-replication-task --replication-task-arn $replication_task_arn \
                                       --start-replication-task-type start-replication
        
    else
    	echo "Test failed" 
    	exit 0;
    fi
    let i++
    let INC++
done

