Tuesday, April 16, 2024

.::: Backup & Restore Database MongoDB Using Mongodump & mongorestore :::.

Step 1: Create Direct Backups Using Mongodump

mongodump
 
[root@teguhth ~]# mongodump
2024-04-15T14:59:40.783+0700    writing admin.system.users to dump/admin/system.users.bson
2024-04-15T14:59:40.789+0700    done dumping admin.system.users (3 documents)
2024-04-15T14:59:40.790+0700    writing admin.system.version to dump/admin/system.version.bson
2024-04-15T14:59:40.790+0700    done dumping admin.system.version (2 documents)
2024-04-15T14:59:40.792+0700    writing teguhth.pembelian to dump/teguhth/pembelian.bson
2024-04-15T14:59:40.793+0700    writing teguhth.pasok to dump/teguhth/pasok.bson
2024-04-15T14:59:40.794+0700    writing teguhth.suplier to dump/teguhth/suplier.bson
2024-04-15T14:59:40.795+0700    writing kampus.mahasiswi to dump/kampus/mahasiswi.bson
2024-04-15T14:59:40.797+0700    done dumping teguhth.pembelian (15 documents)
2024-04-15T14:59:40.798+0700    writing teguhth.barang to dump/teguhth/barang.bson
2024-04-15T14:59:40.799+0700    done dumping teguhth.pasok (12 documents)
2024-04-15T14:59:40.800+0700    writing teguhth.customer to dump/teguhth/customer.bson
2024-04-15T14:59:40.801+0700    done dumping kampus.mahasiswi (8 documents)
2024-04-15T14:59:40.801+0700    writing tka.barang to dump/tka/barang.bson
2024-04-15T14:59:40.802+0700    done dumping teguhth.barang (6 documents)
2024-04-15T14:59:40.803+0700    writing tka.Barang to dump/tka/Barang.bson
2024-04-15T14:59:40.803+0700    done dumping teguhth.customer (6 documents)
2024-04-15T14:59:40.806+0700    done dumping tka.barang (6 documents)
2024-04-15T14:59:40.807+0700    done dumping teguhth.suplier (6 documents)
2024-04-15T14:59:40.808+0700    done dumping tka.Barang (0 documents)
[root@teguhth ~]#


Step 2: Backup a Remote MongoDB Instance

mongodump --host="10.10.10.8" --port=27017

[root@teguhth ~]# mongodump --host="10.10.10.8" --port=27017
2024-04-15T15:01:32.345+0700    writing admin.system.users to dump/admin/system.users.bson
2024-04-15T15:01:32.345+0700    done dumping admin.system.users (3 documents)
2024-04-15T15:01:32.346+0700    writing admin.system.version to dump/admin/system.version.bson
2024-04-15T15:01:32.346+0700    done dumping admin.system.version (2 documents)
2024-04-15T15:01:32.365+0700    writing teguhth.pembelian to dump/teguhth/pembelian.bson
2024-04-15T15:01:32.367+0700    writing teguhth.suplier to dump/teguhth/suplier.bson
2024-04-15T15:01:32.367+0700    writing kampus.mahasiswi to dump/kampus/mahasiswi.bson
2024-04-15T15:01:32.367+0700    writing teguhth.pasok to dump/teguhth/pasok.bson
2024-04-15T15:01:32.368+0700    done dumping teguhth.pembelian (15 documents)
2024-04-15T15:01:32.369+0700    done dumping teguhth.suplier (6 documents)
2024-04-15T15:01:32.370+0700    done dumping kampus.mahasiswi (8 documents)
2024-04-15T15:01:32.370+0700    writing teguhth.barang to dump/teguhth/barang.bson
2024-04-15T15:01:32.370+0700    done dumping teguhth.pasok (12 documents)
2024-04-15T15:01:32.370+0700    writing teguhth.customer to dump/teguhth/customer.bson
2024-04-15T15:01:32.370+0700    writing tka.barang to dump/tka/barang.bson
2024-04-15T15:01:32.371+0700    writing tka.Barang to dump/tka/Barang.bson
2024-04-15T15:01:32.372+0700    done dumping teguhth.barang (6 documents)
2024-04-15T15:01:32.372+0700    done dumping tka.barang (6 documents)
2024-04-15T15:01:32.372+0700    done dumping teguhth.customer (6 documents)
2024-04-15T15:01:32.373+0700    done dumping tka.Barang (0 documents)
[root@teguhth ~]#



