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

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, 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.

What is GEEK

Buddha Community

 How to Backup a Mongo DB In K8s.
Alisha  Larkin

Alisha Larkin

1624739940

5 ways to optimize your backup costs with Azure Backup

At Azure Backup, we’re committed to helping you optimize your backup costs. Over the last few months, we’ve introduced a comprehensive collection of features that not only gives you more visibility into your backup usage, but also helps you take action to achieve significant cost savings.

To help you get started with this journey, below are five steps you can take to optimize your backup costs, without needing to compromise on the safety of your data.

1. Clean up backups for your deleted resources

2. Optimize your backup policies

3. Take advantage of selective disk backup

4. Determine your required storage redundancy settings

5. Summary

#storage #backup & recovery #azure backup #azure #optimize your backup

Nest S with Mongo DB API development #21

Welcome to my channel Code Labs Javascript
Learn Geek stuff [Angular, React, Redux, Nodejs, Web development, Docker, AWS, Vue JS, All about Javascript]

Hi, I’m Tarun - a full-stack software developer based out of India. I build open-source projects and write about modern JavaScript, Node.js, design and web development. If you like my stuff Please subscribe My channel and Fell free to hit One-click unsubscribe anytime. I have 2000 youtube videos on latest technologies.

If you have any comments, ideas, critiques, or you just want to say hi, don’t hesitate to send me an email at tarun.softengg@gmail.com

#api #nest s #mongo #db api #development

Ysia Tamas

1620375354

Build and Deploy Spring Boot with Mongo DB App on Azure | Secure DB secrets with key vaults

You will learn how to build, run and deploy spring boot with MongoDB on Azure environment and how to secure data base secrets with azure key vaults.

Subscribe: https://www.youtube.com/channel/UC3hc8Ql0_OzMY7XI1gH9g8g/featured

#spring-boot #azure #mongo db

Node JS Blog APIs with Mongo DB #28

Welcome to my channel Code Labs Javascript
Learn Geek stuff [Angular, React, Redux, Nodejs, Web development, Docker, AWS, Vue JS, All about Javascript]
Hi, I’m Tarun - a full-stack software developer based out of India. I build open-source projects and write about modern JavaScript, Node.js, design and web development. If you like my stuff Please subscribe My channel and Fell free to hit One-click unsubscribe anytime. I have 2000 youtube videos on latest technologies.
If you have any comments, ideas, critiques, or you just want to say hi, don’t hesitate to send me an email at tarun.softengg@gmail.com

#api #node js #blog apis #mongo db