{"componentChunkName":"component---src-pages-markdown-remark-fields-slug-js","path":"/engineering/live-data-migration-mongodb/","result":{"data":{"markdownRemark":{"id":"8d1c6c7b-3313-5137-89fa-d096a463287e","excerpt":"The goal of this post is to learn about the various ways of data migration in MongoDB that can help us to write scripts that change your database by adding new…","html":"<p>The goal of this post is to learn about the various ways of data migration in MongoDB that can help us to write scripts that change your database by adding new documents, modifying existing ones.</p>\n<p>If you're coming here for the first time, please take a look at the prequel <a href=\"https://www.loginradius.com/blog/engineering/self-hosted-mongo/\">Self-Hosted MongoDB</a>.</p>\n<p>Alright then, picking from where we left off, let's get started with the data migration in MongoDB.</p>\n<p>Now, the basic steps to migrate data from one MongoDB to another would be:</p>\n<ol>\n<li>Create a zipped backup of the existing data</li>\n<li>Dump the data in a new DB</li>\n</ol>\n<p>This is very straight forward when the source database is not online because we know that there won't be any new documents created/updated during the migration process.\nLet's look at simple migration first before diving into the live scenario.</p>\n<hr />\n<h1 id=\"migrating-from-an-offline-database-in-mongodb\" style=\"position:relative;\"><a href=\"#migrating-from-an-offline-database-in-mongodb\" aria-label=\"migrating from an offline database in mongodb permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Migrating from an offline database in MongoDB</h1>\n<h2 id=\"creating-a-backup\" style=\"position:relative;\"><a href=\"#creating-a-backup\" aria-label=\"creating a backup permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Creating a backup</h2>\n<p>We're going to use an existing utility program <a href=\"https://docs.mongodb.com/database-tools/mongodump/\">mongodump</a> for creating the database backup.</p>\n<p>Run this command in the source database server</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mongodump --host=&quot;hostname:port&quot; \\</span>\n<span class=\"grvsc-line\">  --username=&quot;username&quot; --password=&quot;password&quot; \\</span>\n<span class=\"grvsc-line\">  --authenticationDatabase &quot;admin&quot; \\</span>\n<span class=\"grvsc-line\">  --db=&quot;db name&quot; --collection=&quot;collection name&quot; --query=&#39;json&#39; \\</span>\n<span class=\"grvsc-line\">  --forceTableScan -v --gzip --out ./dump</span></code></pre>\n<p><strong><code>--host</code></strong>: The source MongoDB hostname along with the port. It defaults to <code>localhost:27017</code>. If it is a connection string you can use this option <code>—-uri=\"mongodb://username:password@host1[:port1]...\"</code></p>\n<p><strong><code>--username</code></strong>: Specifies a username to authenticate to a MongoDB database that uses authentication.</p>\n<p><strong><code>--password</code></strong>: Specifies a password to authenticate to a MongoDB database that uses authentication.</p>\n<p><strong><code>--authenticationDatabase</code></strong>: Specifies the authentication database where the specified <code>--username</code> has been created.</p>\n<blockquote>\n<p>If you do not specify an authentication database or a database to export, mongodump assumes the admin database holds the user's credentials.</p>\n</blockquote>\n<p><strong><code>--db</code></strong>: Specifies the database to take a backup from. If you do not specify a database, mongodump collects from all databases in this instance.</p>\n<blockquote>\n<p>Alternatively, you can also specify the database directly in the <a href=\"https://docs.mongodb.com/database-tools/mongodump/#cmdoption-mongodump-uri\">URI connection string</a> i.e. <code>mongodb://username:password@uri/dbname</code>. <br /> Providing a connection string while also using <code>--db</code> and specifying conflicting information <strong>will result in an error</strong>.</p>\n</blockquote>\n<p><strong><code>--collection</code></strong>: Specifies a collection to backup. If you do not specify a collection, this option copies all collections in the specified database or instance to the dump files.</p>\n<p><strong><code>--query</code></strong> : Provides a <a href=\"https://docs.mongodb.com/manual/reference/glossary/#term-json-document\">JSON document</a> as a query that optionally limits the documents included in the output of mongodump. <br />\nYou must enclose the query document in single quotes <code>('{ ... }')</code> to ensure that it does not interact with your  environment.<br />\nThe query must be in <a href=\"https://docs.mongodb.com/manual/reference/mongodb-extended-json\">Extended JSON v2 format (either relaxed or canonical/strict mode)</a>, including enclosing the field names and operators in quotes e.g. <code>'{ \"created_at\": { \"\\$gte\": ISODate(...) } }'</code>.</p>\n<blockquote>\n<p>To use the <code>--query</code> option, you must also specify the <a href=\"https://docs.mongodb.com/database-tools/mongodump/#cmdoption-mongodump-collection\"><code>--collection</code></a> option.</p>\n</blockquote>\n<p><strong><code>--forceTableScan</code></strong>: Forces mongodump to scan the data store directly. Typically, mongodump saves entries as they appear in the index of the <code>_id</code> field. <br /></p>\n<blockquote>\n<p>If you specify a query <code>--query</code>, mongodump will use the most appropriate index to support that query. <br /><strong>Hence , you cannot use <a href=\"https://docs.mongodb.com/database-tools/mongodump/#cmdoption-mongodump-forcetablescan\"><code>--forceTableScan</code></a> with the <a href=\"https://docs.mongodb.com/database-tools/mongodump/#cmdoption-mongodump-query\"><code>--query</code></a> option</strong>.</p>\n</blockquote>\n<p><strong><code>--gzip</code></strong>: Compresses the output. If mongodump outputs to the dump directory, the new feature compresses the individual files. The files have the suffix <code>.gz</code>.</p>\n<p><strong><code>--out</code></strong>: Specifies the directory where mongodump will write <a href=\"https://docs.mongodb.com/manual/reference/glossary/#term-bson\"><code>BSON</code></a> files for the dumped databases. By default, mongodump saves output files in a directory named dump in the current working directory.</p>\n<h2 id=\"restoring-the-backup\" style=\"position:relative;\"><a href=\"#restoring-the-backup\" aria-label=\"restoring the backup permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Restoring the backup</h2>\n<p>We will use a utility program called <a href=\"https://docs.mongodb.com/database-tools/mongorestore/\"><code>mongorestore</code></a> for restoring the database backup.</p>\n<p>Copy the backup directory dump to the new Database instance and run the following command:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mongorestore --uri=&quot;mongodb://user:password@host:port/?authSource=admin&quot; \\</span>\n<span class=\"grvsc-line\">  --drop --noIndexRestore --gzip -v ./dump</span></code></pre>\n<p>Replace the credentials with the new database credentials. Unline in the previous step, the <code>--authenticationDatabase</code> option is specified in the URI string.</p>\n<p>Also, use <code>--gzip</code> if used while creating the backup.</p>\n<p><strong><code>--drop</code></strong>: Before restoring the collections from the dumped backup, drops the collections from the target database. It does not drop collections that are not in the backup.\n<strong><code>--noIndexRestore</code></strong>: Prevents mongorestore from restoring and building indexes as specified in the corresponding mongodump output.</p>\n<blockquote>\n<p>If you want to change name of the database while restoring, you can do so using <br /><code>--nsFrom=\"old_name.*\" --nsTo=\"new_name.*\"</code> options.<br /><br />However, it won’t work if you were to migrate with <code>oplogs</code> which is a requirement in migration from an online instance.</p>\n</blockquote>\n<hr />\n<h1 id=\"migrating-from-an-online-database-in-mongodb\" style=\"position:relative;\"><a href=\"#migrating-from-an-online-database-in-mongodb\" aria-label=\"migrating from an online database in mongodb permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Migrating from an online database in MongoDB</h1>\n<p>The only challenge with migrating from an online database is not able to pause the updates during migration. So here is the overview of the steps,</p>\n<ol>\n<li>Run an initial bulk migration with <code>oplogs</code> capture</li>\n<li>Run a sync job to mitigate the database connection switch latency</li>\n</ol>\n<blockquote>\n<p>Now, to capture <code>oplogs</code>, a replica set must be initialized in the source and destination databases. This is because the <code>oplogs</code> are captured from <strong><code>local.oplog.rs</code></strong> namespace, which is created after initializing a replica set. <br /><br />You can follow <a href=\"https://medium.com/swlh/self-hosted-mongodb-deployment-7f1b6fb4973f#1cdf\">this guide</a> to configure a replica set.</p>\n</blockquote>\n<h2 id=\"initial-migration-with-oplog-capture\" style=\"position:relative;\"><a href=\"#initial-migration-with-oplog-capture\" aria-label=\"initial migration with oplog capture permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Initial Migration with Oplog Capture</h2>\n<p>Oplogs, in simple words, are the operation logs created per operation in the database. They represent a partial document state or, in other words, the database state. So we are going to capture any updates in our old database during the migration process using these <code>oplogs</code>.</p>\n<p>Run the mongodump program with the following options,</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mongodump --uri=&quot;.../?authSource=admin&quot; \\</span>\n<span class=\"grvsc-line\">  --forceTableScan --oplog \\</span>\n<span class=\"grvsc-line\">  --gzip -v --out ./dump</span></code></pre>\n<p><strong><code>--oplog</code></strong>: Creates a file named <code>oplog.bson</code> as part of the <code>mongodump</code> output. The <code>oplog.bson</code> file, located in the top level of the output directory, contains <code>oplog</code> entries that occur during the mongodump operation. This file provides an effective point-in-time snapshot of the state of our database instance.</p>\n<h2 id=\"restore-the-data-with-oplog-replay\" style=\"position:relative;\"><a href=\"#restore-the-data-with-oplog-replay\" aria-label=\"restore the data with oplog replay permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Restore the data with oplog replay</h2>\n<p>In order to replay the oplogs, a special role is required. Let's create and assign the role to the database user being used for migration.</p>\n<h3 id=\"create-the-role\" style=\"position:relative;\"><a href=\"#create-the-role\" aria-label=\"create the role permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Create the role</h3>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">db.createRole({</span>\n<span class=\"grvsc-line\">  role: &quot;interalUseOnlyOplogRestore&quot;,</span>\n<span class=\"grvsc-line\">  privileges: [</span>\n<span class=\"grvsc-line\">    {</span>\n<span class=\"grvsc-line\">      resource: { anyResource: true },</span>\n<span class=\"grvsc-line\">      actions: [ &quot;anyAction&quot; ] </span>\n<span class=\"grvsc-line\">    }</span>\n<span class=\"grvsc-line\">  ],</span>\n<span class=\"grvsc-line\">  roles: []</span>\n<span class=\"grvsc-line\">})</span></code></pre>\n<h3 id=\"assign-the-role\" style=\"position:relative;\"><a href=\"#assign-the-role\" aria-label=\"assign the role permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Assign the role</h3>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">db.grantRolesToUser(</span>\n<span class=\"grvsc-line\">  &quot;admin&quot;,</span>\n<span class=\"grvsc-line\">  [{ role:&quot;interalUseOnlyOplogRestore&quot;, db:&quot;admin&quot; }]</span>\n<span class=\"grvsc-line\">);</span></code></pre>\n<p>Now you can restore using the mongorestore program with the following options,</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mongorestore --uri=&quot;mongodb://admin:.../?authSource=admin&quot; \\</span>\n<span class=\"grvsc-line\">  --oplogReplay </span>\n<span class=\"grvsc-line\">  --gzip -v ./dump</span></code></pre>\n<p>In the above command, using the same user <strong><code>admin</code></strong> with whom the role was associated.</p>\n<p><strong><code>--oplogReplay</code></strong>: After restoring the database dump, replays the oplog entries from a bson file and restores the database to the point-in-time backup captured with the mongodump <code>--oplog</code> command.</p>\n<h2 id=\"mitigating-database-connection-switch-latency\" style=\"position:relative;\"><a href=\"#mitigating-database-connection-switch-latency\" aria-label=\"mitigating database connection switch latency permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Mitigating database connection switch latency</h2>\n<p>Alright, so far we are done with most of the heavy lifting. The only thing that remains is maintaining consistency between the databases during the connection switch in our application servers.</p>\n<blockquote>\n<p>If you're running MongoDB version 3.6+, it's better to go for the Change Stream approach, which is a event-based mechanism introduced to capture changes in your database in an optimized way. Here is an article that covers it : <a href=\"https://www.mongodb.com/blog/post/an-introduction-to-change-streams\">An Introduction to Change Streams</a></p>\n</blockquote>\n<p>Check out the <a href=\"https://gist.github.com/cnp96/7be1756f7eb76ea78c9b832966e84dbf#file-delta-sync-sh\">generic sync script</a>, which you can run as a CRON job every minute.</p>\n<p>Update the variables in this script and run as</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">$ ./delta-sync.sh from_epoch_in_milliseconds</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"># from_epoch_in_milliseconds is automatically picked with every iteration if not supplied</span></code></pre>\n<p>Or you can set up a cron job to run this every minute.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">* * * * * ~/delta-sync.sh</span></code></pre>\n<p>The output can be monitored with the following command (I'm running RHEL 8, refer to your OS guide for cron output)</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">$ tail -f /var/log/cron | grep CRON</span></code></pre>\n<p>This is a sample sync log.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">CMD (~/cron/dsync.sh)</span>\n<span class=\"grvsc-line\">CMDOUT (INFO: Updated log registry to use new timestamp on next run.)</span>\n<span class=\"grvsc-line\">CMDOUT (INFO: Created sync directory: /home/ec2-user/cron/dump/2020-11-03T19:01:01Z)</span>\n<span class=\"grvsc-line\">CMDOUT (Fetching oplog in range [2020-11-03T19:00:01Z - 2020-11-03T19:01:01Z])</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:02.319+0000#011dumping up to 1 collections in parallel)</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:02.334+0000#011writing local.oplog.rs to /home/ec2-user/cron/dump/2020-11-03T19:01:01Z/local/oplog.rs.bson.gz)</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:04.943+0000#011local.oplog.rs  0)</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:04.964+0000#011local.oplog.rs  0)</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:04.964+0000#011done dumping local.oplog.rs (0 documents))</span>\n<span class=\"grvsc-line\">CMDOUT (INFO: Dump success!)</span>\n<span class=\"grvsc-line\">CMDOUT (INFO: Replaying oplogs...)</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:05.030+0000#011using write concern: &{majority false 0})</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:05.054+0000#011will listen for SIGTERM, SIGINT, and SIGKILL)</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:05.055+0000#011connected to node type: standalone)</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:05.055+0000#011mongorestore target is a directory, not a file)</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:05.055+0000#011preparing collections to restore from)</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:05.055+0000#011found collection local.oplog.rs bson to restore to local.oplog.rs)</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:05.055+0000#011found collection metadata from local.oplog.rs to restore to local.oplog.rs)</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:05.055+0000#011restoring up to 4 collections in parallel)</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:05.055+0000#011replaying oplog)</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:05.055+0000#011applied 0 oplog entries)</span>\n<span class=\"grvsc-line\">CMDOUT (2020-11-03T19:01:05.055+0000#0110 document(s) restored successfully. 0 document(s) failed to restore.)</span>\n<span class=\"grvsc-line\">CMDOUT (INFO: Restore success!)</span></code></pre>\n<p>You can stop this script after verifying that no more <code>oplogs</code> are being created, i.e., when source DB went offline.</p>\n<p>This concludes the complete self-hosted MongoDB data migration guide. If you want to learn more about MongoDB here is a useful resource on <a href=\"https://www.loginradius.com/blog/engineering/mongodb-as-datasource-in-golang/\">how to use MongoDB as datasource in goLang</a>.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","headings":[{"value":"Migrating from an offline database in MongoDB","depth":1},{"value":"Creating a backup","depth":2},{"value":"Restoring the backup","depth":2},{"value":"Migrating from an online database in MongoDB","depth":1},{"value":"Initial Migration with Oplog Capture","depth":2},{"value":"Restore the data with oplog replay","depth":2},{"value":"Create the role","depth":3},{"value":"Assign the role","depth":3},{"value":"Mitigating database connection switch latency","depth":2}],"fields":{"slug":"/engineering/live-data-migration-mongodb/"},"frontmatter":{"metatitle":null,"metadescription":null,"description":"This article covers the guide to migrate data from offline or live MongoDB instance using oplog replay alongside mitigate connection switch latency with existing utilities.","title":"How to Migrate Data In MongoDB","canonical":null,"date":"December 14, 2020","updated_date":null,"tags":["MongoDB"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.4184397163120568,"src":"/static/90f92104129cbc152fa1d9a64c58d7be/03979/index.png","srcSet":"/static/90f92104129cbc152fa1d9a64c58d7be/f5f11/index.png 200w,\n/static/90f92104129cbc152fa1d9a64c58d7be/6d133/index.png 400w,\n/static/90f92104129cbc152fa1d9a64c58d7be/03979/index.png 800w,\n/static/90f92104129cbc152fa1d9a64c58d7be/30425/index.png 1000w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Chinmaya Pati","github":"cnp96","bio":"I'm an avid FOSS enthusiast and contributor interested in system design, web-dev, UI/UX, data-driven technologies, and DevOps.","avatar":null}}}},"pageContext":{"id":"8d1c6c7b-3313-5137-89fa-d096a463287e","fields__slug":"/engineering/live-data-migration-mongodb/","__params":{"fields__slug":"engineering"}}},"staticQueryHashes":["1171199041","1384082988","1711371485","1753898100","2100481360","229320306","23180105","528864852"]}