Step 3: Backup a Secure MongoDB Instance

mongodump --host="10.10.10.8" --port=27017 --authenticationDatabase="admin" -u="admin" -p="admin"

[root@teguhth ~]# mongodump --host="10.10.10.8" --port=27017 --authenticationDatabase="admin" -u="admin" -p="admin"
2024-04-15T15:06:10.545+0700    WARNING: On some systems, a password provided directly using --password may be visible to system status programs such as `ps` that may be invoked by other users. Consider omitting the password to provide it via stdin, or using the --config option to specify a configuration file with the password.
2024-04-15T15:06:10.568+0700    writing admin.system.users to dump/admin/system.users.bson
2024-04-15T15:06:10.569+0700    done dumping admin.system.users (3 documents)
2024-04-15T15:06:10.569+0700    writing admin.system.version to dump/admin/system.version.bson
2024-04-15T15:06:10.570+0700    done dumping admin.system.version (2 documents)
2024-04-15T15:06:10.582+0700    writing teguhth.pembelian to dump/teguhth/pembelian.bson
2024-04-15T15:06:10.583+0700    writing teguhth.pasok to dump/teguhth/pasok.bson
2024-04-15T15:06:10.584+0700    writing kampus.mahasiswi to dump/kampus/mahasiswi.bson
2024-04-15T15:06:10.584+0700    writing teguhth.suplier to dump/teguhth/suplier.bson
2024-04-15T15:06:10.585+0700    done dumping teguhth.pembelian (15 documents)
2024-04-15T15:06:10.586+0700    done dumping teguhth.pasok (12 documents)
2024-04-15T15:06:10.586+0700    done dumping kampus.mahasiswi (8 documents)
2024-04-15T15:06:10.587+0700    writing teguhth.barang to dump/teguhth/barang.bson
2024-04-15T15:06:10.587+0700    done dumping teguhth.suplier (6 documents)
2024-04-15T15:06:10.587+0700    writing teguhth.customer to dump/teguhth/customer.bson
2024-04-15T15:06:10.587+0700    writing tka.barang to dump/tka/barang.bson
2024-04-15T15:06:10.588+0700    writing tka.Barang to dump/tka/Barang.bson
2024-04-15T15:06:10.588+0700    done dumping teguhth.barang (6 documents)
2024-04-15T15:06:10.589+0700    done dumping teguhth.customer (6 documents)
2024-04-15T15:06:10.589+0700    done dumping tka.barang (6 documents)
2024-04-15T15:06:10.590+0700    done dumping tka.Barang (0 documents)
[root@teguhth ~]#


Step 4: Select Databases & Collections

mongodump  --db=teguhth --collection=barang
mongodump  --db=teguhth --collection=barang --excludeCollection=pembelian

[root@teguhth ~]# mongodump  --db=teguhth --collection=barang
2024-04-15T15:08:30.918+0700    writing teguhth.barang to dump/teguhth/barang.bson
2024-04-15T15:08:30.919+0700    done dumping teguhth.barang (6 documents)
[root@teguhth ~]#

