# How to Back Up and Restore a Database in MongoDB

> URL: https://www.atlantic.net/dedicated-server-hosting/how-to-back-up-and-restore-a-database-in-mongodb/ | Published: 2024-08-02 | Updated: 2024-08-17 | Author: Hitesh Jethva

Backing up your database is essential to protect against data loss and ensure data integrity. Restoring a database from a backup allows you to recover your data in case of accidental deletion, corruption, or hardware failure.

In this tutorial, we will cover how to back up and restore a MongoDB database.

## Backing Up a MongoDB Database

The **mongodump** command is a utility provided by MongoDB that creates a database backup. It exports data from your MongoDB database into BSON files, which can be restored later.

To perform a basic backup of your MongoDB database, open your terminal and run the following command:

```bash
mongodump --db your_database_name --out /path/to/backup/directory
```

Here’s a breakdown of the command:

- **–db your_database_name**: Specifies the name of the database you want to back up.
- **–out /path/to/backup/directory**: Specifies the directory where the backup files will be stored.

For example, to backup a database named tutorialDB to /tmp directory, run:

```bash
mongodump --db tutorialDB --out /tmp/tutorialDB_backup
```

Output:

```bash
2024-07-15T12:29:20.438+0000	writing tutorialDB.products to /tmp/tutorialDB_backup/tutorialDB/products.bson
2024-07-15T12:29:20.439+0000	done dumping tutorialDB.products (1 document)
2024-07-15T12:29:20.442+0000	writing tutorialDB.users to /tmp/tutorialDB_backup/tutorialDB/users.bson
2024-07-15T12:29:20.443+0000	done dumping tutorialDB.users (1 document)
```

If your MongoDB instance is running on a non-default host or port, you can specify these using the **–host** and **–port** options:

```bash
mongodump --host your_host --port your_port --db your_database_name --out /path/to/backup/directory
```

**Example:**

```bash
mongodump --host localhost --port 27017 --db tutorialDB --out /tmp/tutorialDB_backup
```

If your MongoDB instance requires authentication, you can include the **–username** and **–password** options:

```bash
mongodump --username your_username --password your_password --authenticationDatabase admin --db your_database_name --out /path/to/backup/directory
```

**Example:**

```bash
mongodump --username admin --password secret --authenticationDatabase admin --db tutorialDB --out /tmp/tutorialDB_backup
```

## Restoring a MongoDB Database

The **mongorestore** command is a utility for restoring a MongoDB database from BSON files created by mongodump.

To restore a MongoDB database, open your terminal and run the following command:

```bash
mongorestore --db your_database_name /path/to/backup/directory/your_database_name
```

Here’s a breakdown of the command:

- **–db your_database_name**: Specifies the database name to which you want to restore.
- **/path/to/backup/directory/your_database_name**: Specifies the directory where the backup files are located.

**Example:**

```bash
mongorestore --db tutorialDB /tmp/tutorialDB_backup/tutorialDB
```

Output:

```bash
2024-07-15T12:35:23.456+0000    restoring tutorialDB.collection1 from /tmp/tutorialDB_backup/tutorialDB/collection1.bson
2024-07-15T12:35:23.456+0000    restoring tutorialDB.collection2 from /tmp/tutorialDB_backup/tutorialDB/collection2.bson
Restore completed successfully.
```

If your MongoDB instance is running on a non-default host or port, you can specify these using the **–host** and **–port** options:

```bash
mongorestore --host your_host --port your_port --db your_database_name /path/to/backup/directory/your_database_name
```

**Example:**

```bash
mongorestore --host localhost --port 27017 --db tutorialDB /tmp/tutorialDB_backup/tutorialDB
```

If your MongoDB instance requires authentication, you can include the **–username** and **–password** options:

```bash
mongorestore --username your_username --password your_password --authenticationDatabase admin --db your_database_name /path/to/backup/directory/your_database_name
```

**Example:**

```bash
mongorestore --username admin --password secret --authenticationDatabase admin --db tutorialDB /tmp/tutorialDB_backup/tutorialDB
```

## Conclusion

In this article, we have covered the essential steps for backing up and restoring a MongoDB database using the **mongodump** and **mongorestore** commands. Regular backups are crucial for data integrity and security while knowing how to restore data ensures you can recover from any unforeseen data loss. You can now perform a MongoDB back up and restoration on [dedicated server hosting](https://www.atlantic.net/dedicated-server-hosting/) from Atlantic.Net!
