How To Backup and Migrate a MongoDB Database – CloudSavvy IT

Posted on

Migrating databases is one thing each system administrator should do in some unspecified time in the future. Fortunately, MongoDB offers in-built instructions for creating and restoring from backups, making migration to a brand new server simpler.

Utilizing mongodump to Create a Backup

mongodump is an easy command that may create a backup file of a database and its collections you can restore from. It will require some downtime whereas the backup is completed and the brand new server is introduced up.

If you happen to don’t need downtime, you can do a cluster migration by including a brand new node to your cluster, setting that as the first write supply, after which sizzling swapping over to the brand new node. That is made a lot simpler for those who’re utilizing MongoDB Atlas, their managed database service.

mongodump is lots easier. You’ll have to create a listing for the backups:

sudo mkdir /var/backups/mongobackups

After which run mongodump, passing it in a database parameter and an output location:

sudo mongodump --db databasename --out /var/backups/mongobackups/backup

You may as well manually dump particular collections with the --collection flag.

Mongodump will be run on a reside database, and solely takes just a few seconds to create the backup. Nonetheless, any writes to the database will successfully be misplaced, because you’re shifting servers. Due to this, you’ll need to disable visitors earlier than creating the dump.

Restoring From Backup

You’ll have to switch the backup file from the previous server to the brand new server. This may be carried out by downloading it over FTP, then importing it to the brand new server, however for giant backups it’s greatest to determine a direct connection and switch it over utilizing scp.

You should utilize the next command, changing the usernames and hostnames with values in your servers.

scp person@SRC_HOST:/var/backups/mongobackups/FILENAME  person@DEST_HOST:~/FILENAME

Then, after getting the backup on the brand new server, you may load from the backup. You’ll in fact want MongoDB put in on the brand new server.

To take action, you need to use the mongorestore command:

mongorestore <choices> <connection-string> <file to revive>

It’s best to instantly see the brand new desk obtainable within the new database.

After verifying that all the pieces has transferred over correctly, you’ll have to swap visitors to the brand new server, probably by updating your DNS data. If you happen to’re on AWS, or an analogous supplier with Elastic IP addresses, you may swap over the deal with to level to the brand new server, which gained’t require a DNS replace.

Change association on elastic IP address

Transfering The Complete Disk (Non-obligatory)

Optionally, for those who’re simply shifting to a extra highly effective server, you may switch over your entire boot drive, which ought to copy the database together with the remainder of the server configuration.

On this case, you’ll need to use the rsync command to instantly add the information to the goal server. rsync will join utilizing SSH and sync up the 2 folders; on this case, we need to push the native folder to the distant server:

sudo rsync -azAP / --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/misplaced+discovered"} username@remote_host:/

That’s the entire command. It’s best to see a progress bar because it completes the switch (utilizing compression with the -z flag), and when it’s carried out, you’ll see the recordsdata within the goal folder on the brand new server. You might have to run this a number of occasions to repeat every folder; you need to use this online rsync command generator to generate the command for every run.

Source link

Gravatar Image
I love to share everything with you

Leave a Reply

Your email address will not be published. Required fields are marked *