ANS Documentation

Improve This Doc
  • Cloud
  • Domains and DNS management
  • Backup and High Availability
  • eCommerce Stacks
  • Security
  • Email
  • Monitoring and usage management
  • Networking
  • Operating systems
    • Linux
      • Basics
      • Apache
      • Control Panels
      • FTP
      • Magento
      • Miscellaneous
      • MySQL
      • NGINX
      • PHP
      • PHP-FPM
      • Setting up Websites
      • NFS
      • VPN
      • Percona
      • SSH
      • SSL
      • Git
      • Redis
      • Elasticsearch
      • MongoDB
      • Ncdu
    • VMware ESXi
    • Windows
  • Webcelerator
  • MyUKFast
  • Home >
  • Operating systems >
  • Linux >
  • MongoDB >
  • How to Back Up & Restore a MongoDB Database

How to Back Up & Restore a MongoDB Database¶

File Formats¶

MongoDB uses the mongodump tool to export databases. This can export to the following formats;

  • BSON (Default for data)

  • JSON (Default for collection metadata)

  • CSV (specify with --type csv)

  • TSV (specify with --type tsv)

BSON is the recommended database export format.

Exporting all databases¶

To export all databases, cd to a directory path of your choosing, and run the following command

mongodump

This will automatically create a /dump folder in that location. Alternatively, specify a folder path with the following command.

mongodump --out /your/file/path

Exporting a single database¶

To export a single database, cd to a directory path of your choosing, and run the following command

mongodump --db=yourdb

Exporting from a Remote Host¶

You can use the mongodump utility to connect to a remote instance using the below syntax.

mongodump --host="mongodb.yourdomain.com:27017" <your other dump requirements>

Restoring a MongoDB Database¶

.. warning::
   If requesting a database restore from UKFast Support, please provide the directory for your database dumps. Without this, only `/var/lib/mongodb` would be restored.

To restore from a database backup you will need to use the mongorestore command. To import a dump, you just need to specify the location of the restore folder, e.g.

mongorestore dump/

This will create the database and collections specified within the dump data.

To import a specific collection from this dump, you would use the following syntax

mongorestore --nsInclude=testdb.orders /dump

Restoring from JSON¶

Though not recommended, if your dump is in JSON v2.0 format you can use the mongoimport command to import your database.

mongoimport --db yourdb --file /path/to/database/dump.json

If using JSON v1.0, you should use the above with the addition of the --legacy flag.

Restoring to a Remote Host¶

You can use the mongorestore utility to connect to a remote instance using the below syntax.

mongorestore --host="mongodb.yourdomain.com:27017" <your other import requirements>

Next Article > Ncdu

  • Useful Links
  • SMB
  • Enterprise
  • Channel
  • Public Sector
  • ANS Data Centres
  • About ANS
  • Careers
  • Blog
  • Get in touch
  •  
  • Sales 0800 458 4545
  • Support 0800 230 0032
  • Get in touch

© ANS Group Limited | Terms and Conditions | Corporate Guidance | Sitemap
ANS Group Limited, registered in England and Wales, company registration number 03176761, registered office 1 Archway, Birley Fields, Manchester M15 5QJ