{"id":561,"date":"2018-02-10T19:20:17","date_gmt":"2018-02-10T11:20:17","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=561"},"modified":"2018-02-19T16:49:20","modified_gmt":"2018-02-19T08:49:20","slug":"how-to-migrate-your-mongodb-database-instance-with-mongodump-mongorestore-tar-and-scp","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-migrate-your-mongodb-database-instance-with-mongodump-mongorestore-tar-and-scp\/","title":{"rendered":"How to migrate your MongoDB database instance with mongodump, mongorestore, tar and scp"},"content":{"rendered":"<p>Migration of <a href=\"https:\/\/www.mongodb.com\/\" rel=\"noopener\" target=\"_blank\">MongoDB<\/a> database is part and parcel of <a href=\"https:\/\/www.techcoil.com\/blog\/tag\/software-development-operations\" rel=\"noopener\" target=\"_blank\">DevOps<\/a>, especially when you are running your own projects.  <\/p>\n<p>MongoDB provides us with two utilities for performing database migration - <a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/program\/mongodump\/\" rel=\"noopener\" target=\"_blank\"><code>mongodump<\/code><\/a> and <a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/program\/mongorestore\/\" rel=\"noopener\" target=\"_blank\"><code>mongorestore<\/code><\/a>. <\/p>\n<p>A simple MongoDB database migration can be performed in 5 steps:<\/p>\n<ol>\n<li>\nUse the <code>mongodump<\/code> command to export the data of a MongoDB database instance as files in the source server's filesystem.<\/li>\n<li>Use the <code>tar<\/code> command to compress the exported files as a single <code>.tar.gz<\/code> file in the source server's filesystem.<\/li>\n<li>Use the <code>scp<\/code> command to send the <code>.tar.gz<\/code> file from the source server to the destination server.<\/li>\n<li>Use the <code>tar<\/code> command to decompress the <code>.tar.gz<\/code> file at the destination server.<\/li>\n<li>Use the <code>mongorestore<\/code> to import the extracted files into the destination MongoDB database.<\/li>\n<\/ol>\n<p>This post discusses how you can perform MongoDB database migration with utilities provided by MongoDB and most Linux servers.<\/p>\n<h2>Exporting data from MongoDB server with mongodump<\/h2>\n<p>The way you run <code>mongodump<\/code> depends on whether access control is turned on for your MongoDB server.<\/p>\n<h3>Exporting data from MongoDB server without access control<\/h3>\n<p>When you have a MongoDB server listening at the default port of localhost, you run the following command to export the data from the MongoDB database instance, <strong><code>a_database<\/code><\/strong>, onto the server's file system.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmongodump --db a_database\r\n<\/pre>\n<p>After the command completes, you will find a directory named as <strong><code>dump<\/code><\/strong> in the current working directory. Inside the <strong><code>dump<\/code><\/strong> directory, you will find the <strong><code>a_database<\/code><\/strong> directory. And inside the <code>a_database<\/code> directory, you will find <code>.bson<\/code> and <code>.json<\/code> files that describe the contents of the MongoDB database instance that you are exporting data from.<\/p>\n<h3>Exporting data from MongoDB server with access control<\/h3>\n<p>Suppose you have a MongoDB user account with the username <strong><code>auser<\/code><\/strong> and password <strong><code>apassword<\/code><\/strong> created inside the database <strong><code>admin<\/code><\/strong>, you run the following command to extract the data from the database instance <strong><code>a_database<\/code><\/strong> from the MongoDB server listening at the default port of localhost: <\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmongodump --username auser --password apassword \\\r\n--authenticationDatabase admin --db a_database\r\n<\/pre>\n<p>As with the case of exporting data from a MongoDB server without access control, you will find similar output in the current working directory when the <code>mongodump<\/code> command completes.<\/p>\n<p>Make sure that the user that is able to read from the database, if not you will encounter the following error: <\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nFailed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.\r\n<\/pre>\n<h2>Using the tar command to compress the exported files as a single .tar.gz file in the source server's filesystem<\/h2>\n<p>When we compress all the contents inside the dump directory into a single .tar.gz file, we can transfer the exported data over to the destination server's file system easily. You can use the <code>tar<\/code> command to compress all the contents inside the <code>dump<\/code> directory into a single <code>.tar.gz<\/code> file:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ntar -czvf dump.tar.gz dump\r\n<\/pre>\n<p>When the command completes, you will be able to see the <code>dump.tar.gz<\/code> file in your current working directory. The <code>dump.tar.gz<\/code> file will contain the <code>dump<\/code> directory along with its subdirectories and files.<\/p>\n<h2>Using the scp command to send the .tar.gz into the destination server<\/h2>\n<p>If there is a SSH server running at your destination server, you can use the <code>scp<\/code> command to send the <strong><code>dump.tar.gz<\/code><\/strong> file from your current working directory to the destination server. Suppose your destination server is reachable by the domain <strong><code>example.com<\/code><\/strong>, has a user account with username <strong><code>root<\/code><\/strong> and has a <strong><code>\/var\/receiving directory<\/code><\/strong>, you can run the following command to send your <strong><code>dump.tar.gz<\/code><\/strong> file to the destination server:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nscp dump.tar.gz root@example.com:\/var\/receiving\/dump.tar.gz\r\n<\/pre>\n<h2>Using the tar command to decompress the single .tar.gz file back into output files generated by mongodump<\/h2>\n<p>Once the file transfer is done, you can use the <code>ssh<\/code> command to get into your destination server to extract the contents of dump.tar.gz:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nssh root@example.com\r\n<\/pre>\n<p>Once you had logged into the destination server, you can run the following commands to extract the <strong><code>dump<\/code><\/strong> directory from <strong><code>dump.tar.gz<\/code><\/strong>:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncd \/var\/receiving\r\ntar -zxvf dump.tar.gz\r\n<\/pre>\n<p>After the command completes, you will find the output files generated by <code>mongodump<\/code> at the <strong><code>\/var\/receiving<\/code><\/strong> folder.<\/p>\n<h2>Importing data into MongoDB server with mongorestore<\/h2>\n<p>As with the case of exporting data from MongoDB server, the way you run <code>mongorestore<\/code> depends on whether access control is turned on for your MongoDB.<\/p>\n<h3>Importing data into MongoDB server with no access control<\/h3>\n<p>When you have a MongoDB server listening at the default port of localhost, you run the following command to import the data from the <strong><code>dump<\/code><\/strong> directory into the MongoDB database instance, <strong><code>a_database<\/code><\/strong>:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmongorestore --drop --db a_database dump\/a_database\r\n<\/pre>\n<p>This command will drop all existing collections inside the MongoDB database instance <strong><code>a_database<\/code><\/strong> before populating the contents from <code>dump\/a_database<\/code>. When the command completes, the MongoDB database instance at the destination server will contain the migrated data from the source server.<\/p>\n<h3>Importing data into MongoDB server with access control<\/h3>\n<p>Suppose you have a MongoDB user account with the username <strong><code>auser<\/code><\/strong> and password <strong><code>apassword<\/code><\/strong> created inside the database <strong><code>admin<\/code><\/strong>, you run the following command to import the data from <strong><code>dump\/a_database<\/code><\/strong> into the MongoDB server listening at the default port of localhost: <\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmongorestore --drop --db a_database --username auser \r\n\\ --password apassword --authenticationDatabase admin dump\/a_database\r\n<\/pre>\n<p>Similar to the case of importing data into the MongoDB server without access control, the command will drop all existing collections inside the MongoDB database instance <code>a_database<\/code> before populating the contents from <code>dump\/a_database<\/code>. After the command completes, the MongoDB database instance at the destination server will contain the migrated data from the source server.<\/p>\n<p>Make sure that the user that is able to write to the database, if not you will encounter the following error: <\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nFailed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.\r\n<\/pre>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-93\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-93&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-93&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F561&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Migration of MongoDB database is part and parcel of <a href=\"https:\/\/www.techcoil.com\/blog\/tag\/software-development-operations\" rel=\"noopener\" target=\"_blank\">DevOps<\/a>, especially when you are running your own projects.  <\/p>\n<p>MongoDB provides us with two utilities for performing database migration &#8211; <a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/program\/mongodump\/\" rel=\"noopener\" target=\"_blank\"><code>mongodump<\/code><\/a> and <a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/program\/mongorestore\/\" rel=\"noopener\" target=\"_blank\"><code>mongorestore<\/code><\/a>. <\/p>\n<p>A simple MongoDB database migration can be performed in 5 steps:<\/p>\n<ol>\n<li>\nUse the <code>mongodump<\/code> command to export the data of a MongoDB database instance as files in the source server&#8217;s filesystem.<\/li>\n<li>Use the <code>tar<\/code> command to compress the exported files as a single <code>.tar.gz<\/code> file in the source server&#8217;s filesystem.<\/li>\n<li>Use the <code>scp<\/code> command to send the <code>.tar.gz<\/code> file from the source server to the destination server.<\/li>\n<li>Use the <code>tar<\/code> command to decompress the <code>.tar.gz<\/code> file at the destination server.<\/li>\n<li>Use the <code>mongorestore<\/code> to import the extracted files into the destination MongoDB database.<\/li>\n<\/ol>\n<p>This post discusses how you can perform MongoDB database migration with utilities provided by MongoDB and most Linux servers.<\/p>\n","protected":false},"author":1,"featured_media":950,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[504,156,152,500,499,503,350,498],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/mongodb-logo.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-93","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/561"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=561"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/561\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/950"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}