Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- function refreshAccessToken() {
- echo "getting access token..."
- ACCESSTOKEN=`curl -s "http://metadata/computeMetadata/v1/instance/service-accounts/default/token" -H "X-Google-Metadata-Request: True" | jq ".access_token" | sed 's/"//g'`
- echo "retrieved access token $ACCESSTOKEN"
- }
- START=`date +%s%N`
- DB_INSTANCE=$1
- GCS_FILE=$2
- SLEEP_SECONDS=$3
- refreshAccessToken
- CURL_URL="https://www.googleapis.com/sql/v1beta1/projects/myproject/instances/$DB_INSTANCE/import"
- CURL_OPTIONS="-s --header 'Content-Type: application/json' --header 'Authorization: OAuth $ACCESSTOKEN' --header 'x-goog-project-id:myprojectId' --header 'x-goog-api-version:1'"
- CURL_PAYLOAD="--data '{ "importContext": { "database": "mydbname", "kind": "sql#importContext", "uri": [ "$GCS_FILE" ]}}'"
- CURL_COMMAND="curl --request POST $CURL_URL $CURL_OPTIONS $CURL_PAYLOAD"
- echo "executing $CURL_COMMAND"
- CURL_RESPONSE=`eval $CURL_COMMAND`
- echo "$CURL_RESPONSE"
- OPERATION=`echo $CURL_RESPONSE | jq ".operation" | sed 's/"//g'`
- echo "Import operation $OPERATION started..."
- CURL_URL="https://www.googleapis.com/sql/v1beta1/projects/myproject/instances/$DB_INSTANCE/operations/$OPERATION"
- STATE="RUNNING"
- while [[ $STATE == "RUNNING" ]]
- do
- echo "waiting for $SLEEP_SECONDS seconds for the import to finish..."
- sleep $SLEEP_SECONDS
- refreshAccessToken
- CURL_OPTIONS="-s --header 'Content-Type: application/json' --header 'Authorization: OAuth $ACCESSTOKEN' --header 'x-goog-project-id:myprojectId' --header 'x-goog-api-version:1'"
- CURL_COMMAND="curl --request GET $CURL_URL $CURL_OPTIONS"
- CURL_RESPONSE=`eval $CURL_COMMAND`
- STATE=`echo $CURL_RESPONSE | jq ".state" | sed 's/"//g'`
- END=`date +%s%N`
- ELAPSED=`echo "scale=8; ($END - $START) / 1000000000" | bc`
- echo "Import process $OPERATION for $GCS_FILE : $STATE, elapsed time $ELAPSED"
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement