Skip to content

Instantly share code, notes, and snippets.

@danbills
Last active April 26, 2019 01:21
Show Gist options
  • Save danbills/8a510c7ee27360b018cc97503eb3d5b1 to your computer and use it in GitHub Desktop.
Save danbills/8a510c7ee27360b018cc97503eb3d5b1 to your computer and use it in GitHub Desktop.
truncate a cloud sql database's general log
{
"truncateLogContext": {
"kind": "sql#truncateLogContext",
"logType": "MYSQL_GENERAL_TABLE"
}
}
#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