英文:
Automatically delete GCP Cloud SQL on demand backups
问题
我正在寻找一种方法,在经过x天后可以自动删除Cloud SQL数据库备份。我已找到使用gcloud命令创建按需备份的方法。但是否有办法自动删除GCP CloudSQL的按需备份?
我已使用以下命令创建按需备份,是否有一个标志可以设置保留此备份的天数?
gcloud sql backups create \ --async \ --instance=my-instance
英文:
I am looking for ways where I can automatically delete Cloud SQL database backups after x number of days. I have found ways to create on demand backup using gcloud command. But does someone know is there a way to automate deletion of on demand backups in GCP CloudSQL?
I have used the below command to create on demand backup is there a flag where i can set number of days to retain this backup?
gcloud sql backups create \
--async \
--instance=my-instance
答案1
得分: 2
这是一个删除特定日期备份的代码片段。可以修改它以便每天运行并删除指定天数之前的备份。
gcloud sql backups list --instance myinstance --sort-by WINDOW_START_TIME --filter 2023-06-06 --format="value(id)" | while read id; do gcloud sql backups delete --instance myinstance $id; done
输出:
备份将被删除。您无法撤销此操作。
您要继续吗(Y/n)?
正在删除备份运行...
....完成。
已删除备份运行 [1186013200000]。
英文:
Here is a code snippet that deletes backups for a specific date. It can be modified to run every day and delete X number of days behind.
gcloud sql backups list --instance myinstance --sort-by WINDOW_START_TIME --filter 2023-06-06 --format="value(id)" | while read id; do gcloud sql backups delete --instance myinstance $id; done
Output:
The backup will be deleted. You cannot undo this action.
Do you want to continue (Y/n)?
Deleting backup run...
....done.
Deleted backup run [1186013200000].
答案2
得分: 1
以下是翻译好的内容:
根据被接受的答案所提到的,我稍微修改了脚本,并成功删除了 x 天前创建的按需/自动备份。
使用下面的 shell 脚本来删除 7 天前创建的任何备份。
#!/bin/bash
current_date=$(date +%Y-%m-%d)
new_date=$(date -d "$current_date - 7 days" +%Y-%m-%d)
gcloud sql backups list --instance=my-instance --sort-by WINDOW_START_TIME --filter $new_date | awk 'NR>1{print $1}' | while read id; do gcloud sql backups delete --instance=my-instance $id; done
在此脚本中,DB 实例名称为 my-instance。
英文:
As mentioned by the accepted answer I have modified the script a little and was able to delete OnDemand/automated backups that was created x days ago.
Use the below shell script to delete any backup that was created 7 days ago
#!/bin/bash
current_date=$(date +%Y-%m-%d)
new_date=$(date -d "$current_date - 7 days" +%Y-%m-%d)
gcloud sql backups list --instance=my-instance --sort-by WINDOW_START_TIME --filter $new_date | awk 'NR>1{print $1}' | while read id; do gcloud sql backups delete --instance=my-instance $id; done
DB instance name is my-instance in this script.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论