{"id":157890,"date":"2021-01-19T16:00:21","date_gmt":"2021-01-19T13:00:21","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-backup-and-migrate-a-mongodb-database-cloudsavvy-it\/"},"modified":"2021-01-19T16:00:21","modified_gmt":"2021-01-19T13:00:21","slug":"how-to-backup-and-migrate-a-mongodb-database-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-backup-and-migrate-a-mongodb-database-cloudsavvy-it\/","title":{"rendered":"#How To Backup and Migrate a MongoDB Database \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2a731e5a017\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2a731e5a017\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-backup-and-migrate-a-mongodb-database-cloudsavvy-it\/#Using_mongodump_to_Create_a_Backup\" >Using mongodump to Create a Backup<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-backup-and-migrate-a-mongodb-database-cloudsavvy-it\/#Restoring_From_Backup\" >Restoring From Backup<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-backup-and-migrate-a-mongodb-database-cloudsavvy-it\/#Transfering_The_Entire_Disk_Optional\" >Transfering The Entire Disk (Optional)<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How To Backup and Migrate a MongoDB Database \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9064\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/b8cd001f27ec4e080394f32fa8ef9838\/p\/uploads\/2021\/01\/44b8c1b1.png\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Migrating databases is something every system administrator will have to do at some point. Luckily, MongoDB provides built in commands for creating and restoring from backups, making migration to a new server easier.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_mongodump_to_Create_a_Backup\"><\/span>Using mongodump to Create a Backup<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>mongodump is a simple command that will create a backup file of a database and its collections that you can restore from. This will require some downtime while the backup is done and the new server is brought up.<\/p>\n<p>If you don\u2019t want downtime, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/engineering.checkr.com\/mongodb-cluster-migration-with-zero-downtime-f4e294c29bef\">you can do a cluster migration<\/a> by adding a new node to your cluster, setting that as the primary write source, and then hot sw<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ing over to the new node. This is made much easier if you\u2019re using MongoDB Atlas, their managed database service.<\/p>\n<p><code>mongodump<\/code>\u00a0is a lot simpler. You\u2019ll need to create a directory for the backups:<\/p>\n<pre>sudo mkdir \/var\/backups\/mongobackups<\/pre>\n<p>And then run <code>mongodump<\/code>, passing it in a database parameter and an output location:<\/p>\n<pre>sudo mongodump --db databasename --out \/var\/backups\/mongobackups\/backup<\/pre>\n<p>You can also manually dump specific collections with the <code> --collection<\/code> flag.<\/p>\n<p>Mongodump can be run on a live database, and only takes a few seconds to create the backup. However, any writes to the database will effectively be lost, since you\u2019re moving servers. Because of this, you\u2019ll want to disable traffic before creating the dump.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Restoring_From_Backup\"><\/span>Restoring From Backup<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You\u2019ll need to transfer the backup file from the old server to the new server. This can be done by downloading it over FTP, then uploading it to the new server, but for large backups it\u2019s best to establish a direct connection and transfer it over using <code>scp<\/code>.<\/p>\n<p>You can use the following command, replacing the usernames and hostnames with values for your servers.<\/p>\n<pre>scp\u00a0user@SRC_HOST:\/var\/backups\/mongobackups\/FILENAME\u00a0 user@DEST_HOST:~\/FILENAME<\/pre>\n<p>Then, once you have the backup on the new server, you can load from the backup.\u00a0You will of course need MongoDB installed on the new server.<\/p>\n<p>To do so, you can use the <code>mongorestore<\/code>\u00a0command:<\/p>\n<pre>mongorestore &lt;options&gt; &lt;connection-string&gt; &lt;file to restore&gt;<\/pre>\n<p>You should im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely see the new table available in the new database.<\/p>\n<p>After verifying that everything has transferred over properly, you\u2019ll need to swap traffic to the new server, likely by updating your DNS records. If you\u2019re on AWS, or a similar provider with Elastic IP addresses, you can swap over the address to point to the new server, which won\u2019t require a DNS update.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"imgchk9 alignnone wp-image-4192 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/ef0f29f336bd367cec2e0081e73f4d4c\/p\/uploads\/2020\/03\/4a912322.png\" alt=\"Change association on elastic IP address\" width=\"590\" height=\"207\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Transfering_The_Entire_Disk_Optional\"><\/span>Transfering The Entire Disk (Optional)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Optionally, if you\u2019re just moving to a more powerful server, you can transfer over the entire boot drive, which should copy the database along with the rest of the server configuration.<\/p>\n<p>In this case, you\u2019ll want to use the\u00a0<code>rsync<\/code>\u00a0command to directly upload the data to the target server. <code>rsync<\/code>\u00a0will connect using SSH and sync up the two folders; in this case, we want to push the local folder to the remote server:<\/p>\n<pre>sudo rsync -azAP \/ --exclude={\"\/dev\/*\",\"\/proc\/*\",\"\/sys\/*\",\"\/tmp\/*\",\"\/run\/*\",\"\/mnt\/*\",\"\/media\/*\",\"\/lost+found\"} username@remote_host:\/<\/pre>\n<p>That\u2019s the whole command. You should see a progress bar as it completes the transfer (using compression with the <code>-z<\/code> flag), and when it\u2019s done, you\u2019ll see the files in the target folder on the new server. You may have to run this multiple times to copy each folder; you can use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/rsync.snipline.io\/\">this online <code>rsync<\/code>\u00a0command generator<\/a> to generate the command for each run.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/8866\/how-to-backup-and-migrate-a-mongodb-database\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How To Backup and Migrate a MongoDB Database \u2013 CloudSavvy IT&#8221; Migrating databases is something every system administrator will have to do at some point. Luckily, MongoDB provides built in commands for creating and restoring from backups, making migration to a new server easier. Using mongodump to Create a Backup mongodump is a simple command&#8230;<\/p>\n","protected":false},"author":1,"featured_media":157891,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/44b8c1b1.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-157890","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/157890","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=157890"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/157890\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/157891"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=157890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=157890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=157890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}