Restore mongodb standalone

To do a restore you first need a backup. A filesystem snapshot is good enough.

1 Like

By your own words you don’t have a backup, ergo you cannot restore. You would need the database file at the point before you lost your data.

The files you have for your standalone database represent the database as it is now or when you shut it down.

1 Like

Thanks Chris for feedback.

We have checked with our dba team they are suggesting all the collection and index files before 13th july are present in /data/db dir…whcih i have shared above.

Suppose if DBAs are telling correctly that all the files before 13th july are present in /data/db dir, how can we restore it any methods.

Current senerio is i checked the collections by taking latest mongodump in standalone db and checked in one db collection documents are present after 13th july to sysdata in collection files.

Any suggestion would be great help, we need to recover the old data before 13th july, what steps we have to follow.

We checked all the files before shut down, no WT files found except /data/db dir.

So there was a backup and they have given you files? Why would your dba team not just do the recovery for you, that is a dba’s role.

Then you should be able to start mongod using /data/db for the --dbpath option(this is the default anyway).

If the data is not there, then you need files from further back.

1 Like

Thanks Chris for the feedback.

We have standlone db in prod, and we are planning to run this steps to restore mongodb old data before 13th July data.

  1. taken mongodump backup of all dbs using mongodump command

  2. take backup of current collection and index WT files present in data/db dir.

  3. As dba team suggest us all files present in /data/db dir before 13th July also
    Will run the command after backups :
    mongod --dbpath /data/db

  4. now if suppose db is not coming up, what should we do .
    and also we have question we dont have any test and dev env setup so will do run these commands in prod db only.

  5. we need suggestions the data which is present in /data/db dir from 13th july to till date if suppose restore happens from mongod --dbpath /data/db command will current data will mismatch , is there any conflicts in data can occur.

for all these please suggest, thanks.

Hi @bp_sin

I would be very cautious in your scenario. I strongly recommend you take a backup of your production database and test all outcomes in a separate environment first. If this is critical production data to restore and your team doesn’t have the in-house expertise, I would consider hiring a consultant for assistance.

If I understand your situation correctly:

  • Some data before July 13th was accidentally deleted
  • You have a file copy backup of the dbPath before the data was deleted
  • You want to merge missing data into a production database

The last step is the most complicated: unless collections of interest happen to be insert only, there may be conflicts between the current data and the data as of a month ago. You will have to work out how to reconcile any differences. Restoring older data will also include data that has been intentionally removed at a later date.

My suggested approach would be:

  • Start a mongod in a test environment using the dbPath backup from July 13th. Verify that this is a viable backup and the data that you are looking for is available.
  • Take a backup of your production data and restore into a separate mongod in a test environment.
  • Write queries (or a script) to compare collections of interest in these two test environments.
  • Use mongodump --query to selectively export missing data from the July 13th backup.
  • Import into your production test server using mongorestore, and verify the outcomes are as you expect.
  • Repeat in production once you are confident your procedure results in the desired outcome. Always take fresh backups before doing any maintenance procedures that might alter data.

Unless your production data is not essential, I would take the time to set up a test environment.

Regards,
Stennie

Thanks Stennie,

Could you please assist from WT collection and index files present in /data/db dir , how to restore the old data.
is we can use mongod --dbpath /data/db command to restore , before that will take the backup using mongodump and current backup of all files present inn /data/db dir.

for test env creation can we keep all this /data/db dir fles in our local system where i have installed mongo in dir: /data/ and restart mongod to test.

Hi @bp_sin,

If you follow my suggested approach above, the first step is making sure you have a viable file copy backup:

  • Start a mongod in a test environment using the dbPath backup from July 13th. Verify that this is a viable backup and the data that you are looking for is available.

If these files are a viable backup copy, mongod --dbpath /path/to/your/backup should start without any errors relating to data files and you will be able to query to confirm if the data you are looking for is present.

