1) Launching a MongoDB client pod to connect to AWS DocumentDB
Using the following command
kubectl run -i --rm --tty mongo-client --image=mvertes/alpine-mongo --restart=Never --command -- /bin/bash
this will download the public key file into the current folder, next, if we run the following command from the same folder
mongo --ssl --host <hostname> --sslCAFile rds-combined-ca-bundle.pem --username <mongo-user> --password <mongo-password>
After running the previous command we should be connected to the AWS DocumentDB and see a prompt like this:
MongoDB shell version v3.4.2 connecting to: mongodb://<server>:27017/ MongoDB server version: 3.6.0 WARNING: shell and server versions do not match rs0:PRIMARY>
Then, we can just switch to the desired db using the following command:
rs0:PRIMARY> use <db-name>; switched to db <dn-name>
2) Launching a Mongo Dump client pod to to create a backup from DocumentDB
Using the following command
kubectl run -i --rm --tty mongodump-client --image=bigtruedata/mongodump --restart=Never --command -- /bin/sh
We are ready now to dump the database using the following command:
mongodump --ssl --host <host> --sslCAFile rds-combined-ca-bundle.pem --username <db-user> --password <db-password> --db <db-name>
At the end of the dump we will see a confirmation message like:
2019-08-14T20:22:46.547+0000 done dumping <db-name>.collectionX (12775 documents)
This should have created a new folder named
dump/ with all the dump data. This means we are ready to create a tar/gz file with that folder using the following command:
tar -zcvf db-dump.tar.gz dump
Now we have the tar/gz file with all the information:
ls -ltrh *.tar.gz -rw-r--r-- 1 root root 9.9M Aug 14 20:37 db-dump.tar.gz
Now without closing the current bash shell, open a new terminal tab and run the following command:
kubectl default/mongo-client3:/dump/db-dump.tar.gz /tmp
This will copy the backup
db-dump.tar.gz file into the tmp folder of the computer you are connected from
tmp (hopefully still inside a secure network)
At this point, we can run the same
cp command to copy to your second k8s environment, but we need to first launch a pod instance of the mongodump pod to be able to do a
mongorestore so let’s launch it.
Finally, we can exit the mongodump pod and it will be automatically deleted.
NOTE: At this point you should switch kubectl to point to the destination k8s cluster where you want your backup to be restored.
3) Launching a Mongo Dump client pod to restore the backup from DocumentDB
Using the following command we will launch a pod that will be used to restore the database.
kubectl run -i --rm --tty mongorestore-client --image=bigtruedata/mongodump --restart=Never --command -- /bin/sh
kubectl cp /tmp/db-dump.tar.gz default/mongorestore-client:/dump
After it finishes uploading the file into our pod we can now delete our local copy of the backup.
Now, we need to extract the tar/gz file using the following command
tar xvzf db-dump.tar.gz
This will extract all the contents of the tar/gz file into our
dump/ folder. We can check by running:
ls -ltrh total 10120 -rw-r--r-- 1 501 wheel 9.9M Aug 14 20:44 db-dump.tar.gz drwxr-xr-x 3 root root 29 Aug 14 21:00 dump
Finally we can run the restore:
mongorestore dump/ --ssl --host <host> --sslCAFile rds-combined-ca-bundle.pem --username <db-user> --password <db-password> --db <db-name>
Hans is a Senior Full Stack Engineer in a wide variety of fields including Business Applications and BPM projects. He’s always interested in integration projects, software design, and BPM trends. He has a strong background in the Java Enterprise world (more than 10 years). He also has been developing successful web apps using Node.js and RoR for the last couple of years. On the mobile front, he’s been focused on native iOS/Android development with more than 10 different apps in the App Store and Google Play currently running business transactions for happy customers.
He has a Bachelor’s degree in System Engineering and Project Management.