[root@teguhth ~]# mongodump  --db=teguhth  --excludeCollection=pembelian
2024-04-15T15:10:01.686+0700    writing teguhth.suplier to dump/teguhth/suplier.bson
2024-04-15T15:10:01.689+0700    writing teguhth.customer to dump/teguhth/customer.bson
2024-04-15T15:10:01.689+0700    writing teguhth.barang to dump/teguhth/barang.bson
2024-04-15T15:10:01.692+0700    writing teguhth.pasok to dump/teguhth/pasok.bson
2024-04-15T15:10:01.692+0700    done dumping teguhth.barang (6 documents)
2024-04-15T15:10:01.693+0700    done dumping teguhth.customer (6 documents)
2024-04-15T15:10:01.693+0700    done dumping teguhth.suplier (6 documents)
2024-04-15T15:10:01.694+0700    done dumping teguhth.pasok (12 documents)
[root@teguhth ~]#


Step 5: Change the Backup Directory

mongodump --host="10.10.10.8" --port=27017 --authenticationDatabase="admin" -u="admin" -p="admin" --out=/root/tempbackup

[root@teguhth ~]# mongodump --host="10.10.10.8" --port=27017 --authenticationDatabase="admin" -u="admin" -p="admin" --out=/root/tempbackup
2024-04-15T15:11:41.992+0700    writing admin.system.users to /root/tempbackup/admin/system.users.bson
2024-04-15T15:11:41.994+0700    done dumping admin.system.users (3 documents)
2024-04-15T15:11:41.994+0700    writing admin.system.version to /root/tempbackup/admin/system.version.bson
2024-04-15T15:11:41.995+0700    done dumping admin.system.version (2 documents)
2024-04-15T15:11:41.996+0700    writing teguhth.pembelian to /root/tempbackup/teguhth/pembelian.bson
2024-04-15T15:11:41.998+0700    writing teguhth.pasok to /root/tempbackup/teguhth/pasok.bson
2024-04-15T15:11:41.998+0700    writing kampus.mahasiswi to /root/tempbackup/kampus/mahasiswi.bson
2024-04-15T15:11:41.999+0700    writing teguhth.suplier to /root/tempbackup/teguhth/suplier.bson
2024-04-15T15:11:42.000+0700    done dumping teguhth.pembelian (15 documents)
2024-04-15T15:11:42.003+0700    done dumping teguhth.suplier (6 documents)
2024-04-15T15:11:42.003+0700    done dumping teguhth.pasok (12 documents)
2024-04-15T15:11:42.004+0700    writing teguhth.barang to /root/tempbackup/teguhth/barang.bson
2024-04-15T15:11:42.004+0700    done dumping kampus.mahasiswi (8 documents)
2024-04-15T15:11:42.004+0700    writing tka.barang to /root/tempbackup/tka/barang.bson
2024-04-15T15:11:42.004+0700    writing teguhth.customer to /root/tempbackup/teguhth/customer.bson
2024-04-15T15:11:42.005+0700    writing tka.Barang to /root/tempbackup/tka/Barang.bson
2024-04-15T15:11:42.006+0700    done dumping tka.Barang (0 documents)
2024-04-15T15:11:42.007+0700    done dumping teguhth.customer (6 documents)
2024-04-15T15:11:42.008+0700    done dumping tka.barang (6 documents)
2024-04-15T15:11:42.009+0700    done dumping teguhth.barang (6 documents)
[root@teguhth ~]#


Step 6: Create an Archive File

mongodump --host="10.10.10.8" --port=27017 --authenticationDatabase="admin" -u="admin" -p="admin" --archive=/root/dump/db.archive

