Back up and restore with MongoDB tools
Contents
Tested on: Ubuntu 22.04
MongoDB backup methods from official documentation:
- Back Up with Atlas
MongoDB Atlas, the hosted MongoDB service option in the cloud.
Back Up with MongoDB Cloud Manager or Ops Manager:
- https://www.mongodb.com/docs/manual/core/backups/#mms
- https://www.mongodb.com/docs/manual/core/backups/#ops-manager With Ops Manager, MongoDB subscribers can install and run the same core software that powers MongoDB Cloud Manager on their own infrastructure.
- Back Up by Copying Underlying Data Files
Let’s consider back up with mongodump!
Run docker containers with MongoDB on board and a container for our activities with this docker-compose.yml:
mkdir /tmp/mongocat << EOF > /tmp/mongo/docker-compose.yml
version: '3.1'
services:
mongo1:
image: mongo:6
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: toor
mongo-express1:
image: mongo-express:1.0.0-alpha.4
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: toor
ME_CONFIG_MONGODB_URL: mongodb://root:toor@mongo1:27017/
mongo2:
image: mongo:6
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: toor
mongo-express2:
image: mongo-express:1.0.0-alpha.4
restart: always
ports:
- 8082:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: toor
ME_CONFIG_MONGODB_URL: mongodb://root:toor@mongo2:27017/
EOFTry to connect to mongo1.mongo and mongo2.mongo:
docker exec -it -w /root mongo-node-1 bashapt update \
&& apt -y install wget curl gnupg2 software-properties-common apt-transport-https ca-certificates lsb-release inetutils-ping \
&& curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc| gpg --dearmor -o /etc/apt/trusted.gpg.d/mongodb-6.gpg \
&& echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-6.0.list \
&& apt update \
&& apt -y install mongodb-mongosh mongodb-org-toolsmongosh --norc --host 'mongo1.mongo:27017' --username root --password toor --authenticationDatabase=admin
# or
mongosh --norc --host 'mongo2:27017' --username root --password toor --authenticationDatabase=adminSome activities:
show dbs
use myshinynewdb
db.user.insert({name:'Taro',age:10,gender:'m'})
db.createCollection('collection1')-
Without ssl:
mongosh --norc \ --host '<FQDN of host 1 MongoDB>:27018,...,<FQDN of host N MongoDB>:27018' \ --username <database user name> \ --password <database user password> \ <database name> -
With ssl:
mongosh --norc \ --tls \ --tlsCAFile /home/<username>/.mongodb/root.crt \ --host '<FQDN of host 1 MongoDB>:27018,...,<FQDN of host N MongoDB>:27018' \ --username <database user name> \ --password <database user password> \ <database name>
Work with mongo
Create a collection
use animals
animals> db.createCollection('cats')
animals> db.createCollection('dogs')
animals> show collections
cats
dogsRename the collection
animals> db.createCollection('bird')
animals> show collections
bird
...
animals> db.bird.renameCollection('birds')
show collections
birds
...Delete the collection
animals> db.birds.drop()Create a collection - automatic method
animals> db.insects.insert({ name: 'cockroach' })
animals> db.insects.find()
[ { _id: ObjectId("64613df28befa5fc404ef60c"), name: 'cockroach' } ]Move a database
db.copyDatabase("db_to_rename","db_renamed","localhost")
use db_to_rename
db.db_to_rename.drop()mongodump -d old_db_name -o mongodump/
mongorestore -d new_db_name mongodump/old_db_name
db.old_db_name.drop()Import and restore:
It is convenient to work with JSON, but it does not retain all data formats. This means that you can get inconsistent data compared to BSON. A better way to back up and restore data is to use binary BSON.
Back up database
It is a good approach to shut down all connected databases with our MongoDB, if they exist. In my case, I shut down Neo4j and scaled the root component to 0 in my Kubernetes cluster.
For all dbs we can remove
--db animals
mkdir /var/backups/mongobackupsmongodump --host 'mongo1.mongo:27017' --username root --password toor --authenticationDatabase admin --db animals --out /var/backups/mongobackups/`date +"%m-%d-%y"`
2023-05-14T20:23:05.758+0000 writing animals.insects to /var/backups/mongobackups/05-14-23/animals/insects.bson
2023-05-14T20:23:05.759+0000 writing animals.dogs to /var/backups/mongobackups/05-14-23/animals/dogs.bson
2023-05-14T20:23:05.760+0000 done dumping animals.insects (1 document)
2023-05-14T20:23:05.760+0000 done dumping animals.dogs (0 documents)
2023-05-14T20:23:05.762+0000 writing animals.cats to /var/backups/mongobackups/05-14-23/animals/cats.bson
2023-05-14T20:23:05.763+0000 done dumping animals.cats (0 documents)Restore database
We’ll start by indicating the database name with the --nsInclude argument. We’ll be using newdb.* to restore all collections. To restore just one collection, like restaurants, use newdb.restaurants instead.
Using –drop, we’ll make sure that the target database is first dropped so that the backup is restored in a clean database.
mongorestore --host 'mongo2.mongo:27017' --username root --password toor --authenticationDatabase=admin --db animals --drop /var/backups/mongobackups/05-14-23/animals/
...
2023-05-14T20:29:12.061+0000 finished restoring animals.insects (1 document, 0 failures)
2023-05-14T20:29:12.065+0000 finished restoring animals.cats (0 documents, 0 failures)
2023-05-14T20:29:12.070+0000 finished restoring animals.dogs (0 documents, 0 failures)
2023-05-14T20:29:12.070+0000 no indexes to restore for collection animals.dogs
2023-05-14T20:29:12.070+0000 no indexes to restore for collection animals.insects
2023-05-14T20:29:12.070+0000 no indexes to restore for collection animals.cats
2023-05-14T20:29:12.070+0000 1 document(s) restored successfully. 0 document(s) failed to restore.Replication and Replica Sets of MongoDB
Usefull links: