-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create mongo-reset.yaml cronjob in k8 infra
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
apiVersion: batch/v1 | ||
kind: CronJob | ||
metadata: | ||
name: reset-dev-mongo | ||
namespace: bt | ||
spec: | ||
schedule: "0 0 * * *" # Daily at 12 AM | ||
jobTemplate: | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: reset-mongo | ||
image: alpine/k8s | ||
command: | ||
- sh | ||
- -c | ||
- | | ||
set -e # Exit immediately if a command fails | ||
# Find stage and dev MongoDB pods | ||
stage_pod=$(kubectl get pods -n bt | grep 'bt-stage-mongo' | grep -o '^[^ ]*') | ||
dev_pod=$(kubectl get pods -n bt | grep 'bt-dev-mongo' | grep -o '^[^ ]*') | ||
# Dump staging MongoDB state | ||
echo "Dumping staging MongoDB state..." | ||
kubectl exec --namespace=bt \ | ||
"$stage_pod" -- mongodump --archive=/tmp/stage_backup.gz --gzip | ||
kubectl cp --namespace=bt \ | ||
"$stage_pod:/tmp/stage_backup.gz" /tmp/stage_backup.gz | ||
kubectl exec --namespace=bt \ | ||
"$stage_pod" -- rm /tmp/stage_backup.gz | ||
# Restore dump into dev MongoDB | ||
echo "Restoring dump into dev MongoDB..." | ||
kubectl cp --namespace=bt \ | ||
/tmp/stage_backup.gz "$dev_pod:/tmp/stage_backup.gz" | ||
kubectl exec --namespace=bt \ | ||
"$dev_pod" -- mongorestore --archive=/tmp/stage_backup.gz --gzip --drop | ||
kubectl exec --namespace=bt \ | ||
"$dev_pod" -- rm /tmp/stage_backup.gz | ||
# Cleanup local files | ||
rm /tmp/stage_backup.gz | ||
echo "MongoDB reset completed successfully!" | ||
restartPolicy: Never |