[root@teguhth ~]# mongodump --host="10.10.10.8" --port=27017 --authenticationDatabase="admin" -u="admin" -p="admin" --archive=/root/dump/db.archive
2024-04-15T15:21:30.796+0700    writing admin.system.users to archive '/root/dump/db.archive'
2024-04-15T15:21:30.799+0700    done dumping admin.system.users (3 documents)
2024-04-15T15:21:30.800+0700    writing admin.system.version to archive '/root/dump/db.archive'
2024-04-15T15:21:30.801+0700    done dumping admin.system.version (2 documents)
2024-04-15T15:21:30.804+0700    writing teguhth.pembelian to archive '/root/dump/db.archive'
2024-04-15T15:21:30.808+0700    writing teguhth.pasok to archive '/root/dump/db.archive'
2024-04-15T15:21:30.816+0700    writing kampus.mahasiswi to archive '/root/dump/db.archive'
2024-04-15T15:21:30.817+0700    writing teguhth.suplier to archive '/root/dump/db.archive'
2024-04-15T15:21:30.821+0700    done dumping teguhth.pembelian (15 documents)
2024-04-15T15:21:30.821+0700    done dumping teguhth.suplier (6 documents)
2024-04-15T15:21:30.821+0700    done dumping teguhth.pasok (12 documents)
2024-04-15T15:21:30.821+0700    done dumping kampus.mahasiswi (8 documents)
2024-04-15T15:21:30.821+0700    writing tka.Barang to archive '/root/dump/db.archive'
2024-04-15T15:21:30.823+0700    writing teguhth.customer to archive '/root/dump/db.archive'
2024-04-15T15:21:30.824+0700    writing tka.barang to archive '/root/dump/db.archive'
2024-04-15T15:21:30.828+0700    writing teguhth.barang to archive '/root/dump/db.archive'
2024-04-15T15:21:30.838+0700    done dumping teguhth.customer (6 documents)
2024-04-15T15:21:30.838+0700    done dumping tka.Barang (0 documents)
2024-04-15T15:21:30.838+0700    done dumping tka.barang (6 documents)
2024-04-15T15:21:30.839+0700    done dumping teguhth.barang (6 documents)
[root@teguhth ~]#


Step 7: Compress the MongoDB Backup

mongodump --host="10.10.10.8" --port=27017 --authenticationDatabase="admin" -u="admin" -p="admin"  --gzip

[root@teguhth ~]# mongodump --host="10.10.10.8" --port=27017 --authenticationDatabase="admin" -u="admin" -p="admin"  --gzip
2024-04-15T15:23:52.905+0700    writing admin.system.users to dump/admin/system.users.bson.gz
2024-04-15T15:23:52.906+0700    done dumping admin.system.users (3 documents)
2024-04-15T15:23:52.906+0700    writing admin.system.version to dump/admin/system.version.bson.gz
2024-04-15T15:23:52.907+0700    done dumping admin.system.version (2 documents)
2024-04-15T15:23:52.908+0700    writing teguhth.pembelian to dump/teguhth/pembelian.bson.gz
2024-04-15T15:23:52.909+0700    writing teguhth.pasok to dump/teguhth/pasok.bson.gz
2024-04-15T15:23:52.909+0700    writing kampus.mahasiswi to dump/kampus/mahasiswi.bson.gz
2024-04-15T15:23:52.910+0700    writing teguhth.suplier to dump/teguhth/suplier.bson.gz
2024-04-15T15:23:52.913+0700    done dumping teguhth.pasok (12 documents)
2024-04-15T15:23:52.914+0700    done dumping teguhth.pembelian (15 documents)
2024-04-15T15:23:52.915+0700    writing teguhth.barang to dump/teguhth/barang.bson.gz
2024-04-15T15:23:52.915+0700    done dumping kampus.mahasiswi (8 documents)
2024-04-15T15:23:52.918+0700    writing teguhth.customer to dump/teguhth/customer.bson.gz
2024-04-15T15:23:52.918+0700    done dumping teguhth.suplier (6 documents)
2024-04-15T15:23:52.918+0700    writing tka.barang to dump/tka/barang.bson.gz
2024-04-15T15:23:52.919+0700    writing tka.Barang to dump/tka/Barang.bson.gz
2024-04-15T15:23:52.919+0700    done dumping teguhth.barang (6 documents)
2024-04-15T15:23:52.920+0700    done dumping tka.barang (6 documents)
2024-04-15T15:23:52.920+0700    done dumping teguhth.customer (6 documents)
2024-04-15T15:23:52.920+0700    done dumping tka.Barang (0 documents)
[root@teguhth ~]#

