Cloudsql delete instance restore buckup - ghdrako/doc_snipets GitHub Wiki

Prevent delete instance

gcloud sql instances create [INSTANCE_NAME] --deletion-protection
gcloud sql instances patch [INSTANCE_NAME]  --no-deletion-protection

Delete instance

All data on an instance, including backups, is permanently lost when that instance is deleted. To preserve your data, export it to Cloud Storage before you delete it.

gcloud sql instances delete INSTANCE_NAME --project=PROJECT_ID

Create an on-demand backup

gcloud sql backups create --async --instance=INSTANCE_NAME [--location=BACKUP_LOCATION] [--description="opis"]

Export do bucketu - przed skasowaniem instancji

gcloud sql import sql INSTANCE URI [--async]
[--database=DATABASE, -d DATABASE] [--user=USER] [GCLOUD_WIDE_FLAG …]

Buckup list

gcloud sql backups list --instance INSTANCE_NAME
gcloud sql backups describe BACKUP_ID --instance INSTANCE_NAME

Restore from buckup after delete instance

- name: Restore snapshot of temp instance to staging instance
run: |
echo STAGING_RESTORE_OPERATION_NAME=$(curl -sS -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d '{"restoreBackupContext":{"backupRunId": ${{ env.TEMP_INSTANCE_LAST_BACKUP_ID }},"project": "${{ env.TEMP_INSTANCE_PROJECT }}","instanceId": "${{ env.TEMP_INSTANCE_NAME }}"}}' \
"https://sqladmin.googleapis.com/v1/projects/${{ env.STAGING_INSTANCE_PROJECT }}/instances/${{ env.STAGING_INSTANCE_NAME }}/restoreBackup" | jq -r .name) >> $GITHUB_ENV

- name: Wait for database restore staging instance operations to finish
run: gcloud sql operations wait ${{ env.STAGING_RESTORE_OPERATION_NAME }} --project=${{ env.STAGING_INSTANCE_PROJECT }} --timeout=1200 --quiet

Short time after delete instance is posible to restore from backup

gcloud sql backups list --instance=deleted-db-name --project your-project-name
gcloud sql backups restore <backup-ID> --restore-instance=new-db-from-scratch-name --project your-project
⚠️ **GitHub.com Fallback** ⚠️