Skip to content

Commit

Permalink
Create mongo-reset.yaml cronjob in k8 infra
Browse files Browse the repository at this point in the history
  • Loading branch information
klhftco authored Nov 21, 2024
1 parent 4f6167e commit 3b17508
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions infra/base/templates/mongo-reset.yaml
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

0 comments on commit 3b17508

Please sign in to comment.