|
| 1 | +--- |
| 2 | +meta: |
| 3 | + title: Back up and restore Document Databases with MongoDB® tools |
| 4 | + description: This page shows you how to back up and restore Document Databases with MongoDB® command line tools |
| 5 | +content: |
| 6 | + h1: Back up and restore Document Databases with MongoDB® tools |
| 7 | + paragraph: This page shows you how to back up and restore Document Databases with MongoDB® command line tools |
| 8 | +tags: databases mongodb ferretdb document backup database-nodes |
| 9 | +categories: |
| 10 | + - managed-databases |
| 11 | + - postgresql-and-mysql |
| 12 | +--- |
| 13 | + |
| 14 | +Managed Document Databases provide fully-managed document Database Instances, with a MongoDB®-compatible API layer over a PostgreSQL engine to store and retrieve data. |
| 15 | + |
| 16 | +This means you can use MongoDB® command line tools to use and manage your Document Databases. |
| 17 | + |
| 18 | +This tutorial shows how to backup and restore your Document Databases with the MongoDB® `mongodump` and `mongorestore` command line database tools. |
| 19 | + |
| 20 | +<Macro id="requirements" /> |
| 21 | + |
| 22 | +<Message type="requirement"> |
| 23 | + - You have an account and are logged into the [Scaleway console](https://console.scaleway.com/) |
| 24 | + - You have a [MongoDB®-compatible client](https://www.mongodb.com/try/download/shell) installed |
| 25 | + - You have [created a Document Database Instance](/managed-databases/document-database/how-to/create-a-database-instance) |
| 26 | + - You have [mongodump and mongorestore tools](https://www.mongodb.com/docs/database-tools/installation/installation/) installed |
| 27 | +</Message> |
| 28 | + |
| 29 | +<Message type="important"> |
| 30 | + This tutorial shows you basic restore and backup operations. If you need additional options for the operations, refer to the [official MongoDB® documentation](https://www.mongodb.com/docs/manual/tutorial/backup-and-restore-tools/). |
| 31 | + |
| 32 | + Make sure you keep the `--ssl` and `--sslCAFile=<file.pem>` options in your commands, as you will need to authenticate using the default SSL certificate when you connect to your Database Instance. |
| 33 | +</Message> |
| 34 | + |
| 35 | +## How to back up a Document Database Instance |
| 36 | + |
| 37 | +To create backups, we will use the `mongodump` tool, which can create backups for an entire Database Instance, logical database or collection. It can also use a query to back up part of a collection. |
| 38 | + |
| 39 | +1. Navigate to the [Scaleway console](https://console.scaleway.com/). |
| 40 | +2. Click **Document Databases** under **Managed Databases** on the side menu. A list of your Database Instances displays. |
| 41 | +3. Click the database name or <Icon name="more" /> > **More info** to access the Database Instance information page. |
| 42 | +4. Download the Database Instance's SSL certificate. |
| 43 | + <Message type="important"> |
| 44 | + Make sure you know the path to the certificate in your local machine, as it will be used in a later step. |
| 45 | + </Message> |
| 46 | +5. Run the following command, replacing the values according to the table below. |
| 47 | + ``` |
| 48 | + mongodump \ |
| 49 | + --host=<ip-address> \ |
| 50 | + --port=<port> \ |
| 51 | + --username=<username> \ |
| 52 | + --password="<password>" \ |
| 53 | + --out=/path/to/backup \ |
| 54 | + --ssl \ |
| 55 | + --sslCAFile=<file.pem> \ |
| 56 | + --authenticationMechanism=PLAIN |
| 57 | + ``` |
| 58 | + |
| 59 | + `host` |
| 60 | + : the IP address of your Database Instance |
| 61 | + |
| 62 | + `port` |
| 63 | + : the connection port of your Database Instance |
| 64 | + |
| 65 | + `username` |
| 66 | + : the username of the database user created upon Database Instance creation |
| 67 | + |
| 68 | + `password` |
| 69 | + : the password of the database user created upon Database Instance creation |
| 70 | + |
| 71 | + `out` |
| 72 | + : the path to the folder where you want the backups to be stored on your machine |
| 73 | + |
| 74 | + `sslCAFile` |
| 75 | + : the path to where your SSL certificate is stored |
| 76 | + |
| 77 | + `authenticationMechanism` |
| 78 | + : the list of authentication mechanisms the server accepts |
| 79 | + |
| 80 | + If no response is returned, the operation was successful. You can make sure the backup occurred by checking if new files were added to the destination folder. |
| 81 | + |
| 82 | +## How to restore from a backup |
| 83 | + |
| 84 | +You can restore either an entire database backup or a subset of a backup using `mongorestore`. |
| 85 | + |
| 86 | +The tool restores binary backups created using `mongodump`. |
| 87 | + |
| 88 | +You can restore the backed up data to any database within any Database Instance. |
| 89 | + |
| 90 | +Run the following command, replacing the values according to the table above. You must specify the name of the destination `db`. |
| 91 | +``` |
| 92 | +mongorestore \ |
| 93 | + --host=<ip-address> \ |
| 94 | + --port=<port> \ |
| 95 | + --username=<username> \ |
| 96 | + --password=<password> \ |
| 97 | + /path/to/backup \ |
| 98 | + --db=rdb \ |
| 99 | + --ssl \ |
| 100 | + --sslCAFile=<file.pem> \ |
| 101 | + --authenticationMechanism=PLAIN |
| 102 | +``` |
| 103 | +`db` |
| 104 | +: the name of the database to which you want to restore the backup |
| 105 | + |
| 106 | +If the procedure was successful, you will see an output like the following: |
| 107 | + |
| 108 | +``` |
| 109 | +2023-09-04T17:50:29.893+0200 1 document(s) restored successfully. 0 document(s) failed to restore. |
| 110 | +``` |
0 commit comments