Step 8: Create Direct Backups Using Mongodump with timing

mongodump --host="10.10.10.8" --port=27017 --db=teguhth --authenticationDatabase="admin" -u="admin" -p="admin" --out=/root/tempbackup/$(date +"%Y%m%d")
mongodump --host="10.10.10.8" --port=27017 --db=teguhth --authenticationDatabase="admin" -u="admin" -p="admin" --out=/root/tempbackup/`date +"%Y%m%d_%H%M%S"`

[root@teguhth tempbackup]# pwd
/root/tempbackup
[root@teguhth tempbackup]# mongodump --host="10.10.10.8" --port=27017 --db=teguhth --authenticationDatabase="admin" -u="admin" -p="admin" --out=/root/tempbackup/$(date +"%Y%m%d")
2024-04-16T13:55:44.553+0700    writing teguhth.pasok to /root/tempbackup/20240416/teguhth/pasok.bson
2024-04-16T13:55:44.555+0700    writing teguhth.suplier to /root/tempbackup/20240416/teguhth/suplier.bson
2024-04-16T13:55:44.559+0700    writing teguhth.barang to /root/tempbackup/20240416/teguhth/barang.bson
2024-04-16T13:55:44.564+0700    done dumping teguhth.pasok (12 documents)
2024-04-16T13:55:44.565+0700    writing teguhth.pembelian to /root/tempbackup/20240416/teguhth/pembelian.bson
2024-04-16T13:55:44.567+0700    done dumping teguhth.suplier (6 documents)
2024-04-16T13:55:44.567+0700    done dumping teguhth.barang (6 documents)
2024-04-16T13:55:44.570+0700    done dumping teguhth.pembelian (15 documents)
2024-04-16T13:55:44.571+0700    writing teguhth.customer to /root/tempbackup/20240416/teguhth/customer.bson
2024-04-16T13:55:44.572+0700    done dumping teguhth.customer (6 documents)
[root@teguhth tempbackup]# ls
20240416
[root@teguhth tempbackup]#
[root@teguhth tempbackup]# mongodump --host="10.10.10.8" --port=27017 --db=teguhth --authenticationDatabase="admin" -u="admin" -p="admin" --out=/root/tempbackup/$(date +"%Y%m%d")
2024-04-16T13:56:17.152+0700    writing teguhth.pasok to /root/tempbackup/20240416/teguhth/pasok.bson
2024-04-16T13:56:17.153+0700    writing teguhth.suplier to /root/tempbackup/20240416/teguhth/suplier.bson
2024-04-16T13:56:17.154+0700    writing teguhth.barang to /root/tempbackup/20240416/teguhth/barang.bson
2024-04-16T13:56:17.161+0700    writing teguhth.pembelian to /root/tempbackup/20240416/teguhth/pembelian.bson
2024-04-16T13:56:17.165+0700    done dumping teguhth.pasok (12 documents)
2024-04-16T13:56:17.168+0700    done dumping teguhth.barang (6 documents)
2024-04-16T13:56:17.169+0700    writing teguhth.customer to /root/tempbackup/20240416/teguhth/customer.bson
2024-04-16T13:56:17.171+0700    done dumping teguhth.pembelian (15 documents)
2024-04-16T13:56:17.174+0700    done dumping teguhth.suplier (6 documents)
2024-04-16T13:56:17.175+0700    done dumping teguhth.customer (6 documents)
[root@teguhth tempbackup]#
[root@teguhth tempbackup]# cd 20240416/teguhth/
[root@teguhth teguhth]# pwd
/root/tempbackup/20240416/teguhth
[root@teguhth teguhth]# ls
barang.bson           customer.bson           pasok.bson           pembelian.bson           suplier.bson
barang.metadata.json  customer.metadata.json  pasok.metadata.json  pembelian.metadata.json  suplier.metadata.json
[root@teguhth teguhth]#


