How to dump and restore my Scalingo for MongoDB®

There are two ways to dump a distant database and restore the data in your Scalingo database. The first one involves dumping the data on your local workstation and the second one involves doing the same operations from within a Scalingo one-off container (see application tasks).

Dump and Restore From Your Local Workstation

To dump and restore your database from your local workstation, you need the connection string to connect to your database and a way to access your database.

You can get the connection string of your database in your Scalingo application environment. Go to the Environment page of your dashboard or run the following command:

$ scalingo --app my-app env | grep MONGO_URL

Your database connection string conforms to the syntax of a generic URI:

mongodb://<username>:<password>@<host>:<port>/<db>

There are two ways to access your database from your local workstation:

  • Setting up a tunnel
  • Making your database accessible from anywhere on the Internet

Setup the Tunnel

$ scalingo --app my-app db-tunnel SCALINGO_MONGO_URL

Building tunnel to my-db.mongo.a.osc-fr1.scalingo-dbs.com:30000
You can access your database on:
127.0.0.1:10000

In this situation you need to use a different connection string than the one from your application environment. The <host> part is replaced by 127.0.0.1 and the <port> is replaced by 10000.

Internet Accessibility

In order to make your database reachable from anywhere on the internet, head to your database dashboard. You first need to force TLS connections to your database. Then toggle Internet Accessibility to make it reachable from the Internet.

In this situation, the connection string to use is exactly the same as the one from your application environment.

Dump

This command will create a dump directory in your current working directory.

$ mongodump --username <username> --password <password> --host <host> --port <port> --db <db>

You can also use the URI format instead of splitting the URI in parts:

$ mongodump --uri $SCALINGO_MONGO_URL

Common Error

If you have this error when executing the mongodump command:

error reading collection: Failed to parse: { find: "<collection name>", skip: 0,
snapshot: true, $readPreference: { mode: "secondaryPreferred" }, $db: "<db>" }.
Unrecognized field 'snapshot'.

You can use --forceTableScan option to resolve the issue.

Restore

This command will restore a dump into your database.

$ mongorestore --username <username> --password <password> --host <host> --port <port> --db <db> <dump directory OR bson file>

You can also use the URI format instead of splitting the URI in parts:

$ mongorestore --uri $SCALINGO_MONGO_URL --db <db> <dump directory OR bson file>

In addition you may use the –drop option to delete the existing data in the database.

Dump and Restore With Force TLS

When the Force TLS option is active, you should add these flags : --ssl and --sslCAFile

Example for a dump :

$ mongodump --ssl --sslCAFile <CAFile.pem> --username <username> --password <password> --host <host> --port <port> --db <db>

Example for a restore :

$ mongorestore --ssl  --sslCAFile <CAFile.pem> --username <username> --password <password> --host <host> --port <port> --db <db> <dump directory OR bson file>

Dump and Restore From Scalingo One-off Container

You can dump and restore your database remotely using the command-line-tool and a one-off container (see application tasks).

The advantage of this method is the network. From your workstation you don’t always have a good bandwidth. From our infrastructure, data transfers will be way faster.

You need to install the MongoDB® CLI tools in the one-off before executing mongodump or mongorestore:

$ scalingo --app my-app run bash

[00:00] Scalingo ~ $ dbclient-fetcher mongo

Dump

$ scalingo --app my-app run bash

[00:00] Scalingo ~ $ dbclient-fetcher mongo
[00:00] Scalingo ~ $ mongodump --username user --password pass --host my-db.mongo.a.osc-fr1.scalingo-dbs.com --port 30000 --db my-db

# Do something with the dump, i.e send it with FTP or to an external server

Restore

$ scalingo --app my-app run bash

[00:00] Scalingo ~ $ dbclient-fetcher mongo

# Get a dump from a remote place, with 'curl' or 'ftp'

[00:00] Scalingo ~ $ mongorestore --uri $SCALINGO_MONGO_URL --db my-db dump/my-db

After exiting the one-off container, the dump will be lost, you’ve to do something with it in the container.


Suggest edits

How to dump and restore my Scalingo for MongoDB®

©2024 Scalingo