Open 44084750 opened 1 month ago
#!/bin/bash
# Set default values for project ID and location
PROJECT_ID=${1:-"causal-hour-418204"}
LOCATION=${2:-"asia-east1"}
# Function to delete backups
delete_backups() {
local backup_plan=$1
backups=$(gcloud beta container backup-restore backups list --project=${PROJECT_ID} --location=${LOCATION} --backup-plan=${backup_plan} --format="value(name)")
for backup in ${backups}; do
# Check if the backup is locked
delete_lock_time=$(gcloud beta container backup-restore backups describe "${backup}" --project=${PROJECT_ID} --location=${LOCATION} --backup-plan=${backup_plan} --format="value(deleteLockDays)")
if [[ -n "${delete_lock_time}" ]]; then
echo "Skipping backup ${backup} as it is locked for deletion until ${delete_lock_time}"
continue
fi
echo "Deleting backup: ${backup}"
if gcloud beta container backup-restore backups delete ${backup} --project=${PROJECT_ID} --location=${LOCATION} --backup-plan=${backup_plan} --quiet; then
echo "Backup ${backup} deleted successfully"
else
echo "Failed to delete backup ${backup}"
fi
done
}
# Function to delete backup plans
delete_backup_plans() {
backup_plans=$(gcloud beta container backup-restore backup-plans list --project=${PROJECT_ID} --location=${LOCATION} --format="value(name)")
for backup_plan in ${backup_plans}; do
echo "Deleting backups in backup plan: ${backup_plan}"
delete_backups "${backup_plan}"
echo "Deleting backup plan: ${backup_plan}"
if gcloud beta container backup-restore backup-plans delete "${backup_plan}" --project=${PROJECT_ID} --location=${LOCATION} --quiet; then
echo "Backup plan ${backup_plan} deleted successfully"
else
echo "Failed to delete backup plan ${backup_plan}"
fi
done
}
# Function to delete restores
delete_restores() {
local restore_plan=$1
restores=$(gcloud beta container backup-restore restores list --project=${PROJECT_ID} --location=${LOCATION} --restore-plan=${restore_plan} --format="value(name)")
for restore in ${restores}; do
echo "Deleting restore: ${restore}"
if gcloud beta container backup-restore restores delete "${restore}" --project=${PROJECT_ID} --location=${LOCATION} --restore-plan=${restore_plan} --quiet; then
echo "Restore ${restore} deleted successfully"
else
echo "Failed to delete restore ${restore}"
fi
done
}
# Function to delete restore plans
delete_restore_plans() {
restore_plans=$(gcloud beta container backup-restore restore-plans list --project=${PROJECT_ID} --location=${LOCATION} --format="value(name)")
for restore_plan in ${restore_plans}; do
echo "Deleting restores in restore plan: ${restore_plan}"
delete_restores "${restore_plan}"
echo "Deleting restore plan: ${restore_plan}"
if gcloud beta container backup-restore restore-plans delete "${restore_plan}" --project=${PROJECT_ID} --location=${LOCATION} --quiet; then
echo "Restore plan ${restore_plan} deleted successfully"
else
echo "Failed to delete restore plan ${restore_plan}"
fi
done
}
# Prompt for confirmation before deleting resources
read -p "This script will delete all backups, backup plans, restores, and restore plans in the project ${PROJECT_ID} and location ${LOCATION}. Are you sure you want to proceed? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Deletion aborted"
exit 1
fi
# Execute the deletion
echo "Starting deletion of all backups, backup plans, restores, and restore plans"
delete_backup_plans
# delete_restore_plans
echo "Deletion complete"
gcloud CLI
gcloud services enable gkebackup.googleapis.com
Tips: 在 GKE 中,备份和还原是针对集群中的资源的状态和配置的。当你执行还原操作时,它会还原备份时的整个集群状态,包括所有的资源(如 Deployment、Pod 等)。然而,还原操作通常不会删除集群中已经存在的资源,而是会尝试将备份的状态与当前集群状态合并,可能会导致一些资源重复存在或冲突。
新建的 Deployment 在还原操作后仍然存在,这是因为还原操作通常不会删除现有的资源。如果你希望还原操作删除特定的资源,你可以在还原之前手动删除这些资源,或者在还原操作中使用适当的参数来指定删除现有资源的行为(如果支持的话)。
需要注意的是,并非所有资源都支持在还原操作中自动删除。有些资源可能需要手动处理。因此,在进行还原操作时,请确保仔细查看文档,并了解每种资源的还原行为。
我的场景:
Terraform