If the WT files you have are not a consistent backup, your options are unfortunately limited. You can try repairing or salvaging data as a last resort but that is often unsuccessful (see: Recover corrupted files (MongoDB 3.0.12) .wt - #4 by Stennie_X).

You should have separate paths for data files belonging to different backups or environments. Mixing files from different deployments may result in an unusable deployment.

I’d still recommend engaging a consultant (or your DBA team, if there is someone with more MongoDB admin experience on the team) as incorrect manipulation of data files or merging of data could lead to further challenges.

Regards,
Stennie

1 Like

Thanks Stennie for feedback.

My approach am trying please suggest is it correct:

  1. I will install mongodb in my local system.
  2. I have copy of all the files which is present in /data/db dir in production env, i will copy all the WT files into my local system in suppose dir /data
  3. Startng mongod using path /data: mongod --dbpath /data
  4. check the collection documents and check if all the documents before 13th July stored in documents by querying from robo3t.

any other steps you would like to provide which i can test in my local system, thanks.

Or any other files i also need to copy apart from all the files from prod in /data/db dir.
to setup and test.

Thanks Stennie,

I have run in my local system by copying all the WT files from /data/db to /data/backup_restore and run the command mongod --dbpath /data/backup_restore, it’s still running from last 3 hours.

Could you please suggest if any files missing or corrupt files present in /data/db dir how can we restore the data before july 13 old data.

any suggestions.

Thanks Chris,

If suppose some of files are missing before 13th july in /data/db dir in prod env, can we still restore the rest of data present before 13th july in WT collection and index files in /data/db dir
using command

mongod --dbpath /data/db

how can we approach to test env first,

Can you please suggest

What are the steps to be follow in test env to replicate this senerios.

We have no data before 13th July in standalone db, DBA team updating all datafiles are present in /data/db dir in prod env, if there statmenet is correct then can we restore the old data directly in prod.

I have installed mongodb in my local system, if want to replicate the prod senerio my approach below:-

  1. copy all the files from prod env(/data/db) to another dir in local system lets say /data/backup
  2. then start the mongod --dbpath /data/backup
    will it restore all the old data before 13th july.

Hi @bp_sin,

mongod is the primary daemon process for MongoDB. On startup, mongod will verify the integrity of files in the dbpath but I wouldn’t expect this to take 3 hours for the 5 collections you have listed in your earlier output.

What is the last output from mongod? It may have started up successfully and be waiting for connections.

Running mongod with an older backup of your data files will allow you to query and potentially mongodump relevant data. This is not doing any sort of “restore” process: you have to do that manually and reconcile any differences between your two environments.

There should be startup errors if data files are missing or have checksum issues.

Are you working on this issue with anyone from your DBA team? As I mentioned earlier, this procedure would benefit from assistance from someone with more MongoDB admin experience.

Regards,
Stennie

1 Like

Thanks Stennie,
Our DBA replying with:
We should take backup.
I’m not sure that the data was actually deleted or not.
We’ve found that mongodb pod cannot access to data then tried to restore them July 16th . During that process we’ve taken a backup.
have no idea what kind of operation happened for production after that.
create new DB or overwrite them?

Could you please provide steps how will we create new mongodb.

How can we restore the old data from these files present in /data/db dir.

Hi @bp_sin,

Have you tried starting up a mongod instance with your backup copy of this dbPath as mentioned earlier?

mongod --dbpath /path/to/your/backup

If mongod is able to start successfully, you can query and export data using standard tools like the mongo shell and mongodump. Assuming the data you are looking for is present, try to follow the general guide I suggested earlier in this discussion.

If mongod has any issues reading the data files, there should be warnings logged on startup with more context on files that are missing or problematic. If you don’t have a viable backup copy, your options are limited and unlikely to be fully successful (see Recover corrupted files (MongoDB 3.0.12) .wt - #4 by Stennie, which Chris mentioned earlier).

The specific steps involved require an understanding of the data you are trying to recover and ideally someone who is working through the process with you directly (i.e. a MongoDB expert from your DBA team or an external consultant).

Regards,
Stennie

1 Like

HI Stennie, thanks for suggestion.
I tried copying all the files from /data/db dir of production db and restored into my local system where mongodb is installed, and kept in the dir: E:\restore_backup
and tried running mongo with command

mongod --dbpath E:\restore_backup

but it’s still runing from last 6 hours.

C:\Program Files\MongoDB\Server\5.0\bin>mongod --dbpath E:\restore_backup
{"t":{"$date":"2021-08-20T23:57:04.308+05:30"},"s":"I",  "c":"NETWORK",  "id":4915701, "ctx":"-","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":13},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":13},"outgoing":{"minWireVersion":0,"maxWireVersion":13},"isInternalClient":true}}}
{"t":{"$date":"2021-08-20T23:57:05.559+05:30"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"-","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2021-08-20T23:57:05.559+05:30"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
{"t":{"$date":"2021-08-21T08:42:57.939+05:30"},"s":"I",  "c":"CONTROL",  "id":20536,   "ctx":"initandlisten","msg":"Flow Control is enabled on this deployment"}
{"t":{"$date":"2021-08-21T08:42:58.313+05:30"},"s":"I",  "c":"FTDC",     "id":20625,   "ctx":"initandlisten","msg":"Initializing full-time diagnostic data capture","attr":{"dataDirectory":"E:/restore_backup/diagnostic.data"}}
{"t":{"$date":"2021-08-21T08:42:58.320+05:30"},"s":"I",  "c":"NETWORK",  "id":23015,   "ctx":"listener","msg":"Listening on","attr":{"address":"127.0.0.1"}}
{"t":{"$date":"2021-08-21T08:42:58.320+05:30"},"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}

any suggestions, thanks

Your mongod is running in foreground
Just open another cmd prompt and connect to your mongod with mongo

2 Likes

Thanks Ramachandra,

Getting below error:

C:\Program Files\MongoDB\Server\5.0\bin>mongo
MongoDB shell version v5.0.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: No connection could be made because the target machine actively refused it. :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
exception: connect failed
exiting with code 1

I tired copy all the files from /data/db dir in prod db to my local system where mongodb is installed to location mongod --dbpath “E:\restore_backup” and run this command.

getting above error while executing.

Customer requirement to restore all the old data before july 16 , and as per dba team they are informing all the files before 16th July is present in prod mongo db in /data/db dir.

any suggestions how to restore all the old data.

Below are the files present in /data/db dir in production db.

/data/db# ls -lrth
total 2.2G
-rw------- 1 mongodb mongodb   46 May  5  2020 WiredTiger
-rw------- 1 mongodb mongodb  114 May  5  2020 storage.bson
-rw------- 1 mongodb mongodb   21 May  5  2020 WiredTiger.lock
drwx------ 2 mongodb mongodb 6.0K May 13  2020 RphLSrH4
drwx------ 2 mongodb mongodb 6.0K May 13  2020 Rtmp36TQ9s
drwx------ 2 mongodb mongodb 6.0K May 14  2020 RtmpU5VhGf
drwx------ 2 mongodb mongodb 6.0K May 14  2020 Rtmp5iOWBZ
drwx------ 2 mongodb mongodb 6.0K May 14  2020 RtmpA6ZerP
drwx------ 2 mongodb mongodb 6.0K May 16  2020 RtmpU81C2J
drwx------ 2 mongodb mongodb 6.0K May 16  2020 RtmpnRL72N
drwx------ 2 mongodb mongodb 6.0K May 16  2020 Rtmpc5BLGI
drwx------ 2 mongodb mongodb 6.0K May 16  2020 RtmpCqEH8v
drwx------ 2 mongodb mongodb 6.0K May 22  2020 RtmpiNBLVk
drwx------ 2 mongodb mongodb 6.0K May 22  2020 RtmpRcEvv0
drwx------ 2 mongodb mongodb 6.0K May 22  2020 RtmpJpTGHu
drwx------ 2 mongodb mongodb 6.0K May 26  2020 RtmpKkjWmv
drwx------ 2 mongodb mongodb 6.0K May 26  2020 Rtmpa4lwb6
drwx------ 2 mongodb mongodb 6.0K May 26  2020 Rtmpt7lBWx
drwx------ 2 mongodb mongodb 6.0K May 26  2020 RtmpAoVvqm
drwx------ 2 mongodb mongodb 6.0K May 26  2020 Rtmprj293v
drwx------ 2 mongodb mongodb 6.0K Jun 12  2020 Rtmpo51iBb
drwx------ 2 mongodb mongodb 6.0K Jun 19  2020 RtmpC4fJRe
drwx------ 2 mongodb mongodb 6.0K Jun 19  2020 RtmpVQwSul
drwx------ 2 mongodb mongodb 6.0K Jun 19  2020 Rtmp6qgLlp
drwx------ 2 mongodb mongodb 6.0K Jun 19  2020 Rtmpwwnk3U
drwx------ 2 mongodb mongodb 6.0K Jun 22  2020 RtmpbYNsjh
drwx------ 2 mongodb mongodb 6.0K Jun 22  2020 Rtmpn6Rnss
drwx------ 2 mongodb mongodb 6.0K Jun 22  2020 Rp1sq31l
drwx------ 2 mongodb mongodb 6.0K Jun 25  2020 RpZDvnEX
drwx------ 2 mongodb mongodb 6.0K Jun 25  2020 Rtmpw5UqoC
drwx------ 2 mongodb mongodb 6.0K Jun 25  2020 RtmpkwIaRu
drwx------ 2 mongodb mongodb 6.0K Jun 25  2020 RtmpFLzqN8
drwx------ 2 mongodb mongodb 6.0K Jun 25  2020 RtmpFVVwy0
drwx------ 2 mongodb mongodb 6.0K Jun 26  2020 RtmpeCeFIv
drwx------ 2 mongodb mongodb 6.0K Jun 26  2020 RtmpN3bWlP
drwx------ 2 mongodb mongodb 6.0K Jul 16  2020 Rtmplis6ts
drwx------ 2 mongodb mongodb 6.0K Jul 18  2020 Rtmp2vg0AJ
drwx------ 2 mongodb mongodb 6.0K Jul 21  2020 RtmpFlfKEo
drwx------ 2 mongodb mongodb 6.0K Jul 21  2020 RtmpIL6z4R
drwx------ 2 mongodb mongodb 6.0K Jul 21  2020 RtmpdnPwgb
drwx------ 2 mongodb mongodb 6.0K Jul 22  2020 RtmpINVgFY
drwx------ 2 mongodb mongodb 6.0K Aug  6  2020 RtmpgtVobk
drwx------ 2 mongodb mongodb 6.0K Aug 17  2020 RtmpyrYidj
drwx------ 2 mongodb mongodb 6.0K Aug 17  2020 RtmpLAyxW4
drwx------ 2 mongodb mongodb 6.0K Sep  1  2020 RtmpXUIbiR
-rw-r--r-- 1 mongodb mongodb  45M Sep  1  2020 cded-e214-ff6b-ccfa
drwxr-xr-x 2 mongodb mongodb 6.0K Sep  8  2020 hsperfdata_root
drwxr-xr-x 2 mongodb mongodb  22K Mar 30 18:23 progress
-rw------- 1 mongodb mongodb  36K Jul 15 16:23 collection-4--5138667409853774933.wt
-rw------- 1 mongodb mongodb  36K Jul 15 16:23 index-5--5138667409853774933.wt
-rw------- 1 mongodb mongodb 168K Jul 15 16:23 index-10--5138667409853774933.wt
-rw------- 1 mongodb mongodb 212K Jul 15 16:23 index-13--5138667409853774933.wt
-rw------- 1 mongodb mongodb  36K Jul 15 16:23 index-6--5138667409853774933.wt
-rw------- 1 mongodb mongodb 879M Jul 15 16:23 collection-12--5138667409853774933.wt
-rw------- 1 mongodb mongodb 228K Jul 15 16:23 index-14--5138667409853774933.wt
-rw------- 1 mongodb mongodb 500K Jul 15 16:23 collection-8--5138667409853774933.wt
-rw------- 1 mongodb mongodb 140K Jul 15 16:23 index-9--5138667409853774933.wt
-rw------- 1 mongodb mongodb  58M Jul 15 16:23 collection-19--5138667409853774933.wt
-rw------- 1 mongodb mongodb  92K Jul 15 16:23 index-20--5138667409853774933.wt
-rw------- 1 mongodb mongodb  59M Jul 15 16:23 collection-17--5138667409853774933.wt
-rw------- 1 mongodb mongodb  68K Jul 15 16:23 index-18--5138667409853774933.wt
-rw------- 1 mongodb mongodb  36K Jul 15 16:23 index-3--5138667409853774933.wt
-rw------- 1 mongodb mongodb  36K Jul 15 16:23 collection-2--5138667409853774933.wt
-rw------- 1 mongodb mongodb  32K Jul 16 02:07 collection-0--5138667409853774933.wt
-rw------- 1 mongodb mongodb  32K Jul 16 02:07 index-1--5138667409853774933.wt
-rw------- 1 mongodb mongodb  32K Jul 16 02:07 _mdb_catalog.wt.1
-rw------- 1 mongodb mongodb  36K Jul 16 02:07 sizeStorer.wt.1
-rw------- 1 mongodb mongodb 4.0K Jul 16 02:07 WiredTigerHS.wt
-rw------- 1 mongodb mongodb  60K Jul 16 12:25 WiredTiger.wt.orig
-rw------- 1 mongodb mongodb 4.0K Jul 16 13:51 WiredTigerLAS.wt
-rw------- 1 mongodb mongodb  16K Jul 16 13:51 collection-0--7947847146613003270.wt
-rw------- 1 mongodb mongodb    2 Jul 16 13:51 mongod.lock
-rw------- 1 mongodb mongodb  16K Jul 16 13:51 index-2--7947847146613003270.wt
-rw------- 1 mongodb mongodb  36K Jul 16 13:52 index-1--7067830708941503295.wt
-rw------- 1 mongodb mongodb  36K Jul 16 13:52 collection-0--7067830708941503295.wt
-rw------- 1 mongodb mongodb  36K Jul 16 20:19 _mdb_catalog.wt
drwx------ 2 mongodb mongodb 6.0K Aug  6 19:03 journal
-rw------- 1 mongodb mongodb  36K Aug 19 16:35 index-7--1930030124829508624.wt
-rw------- 1 mongodb mongodb  36K Aug 19 16:37 index-9--1930030124829508624.wt
-rw------- 1 mongodb mongodb 5.8M Aug 19 16:37 collection-8--1930030124829508624.wt
-rw------- 1 mongodb mongodb 5.3M Aug 19 16:37 collection-6--1930030124829508624.wt
-rw------- 1 mongodb mongodb  52K Aug 19 16:37 index-5--1930030124829508624.wt
-rw------- 1 mongodb mongodb  52K Aug 19 16:37 index-4--1930030124829508624.wt
-rw------- 1 mongodb mongodb  48M Aug 19 16:37 collection-3--1930030124829508624.wt
-rw------- 1 mongodb mongodb  52K Aug 19 16:37 index-2--1930030124829508624.wt
-rw------- 1 mongodb mongodb  44K Aug 19 16:37 index-1--1930030124829508624.wt
-rw------- 1 mongodb mongodb  84K Aug 19 16:37 collection-0--1930030124829508624.wt
-rw------- 1 mongodb mongodb  36K Aug 19 18:02 index-3--7067830708941503295.wt
-rw------- 1 mongodb mongodb  36K Aug 19 18:47 index-4--7067830708941503295.wt
-rw------- 1 mongodb mongodb  36K Aug 19 18:47 collection-2--7067830708941503295.wt
-rw------- 1 mongodb mongodb  36K Aug 19 18:48 sizeStorer.wt
-rw------- 1 mongodb mongodb  84K Aug 19 18:48 WiredTiger.wt
-rw------- 1 mongodb mongodb 1.2K Aug 19 18:48 WiredTiger.turtle
drwx------ 2 mongodb mongodb 6.0K Aug 19 18:52 diagnostic.data