Backup e restore de databases MongoDB

Dando continuidade ao último artigo sobre MongoDB, uma parte importantíssima da administração do cluster do banco é garantir seus dados com backup’s e restores. Com o uso cada vez mais frequente é comum com o passar do tempo que o tamanho dos bancos e das collections aumentem e consequentemente a preocupação com os dados em caso de falhas, o que sempre nos obriga a ter um backup na manga, principalmente banco de dados…igual seguro, melhor pagar e não precisar do que precisar e não ter…ou como dizem por ae…”quem tem dois tem um quem tem um não tem nenhum!”

Para tentar nos prevenir desses eventos o mongodb disponibiliza os seguintes utilitários:

mongodump;
mongorestore;

Para este hand’s on inicial vamos utilizar os SampleCollections desse repositório:
https://github.com/SouthbankSoftware/dbkoda-data

Com o nosso cluster MongoDB up e running:

[root@mongo01 ~]# git clone git@github.com:SouthbankSoftware/dbkoda-data.git
[root@mongo01 ~]# tar czvf mongo_dbs.tar.gz *
a OrderExample
a OrderExample/dump
a OrderExample/dump/OrderExample
a OrderExample/dump/OrderExample/products.metadata.json.gz
a OrderExample/dump/OrderExample/customers.bson.gz
a OrderExample/dump/OrderExample/embeddedOrders.bson.gz
a OrderExample/dump/OrderExample/orders.metadata.json.gz
a OrderExample/dump/OrderExample/orders.bson.gz
a OrderExample/dump/OrderExample/customers.metadata.json.gz
a OrderExample/dump/OrderExample/lineitems.bson.gz
a OrderExample/dump/OrderExample/lineitems.metadata.json.gz
a OrderExample/dump/OrderExample/embeddedOrders.metadata.json.gz
a OrderExample/dump/OrderExample/products.bson.gz



Logamos no mongo e autenticamos:

[root@mongo01 ~]# mongo
MongoDB shell version v3.6.17
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b3eea35a-eff5-4462-aa48-0244c0e95c79") }
MongoDB server version: 3.6.17
mongo_lab:PRIMARY>

mongo_lab:PRIMARY> use admin;
switched to db admin

mongo_lab:PRIMARY> db.auth( "tadeuadmin", "#Senhalinda2000");
1

Para esse exemplo vamos criar um outro usuário e atrelar a role de restore para ele que é a que vamos precisar nesse caso.

mongo_lab:PRIMARY> db.createUser({
... user: 'restoreuser',
... pwd: 'restorepwd',
... roles: ['restore']
... });
Successfully added user: { "user" : "restoreuser", "roles" : [ "restore" ] }

E vamos mandar bala com o comando:

# mongorestore --host mongo01 --username restoreuser --password restorepwd --authenticationDatabase admin --db teste /root/SampleCollections/dump/SampleCollections/
[root@mongo01 mongodb]# mongorestore --host mongo01 --username restoreuser --password restorepwd --authenticationDatabase admin --db teste /root/SampleCollections/dump/SampleCollections/
2018-06-20T19:30:32.763+0000 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2018-06-20T19:30:32.763+0000 building a list of collections to restore from /root/SampleCollections/dump/SampleCollections dir
2018-06-20T19:30:32.781+0000 reading metadata for teste.DBEnvyLoad_customers from /root/SampleCollections/dump/SampleCollections/DBEnvyLoad_customers.metadata.json
2018-06-20T19:30:32.782+0000 reading metadata for teste.DBEnvyLoad_products from /root/SampleCollections/dump/SampleCollections/DBEnvyLoad_products.metadata.json
2018-06-20T19:30:32.783+0000 reading metadata for teste.Sakila_customers from /root/SampleCollections/dump/SampleCollections/Sakila_customers.metadata.json
2018-06-20T19:30:32.784+0000 restoring teste.and_sorted_test from /root/SampleCollections/dump/SampleCollections/and_sorted_test.bson
2018-06-20T19:30:32.831+0000 restoring teste.DBEnvyLoad_customers from /root/SampleCollections/dump/SampleCollections/DBEnvyLoad_customers.bson
2018-06-20T19:30:32.880+0000 restoring teste.DBEnvyLoad_products from /root/SampleCollections/dump/SampleCollections/DBEnvyLoad_products.bson
2018-06-20T19:30:32.918+0000 restoring teste.Sakila_customers from /root/SampleCollections/dump/SampleCollections/Sakila_customers.bson
2018-06-20T19:30:33.277+0000 no indexes to restore
2018-06-20T19:30:33.277+0000 finished restoring teste.Sakila_customers (599 documents)
2018-06-20T19:30:33.277+0000 reading metadata for teste.crunchbase_database from /root/SampleCollections/dump/SampleCollections/crunchbase_database.metadata.json
2018-06-20T19:30:33.431+0000 restoring teste.crunchbase_database from /root/SampleCollections/dump/SampleCollections/crunchbase_database.bson
2018-06-20T19:30:33.951+0000 no indexes to restore
2018-06-20T19:30:33.951+0000 finished restoring teste.crunchbase_database (1000 documents)



