{"id":323,"date":"2025-11-06T04:52:00","date_gmt":"2025-11-06T04:52:00","guid":{"rendered":"https:\/\/mitalgoswami.in\/?p=323"},"modified":"2025-11-08T05:40:30","modified_gmt":"2025-11-08T05:40:30","slug":"backup-and-restore-a-mongodb-database-works-for-mongodb-version-8-2-and-older-same-basic-commands","status":"publish","type":"post","link":"https:\/\/mitalgoswami.in\/?p=323","title":{"rendered":"backup and restore a MongoDB database \u2014 works for MongoDB version 8.2 and older (same basic commands)."},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>1. Backup MongoDB Database<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Option 1: Backup Entire Database<\/h3>\n\n\n\n<p>Use the <code>mongodump<\/code> command.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mongodump --db &lt;database_name&gt; --out &lt;backup_directory&gt;\n<\/code><\/pre>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>C:\\backup>set path=C:\\Program Files\\MongoDB\\Server\\8.2\\bin\nC:\\backup>mongodump --db emp1 --out c:\\backup\n\nmongodump --db testdb --out C:\\backup\\\n<\/code><\/pre>\n\n\n\n<p>This will create:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>C:\\backup\\testdb\\\n<\/code><\/pre>\n\n\n\n<p>Containing <code>.bson<\/code> (data) and <code>.metadata.json<\/code> (indexes).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Option 2: Backup All Databases<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mongodump --out C:\\backup\\all_dbs\\\nC:\\backup>mongodump --out c:\\backup\\alldbs<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Option 3: Backup with Authentication<\/h3>\n\n\n\n<p>If your MongoDB has authentication enabled:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mongodump --uri=\"mongodb:\/\/username:password@localhost:27017\/testdb\" --out C:\\backup\\\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Restore MongoDB Database<\/strong><\/h2>\n\n\n\n<p>Use the <code>mongorestore<\/code> command.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mongorestore --db &lt;database_name&gt; &lt;backup_folder_path&gt;\n<\/code><\/pre>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mongorestore --db testdb C:\\backup\\testdb\\\n<\/code><\/pre>\n\n\n\n<p>This restores the <code>testdb<\/code> database from the backup folder.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Option 2: Restore All Databases<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mongorestore C:\\backup\\all_dbs\\\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Option 3: Restore with Authentication<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mongorestore --uri=\"mongodb:\/\/username:password@localhost:27017\" C:\\backup\\\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"> Useful Flags:<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Option<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>--drop<\/code><\/td><td>Drops existing collections before restoring.<\/td><\/tr><tr><td><code>--gzip<\/code><\/td><td>Use if your backup was compressed.<\/td><\/tr><tr><td><code>--verbose<\/code><\/td><td>Displays detailed output during restore.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Example with <code>--drop<\/code>:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mongorestore --drop --db testdb C:\\backup\\testdb\\\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"> Example Backup &amp; Restore Workflow<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1:<\/strong> Backup<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mongodump --db studentDB --out C:\\mongo_backup\\\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2:<\/strong> Delete or modify data (simulate loss)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>use studentDB\ndb.students.drop()\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3:<\/strong> Restore<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mongorestore --db studentDB C:\\mongo_backup\\studentDB\\\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"> Backup &amp; Restore Files Created<\/h2>\n\n\n\n<p>Each collection is saved as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>collection_name.bson<\/code> \u2014 raw data<\/li>\n\n\n\n<li><code>collection_name.metadata.json<\/code> \u2014 index and metadata<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. Backup MongoDB Database \u2705 Option 1: Backup Entire Database Use the mongodump command. Syntax: Example: This will create: Containing .bson (data) and .metadata.json (indexes). \u2705 Option 2: Backup All Databases \u2705 Option 3: Backup with Authentication If your MongoDB has authentication enabled: 2. Restore MongoDB Database Use the mongorestore command. Syntax: Example: This restores [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-323","post","type-post","status-publish","format-standard","hentry","category-mongodb"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=323"}],"version-history":[{"count":3,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/323\/revisions"}],"predecessor-version":[{"id":327,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/323\/revisions\/327"}],"wp:attachment":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}