DEV Community

Cover image for How to Backup a Mongo DB In K8s.
Thodoris Velmachos
Thodoris Velmachos

Posted on

 

How to Backup a Mongo DB In K8s.

Hello, in this tutorial the goal is to describe the steps needed to backup a Mongo Database in a simple straight forward way, so I will leverage the use of the Kubernetes Job to perform this task.

The Commands for Mongo Backup/Restore is the following.

- mongodump --uri 'mongodb://root:$MONGODB_ROOT_PASSWORD@mongo-mongodb.default.svc.cluster.local:27017/org1?authSource=admin&ext.auth.askPassword=true' --gzip --archive > /data/dump_<db-name>.gz

- mongorestore --uri "mongodb://root:<password>@mongodb.default.svc.cluster.local:27017/<sdb-name>?authMechanism=SCRAM-SHA-256&authSource=admin"  --gzip --archive=/data/mongo/dump_<db-name>.gz
Enter fullscreen mode Exit fullscreen mode

So, Lets Start...

Step1. Provision a Persistent Volume.

cat << EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    app.kubernetes.io/name: pgdumpclient-pvc
  name: mongodumpclient-pvc
  namespace: default
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
  mongodumpclient: standard #GKE -- Depends on the Cloud Provider
  volumeMode: Filesystem
EOF
Enter fullscreen mode Exit fullscreen mode

Step2. Start a Kubernetes Job in which we are referencing the previously provision PVC (aka Persistent Volume Claim) and before starting the process we are changing the owner of the volume mount i.e "/data" in order the mongodump to be able to save the dump.

# Kubernetes Dump Job
cat << EOF | kubectl apply -f -
apiVersion: batch/v1
kind: Job
metadata:
  name: mongodump
  labels:
    app: mongodump
spec:
  template:
    spec:
      initContainers:
        - name: fix-data-dir-permissions
          image: alpine:3.16.2
          command:
            - chown
            - -R  
            - 1001:1001
            - /data
          volumeMounts:
            - name: mongodumpclient-pvc
              mountPath: /data
      containers:
        - name: mongodump
          image: docker.io/bitnami/mongodb:5.0.3 
          command: ["/bin/sh", "-ec", "sleep 120","mongodump --uri 'mongodb://root:$MONGODB_ROOT_PASSWORD@mongo-mongodb.default.svc.cluster.local:27017/org1?authSource=admin&ext.auth.askPassword=true' --gzip --archive > /data/dump_<db-name>.gz"] 
          imagePullPolicy: Always
          env:
            - name: MONGODB_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mongodb
                  key: mongodb-root-password
          volumeMounts:
            - name: mongodumpclient-pvc
              mountPath: /data
      restartPolicy: Never
      volumes:
        - name: mongodumpclient-pvc
          persistentVolumeClaim:
            claimName: mongodumpclient-pvc
  backoffLimit: 1
EOF
Enter fullscreen mode Exit fullscreen mode

Step3. Finally we are retrieving the MongoDB Dump file from the Pod.

kubectl cp mongodump-<random>:/data/dump_<db-name>.gz /<local-path>/
Enter fullscreen mode Exit fullscreen mode

Now In the New Cluster we will follow the same approach in order to restore the Mongo Database to the other MongoDB Instance.

I hope you like the tutorial, if you do give a thumps up! and follow me in Twitter, also you can subscribe to my Newsletter in order to avoid missing any of the upcoming tutorials.

Media Attribution

I would like to thank Clark Tibbs for designing the awesome photo I am using in my posts.

Top comments (0)

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git