Last active
April 26, 2019 01:21
-
-
Save danbills/8a510c7ee27360b018cc97503eb3d5b1 to your computer and use it in GitHub Desktop.
truncate a cloud sql database's general log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"truncateLogContext": { | |
"kind": "sql#truncateLogContext", | |
"logType": "MYSQL_GENERAL_TABLE" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Usage ./script sql_instance project | |
# Turn general log to OFF | |
gcloud sql instances patch $1 --database-flags general_log=OFF --project $2 | |
sleep 10 | |
# issue truncateLog command via gcloud | |
# https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/instances/truncateLog | |
opId=`curl -X POST https://www.googleapis.com/sql/v1beta4/projects/$2/instances/$1/truncateLog -H "Authorization:Bearer $(gcloud auth print-access-token)" --data-binary "@truncate_log.json" --header "Content-Type: application/json" | jq '.name'` | |
# wait for operation to finish | |
status=`gcloud beta sql operations wait --project $2 $opId --timeout=unlimited | jq '.[0].status'` | |
# if operation is successful, turn log back on | |
# Turn general log to FILE | |
if [ status -eq "DONE" ] then | |
gcloud sql instances patch $1 --database-flags general_log=FILE; | |
else | |
# ruh roh, better give the user some info | |
gcloud beta sql operations describe --project $2 $opId | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment