Schedule MongoDB Backup to S3 using Kubernetes CronJob

Introduction

Get Started

mongo admin --host <hostname> --authenticationDatabase admin -u root
db.createUser({
user: 'backup_user',
pwd: 'oO9eV5cG6cF2oM1r',
roles: [{ role: 'backup',db:'admin'}]
})

Kubernetes Namespace

kubectl apply -f https://raw.githubusercontent.com/tuladhar/k8s-backup-mongodb/main/kubernetes/namespace.yaml
namespace/backup-mongodb created
kubectl config set-context --current --namespace=backup-mongodb

Kubernetes Secrets

export MONGODB_URI=mongodb://backup_user:oO9eV5cG6cF2oM1r@<mongodb-hostname>:27017kubectl create secret generic mongodb-uri --from-literal=MONGODB_URI=$MONGODB_URI

Store AWS credentials and S3 bucket URI

export AWS_ACCESS_KEY_ID=***
export AWS_SECRET_ACCESS_KEY=***
export BUCKET_URI=s3://bucket-name
export AWS_DEFAULT_REGION=us-east-1
kubectl create secret generic aws --from-literal=AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_IDkubectl create secret generic aws --from-literal=AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEYkubectl create secret generic aws --from-literal=BUCKET_URI=$BUCKET_URIkubectl create secret generic aws --from-literal=AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION

Deploy CronJob

kubectl apply -f https://raw.githubusercontent.com/tuladhar/k8s-backup-mongodb/main/kubernetes/cronjob.yaml
cronjob.batch/backup-mongodb created
kubectl edit cronjob backup-mongodb
Fig: Adjust the schedule
kubectl get cronjob
NAME             SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULEbackup-mongodb   0 */1 * * *   False     0        <none>
kubectl get jobs
pods=$(kubectl get pods --selector=job-name=<job-name> --output=jsonpath={.items[*].metadata.name})kubectl logs $pods

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store