How to Backup a Mongo DB In K8s.
Tags: devops, kubernetes, mongodb, operations
Published: 2022-11-11 19:39:33

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
```
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
```

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
```

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

```
kubectl cp mongodump-<random>:/data/dump_<db-name>.gz /<local-path>/
```

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](https://twitter.com/TVelmachos), also you can subscribe to my [Newsletter](https://dashboard.mailerlite.com/forms/167581/67759331736553243/share) in order to avoid missing any of the upcoming tutorials.

#### Media Attribution
I would like to thank [Clark Tibbs](https://unsplash.com/@clarktibbs) for designing the awesome [photo ](https://unsplash.com/photos/oqStl2L5oxI)I am using in my posts.