2018-06-20T19:30:51.188+0000 [########################] teste.DBEnvyLoad_customers 26.8MB/26.8MB (100.0%)
2018-06-20T19:30:51.188+0000 no indexes to restore
2018-06-20T19:30:51.188+0000 finished restoring teste.DBEnvyLoad_customers (100000 documents)
2018-06-20T19:30:53.747+0000 [######………………] teste.and_sorted_test 10.9MB/37.8MB (28.8%)
2018-06-20T19:30:56.741+0000 [#########……………] teste.and_sorted_test 14.8MB/37.8MB (39.2%)
2018-06-20T19:30:59.744+0000 [############…………] teste.and_sorted_test 19.1MB/37.8MB (50.6%)
2018-06-20T19:31:02.740+0000 [##############……….] teste.and_sorted_test 22.8MB/37.8MB (60.4%)
2018-06-20T19:31:05.741+0000 [################……..] teste.and_sorted_test 26.6MB/37.8MB (70.3%)
2018-06-20T19:31:08.741+0000 [###################…..] teste.and_sorted_test 30.6MB/37.8MB (80.9%)
2018-06-20T19:31:11.748+0000 [#####################…] teste.and_sorted_test 34.2MB/37.8MB (90.6%)
2018-06-20T19:31:14.467+0000 [########################] teste.and_sorted_test 37.8MB/37.8MB (100.0%)
2018-06-20T19:31:14.467+0000 no indexes to restore
2018-06-20T19:31:14.467+0000 finished restoring teste.and_sorted_test (900000 documents)
2018-06-20T19:31:14.467+0000 done

Repare que ele faz a leitura dos arquivos .json (metadata) e depois executa o restore (.bson).

Esses arquivos são gerados pelo mongodump, ex:

# mongodump --host mongo01 --username restoreuser --password restorepwd --authenticationDatabase admin --db inventory
2018-06-20T22:42:38.220+0000 writing inventory.inventory to
2018-06-20T22:42:38.222+0000 done dumping inventory.inventory (5 documents)
[root@mongo01 ~]# tree dump/
dump/
└── inventory
├── inventory.bson
└── inventory.metadata.json

1 directory, 2 files

Vamos checar se o restore foi executado:

[root@mongo01 ~]# mongo --quiet -u tadeuadmin -p '#Senhalinda2000'
mongo_lab:PRIMARY> show dbs;
admin 0.000GB
config 0.000GB
local 0.047GB
teste 0.043GB
mongo_lab:PRIMARY> use teste;
switched to db teste
mongo_lab:PRIMARY> show collections;
DBEnvyLoad_customers
DBEnvyLoad_orders
DBEnvyLoad_products
GraphTest_smallGTest
Sakila_actors
Sakila_customers
Sakila_films
and_sorted_test
crunchbase_database
enron_messages
mongomart_cart
mongomart_item
samples_friends
samples_pokemon
savedPlans
video_movieDetails
video_movies
video_reviews

Alaaaa rapaiiii, não é que funciona memo…ele criou o banco teste e inseriu todas as collections contidas daquele diretório.

Vamos ver se replicou, no host mongo02:

[root@mongo02 ~]# mongo localhost:27018 -u "tadeuadmin" -p "#Senhalinda2000" --authenticationDatabase "admin"
MongoDB shell version v3.6.17
connecting to: mongodb://localhost:27018/test?authSource=admin&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("6555087d-eba2-4481-8c90-4d695188a49f") }
MongoDB server version: 3.6.17
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
mongo_lab:SECONDARY> rs.slaveOk()
mongo_lab:SECONDARY> show dbs;
admin 0.000GB
config 0.000GB
local 0.048GB
teste 0.038GB

Que belezinhaaa!!! Agora vamos brincar com o dump:

[root@mongo01 mongodb] # mongodump --host mongo01 --username restoreuser --password restorepwd --authenticationDatabase admin --db teste
2018-06-20T20:03:19.842+0000 Failed: error counting teste.video_movieDetails: not authorized on teste to execute command { count: "video_movieDetails", query: {}, $readPreference: { mode: "secondaryPreferred" }, $db: "teste" }

Opaaaa…FAMMM!! Precisamos atribuir ao nosso usuário a role de backup.

mongo_lab:PRIMARY> db.grantRolesToUser( "restoreuser", [ "backup" ]);

Feito isso vamos fazer o backup do banco teste:

[root@mongo01 teste]# mongodump --host mongo01 --username restoreuser --password restorepwd --authenticationDatabase admin --db teste
2018-06-20T20:22:49.211+0000 writing teste.and_sorted_test to
2018-06-20T20:22:49.211+0000 writing teste.DBEnvyLoad_customers to
2018-06-20T20:22:49.211+0000 writing teste.DBEnvyLoad_products to
2018-06-20T20:22:49.212+0000 writing teste.DBEnvyLoad_orders to
2018-06-20T20:22:49.295+0000 done dumping teste.DBEnvyLoad_orders (10000 documents)


Para fazer o backup de uma collection:

[root@mongo01 teste] # mongodump --host mongo01 --username restoreuser --password restorepwd --authenticationDatabase admin --db teste -c video_movies
2018-06-20T22:48:52.413+0000 writing teste.video_movies to
2018-06-20T22:48:52.421+0000 done dumping teste.video_movies (3365 documents)

Vamos dropar uma collection:

mongo_lab:PRIMARY> db.video_movies.drop();
true

Para fazer o restore da collection:

[root@mongo01 mongo] # mongorestore --host mongo01 --username restoreuser --password restorepwd --authenticationDatabase admin --db teste -c video_movies_restore dump/teste/video_movies.bson
2018-06-20T22:58:55.562+0000 checking for collection data in dump/teste/video_movies.bson
2018-06-20T22:58:55.563+0000 reading metadata for teste.video_movies_restore from dump/teste/video_movies.metadata.json
2018-06-20T22:58:55.575+0000 restoring teste.video_movies_restore from dump/teste/video_movies.bson
2018-06-20T22:58:55.756+0000 no indexes to restore
2018-06-20T22:58:55.758+0000 finished restoring teste.video_movies_restore (3365 documents)
2018-06-20T22:58:55.758+0000 done

CONCLUSÃO
Esse foi um artigo hand’s on que eu curti bastante fazer, como já havia feito o do cluster no artigo anterior ficou faltando a parte de backup e restore que resolvi deixar para este post. Particularmente estou gostando muito dos databases NoSQL, espero que gostem e que possa ser útil em algum momento =)

Referências
https://docs.mongodb.com/manual/tutorial/backup-and-restore-tools/
https://docs.mongodb.com/manual/reference/program/mongorestore/