Step 9: Restore Database

mongorestore --db=teguhth  dump/

mongorestore --archive=db.archive  --db=teguhth --host="10.10.10.8" --port=27017 --authenticationDatabase="admin" -u="admin" -p="admin"  --drop

mongorestore --db=teguhth --host="10.10.10.8" --port=27017 --authenticationDatabase="admin" -u="admin" -p="admin"  /root/dump/teguhth

mongorestore --db=teguhth --host="10.10.10.8" --port=27017 --authenticationDatabase="admin" -u="admin" -p="admin"  --noIndexRestore /root/dump/teguhth

mongorestore --db=res --host="10.10.10.8" --port=27017 --authenticationDatabase="admin" -u="admin" -p="admin"  /root/dump/teguhth

mongorestore --db=ls --host="10.10.10.8" --port=27017 --authenticationDatabase="admin" -u="admin" -p="admin"  /root/dump/teguhth

[root@teguhth ~]# mongorestore --db=ls --host="10.10.10.8" --port=27017 --authenticationDatabase="admin" -u="admin" -p="admin"  /root/dump/teguhth
2024-04-15T15:58:44.356+0700    The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2024-04-15T15:58:44.356+0700    building a list of collections to restore from /root/dump/teguhth dir
2024-04-15T15:58:44.356+0700    reading metadata for ls.barang from /root/dump/teguhth/barang.metadata.json
2024-04-15T15:58:44.356+0700    reading metadata for ls.customer from /root/dump/teguhth/customer.metadata.json
2024-04-15T15:58:44.356+0700    reading metadata for ls.pasok from /root/dump/teguhth/pasok.metadata.json
2024-04-15T15:58:44.356+0700    reading metadata for ls.pembelian from /root/dump/teguhth/pembelian.metadata.json
2024-04-15T15:58:44.356+0700    reading metadata for ls.suplier from /root/dump/teguhth/suplier.metadata.json
2024-04-15T15:58:44.383+0700    restoring ls.pembelian from /root/dump/teguhth/pembelian.bson
2024-04-15T15:58:44.389+0700    restoring ls.pasok from /root/dump/teguhth/pasok.bson
2024-04-15T15:58:44.404+0700    restoring ls.customer from /root/dump/teguhth/customer.bson
2024-04-15T15:58:44.404+0700    finished restoring ls.pembelian (15 documents, 0 failures)
2024-04-15T15:58:44.414+0700    finished restoring ls.pasok (12 documents, 0 failures)
2024-04-15T15:58:44.414+0700    restoring ls.suplier from /root/dump/teguhth/suplier.bson
2024-04-15T15:58:44.420+0700    finished restoring ls.customer (6 documents, 0 failures)
2024-04-15T15:58:44.422+0700    restoring ls.barang from /root/dump/teguhth/barang.bson
2024-04-15T15:58:44.462+0700    finished restoring ls.suplier (6 documents, 0 failures)
2024-04-15T15:58:44.495+0700    finished restoring ls.barang (6 documents, 0 failures)
2024-04-15T15:58:44.495+0700    no indexes to restore for collection ls.barang
2024-04-15T15:58:44.495+0700    no indexes to restore for collection ls.customer
2024-04-15T15:58:44.495+0700    no indexes to restore for collection ls.pasok
2024-04-15T15:58:44.495+0700    no indexes to restore for collection ls.pembelian
2024-04-15T15:58:44.495+0700    no indexes to restore for collection ls.suplier
2024-04-15T15:58:44.495+0700    45 document(s) restored successfully. 0 document(s) failed to restore.
[root@teguhth ~]#

No comments:

Post a Comment

Popular Posts