Резервное копирование mongodb на windows

Время на прочтение6 мин

Количество просмотров20K

Привет Хабр, и привет читателям. Не очень долго пришлось ждать выхода моей новой статьи. Спасибо всем, кто дал feedback по моей прошлой статье.

Было очень приятно пообщаться с единомышленниками. Отдельный респект, тем кто дал совет как можно улучшить статью.

Сегодня я хотел бы поделиться со всеми читателями об одной системе управления базами данных, не требующая описания схемы таблиц, правильно — это MongoDB СУБД считается одной из классических примеров NoSQL-систем. Документы состоят из пар ключ-значение, которые являются основной единицей данных в MongoDB. Коллекции содержат наборы документов и функции, которые эквивалентны таблицам реляционной базы данных. MongoDB — это база данных, появившаяся в середине 2000-х годов. Про установку не буду ничего описывать все и так понятно из официального гайда Всем добро пожаловать.
https://docs.mongodb.com/v5.0/administration/install-community/

В этой статье буду показывать о том, как настроить резервное копирование данных, и потом восстановить. На самом деле это не так все сложно, и даже скажу больше интуитивно понятно:) Не буду задерживать всех своей писаниной и сразу приступим к делу. Мануал заточен на то, что у вас уже установлена СУБД из официального гайда. Единственное скажу, что я буду показывать все на машинке с Линуксом, ОС Ubuntu 20.04.2 LTS

Формат данных в MongoDB

Одним из популярных стандартов обмена данными и их хранения является JSON (JavaScript Object Notation). JSON эффективно описывает сложные по структуре данные. Способ хранения данных в MongoDB в этом плане похож на JSON, хотя формально JSON не используется. Для хранения в MongoDB применяется формат, который называется BSON (БиСон) или сокращение от binary JSON.

BSON позволяет работать с данными быстрее: быстрее выполняется поиск и обработка. Хотя надо отметить, что BSON в отличие от хранения данных в формате JSON имеет небольшой недостаток: в целом данные в JSON-формате занимают меньше места, чем в формате BSON, с другой стороны, данный недостаток с лихвой окупается скоростью.

Подключаемся к нашей СУБД, создадим для Резервной копии три базы данных под названием HABR1, HABR2, HABR3. Для наполнения этих бд создадим пару коллекций.

root@backup-server:~# mongo

> use HABR
db.createCollection("posts")
db.createCollection("address")
db.createCollection("phone")

> use HABR2
db.createCollection("posts2")
db.createCollection("address2")
db.createCollection("phone2")

> use HABR3
db.createCollection("posts3")
db.createCollection("address3")
db.createCollection("phone3")
db.createCollection("phonehabr")

Создали бд, проверим, все ли нормально с ними :)
> show dbs

switched to db admin
admin> show dbs
HABR         57.3 kB
HABR2        24.6 kB
HABR3        24.6 kB
admin         184 kB
config       73.7 kB
local        81.9 kB

Как мы видим наши бд создались, давайте проверим есть ли в них коллекции которые мы создавали ранее.

admin> use HABR
switched to db HABR
HABR> show collections
addreshabr
address3
addresshabr
phone5
phonehabr
posts
postshabr
HABR> use HABR2
switched to db HABR2
HABR2> show collections
addres
phone
posts
HABR2> use HABR3
switched to db HABR3
HABR3> show collections
addres3
phone3
posts3

Все коллекции на месте. Можно приступать к первому варианту Резервной копии.
Резервную копию будем делать из под рута.

Делается все одной командой:

mongodump —host=localhost —gzip -d HABR —archive=/tmp/backup-db-habr.gz

root@backup-server:/tmp/test# ls -la
total 20
drwxr-xr-x  2 root root 4096 Aug  3 18:33 .
drwxrwxrwt 13 root root 4096 Aug  3 18:32 ..
-rw-r--r--  1 root root  638 Aug  3 18:32 backup-db-habr.gz
-rw-r--r--  1 root root  416 Aug  3 18:33 backup-db-habr2.gz
-rw-r--r--  1 root root  423 Aug  3 18:33 backup-db-habr3.gz

Как видим, Резервная Копия создалась успешно.

Восстановление из такого бэкапа.

mongorestore —gzip —archive=backup-db-habr.gz

В данном примере делаем Резервную копию одной командой, и складываем в архив, есть несколько вариантов, как можно сделать резервное копирование, постараюсь описать в этой статье как можно больше вариантов.

Способ 2Резервное копирование всех баз данных, без сжатия данных.

Создадим директорию хранения такого бэкапа
mkdir -p /tmp/backup/
Запустим резервное копирование:
mongodump —out /tmp/backup/

После успешного резервного копирования перейдем в директорию с нашей базой и посмотрим что там лежит. Все правильно здесь хранятся коллекции БЕЗ СЖАТИЯ в BSON и JSON формате.

root@backup-server:/tmp/backup/HABR# ls -la
total 36
drwxr-xr-x 2 root root 4096 Aug  3 21:08 .
drwxr-xr-x 5 root root 4096 Aug  3 21:09 ..
-rw-r--r-- 1 root root    0 Aug  3 21:08 addreshabr.bson
-rw-r--r-- 1 root root  177 Aug  3 21:08 addreshabr.metadata.json
-rw-r--r-- 1 root root    0 Aug  3 21:08 address3.bson
-rw-r--r-- 1 root root  175 Aug  3 21:08 address3.metadata.json
-rw-r--r-- 1 root root    0 Aug  3 21:08 addresshabr.bson
-rw-r--r-- 1 root root  178 Aug  3 21:08 addresshabr.metadata.json
-rw-r--r-- 1 root root    0 Aug  3 21:08 phone5.bson
-rw-r--r-- 1 root root  173 Aug  3 21:08 phone5.metadata.json
-rw-r--r-- 1 root root    0 Aug  3 21:08 phonehabr.bson
-rw-r--r-- 1 root root  176 Aug  3 21:08 phonehabr.metadata.json
-rw-r--r-- 1 root root    0 Aug  3 21:08 posts.bson
-rw-r--r-- 1 root root  172 Aug  3 21:08 posts.metadata.json
-rw-r--r-- 1 root root    0 Aug  3 21:08 postshabr.bson
-rw-r--r-- 1 root root  176 Aug  3 21:08 postshabr.metadata.json

Восстановление из такого бэкапа:
mongorestore —drop —dir /tmp/backup

Параметр —drop используется для удаления коллекции перед импортом(если она существует),во избежания ошибки duplicate key errors Этот параметр —drop следует применять с осторожностью.

Восстановление определенной коллекции (например, коллекции postshabr в базе данных HABR) с бекапа всех баз данных:

mongorestore —drop -v —dir /root/backup —nsInclude ‘habr.postshabr’

Восстановление всех баз данных и всех коллекций, за исключением определенной коллекции(например, коллекции postshabr в базе habrdb)

mongorestore —drop -v —dir /root/backup —nsExclude ‘habr.postshabr’

Способ 3 — Резервное копирование всех баз данных с сжатием.mongodump —gzip —out /tmp/backup

root@backup-server:/tmp/backup/HABR# ls -la
total 44
drwxr-xr-x  2 root root 4096 Aug  3 21:51 .
drwxr-xr-x 13 root root 4096 Aug  3 21:51 ..
-rw-r--r--  1 root root    0 Aug  3 21:08 addres.bson
-rw-r--r--  1 root root   23 Aug  3 21:51 addres.bson.gz
-rw-r--r--  1 root root  173 Aug  3 21:08 addres.metadata.json
-rw-r--r--  1 root root  152 Aug  3 21:51 addres.metadata.json.gz
-rw-r--r--  1 root root    0 Aug  3 21:08 phone.bson
-rw-r--r--  1 root root   23 Aug  3 21:51 phone.bson.gz
-rw-r--r--  1 root root  172 Aug  3 21:08 phone.metadata.json
-rw-r--r--  1 root root  151 Aug  3 21:51 phone.metadata.json.gz
-rw-r--r--  1 root root    0 Aug  3 21:08 posts.bson
-rw-r--r--  1 root root   23 Aug  3 21:51 posts.bson.gz
-rw-r--r--  1 root root  172 Aug  3 21:08 posts.metadata.json
-rw-r--r--  1 root root  153 Aug  3 21:51 posts.metadata.json.gz

Восстановление с такого бекапа.

mongorestore —gzip —drop —dir /mnt/backup

Способ 4 — Резервное копирование всех баз данных с сжатием в один архив(.gz)mongodump —gzip —archive=/tmp/backup/mybackup.gz

Восстановление с такого бекапа:

mongorestore —gzip —drop —archive=/tmp/backup/mybackup.gz

Способ 5 — Резервное копирование определенной базы данных.mongodump —gzip -d HABR2

root@backup-server:/tmp/backup/dump/HABR2# mongodump --gzip -d HABR2
2021-08-04T00:17:24.033+0300	writing HABR2.posts to dump/HABR2/posts.bson.gz
2021-08-04T00:17:24.033+0300	writing HABR2.phone to dump/HABR2/phone.bson.gz
2021-08-04T00:17:24.035+0300	writing HABR2.addres to dump/HABR2/addres.bson.gz
2021-08-04T00:17:24.036+0300	done dumping HABR2.posts (0 documents)
2021-08-04T00:17:24.036+0300	done dumping HABR2.phone (0 documents)
2021-08-04T00:17:24.037+0300	done dumping HABR2.addres (0 documents)

Восстановление с такого бекапа:mongorestore —gzip —dir /tmp/backup/

Способ 6 — Резервное копирование одной коллекции address из базы данных HABR2.

mongodump —gzip -d HABR2 -c address

root@backup-server:/tmp/backup/dump/HABR2# ls -la
total 16
drwxr-xr-x 2 root root 4096 Aug  3 22:05 .
drwxr-xr-x 3 root root 4096 Aug  3 22:05 ..
-rw-r--r-- 1 root root   23 Aug  3 22:05 addres.bson.gz
-rw-r--r-- 1 root root  152 Aug  3 22:05 addres.metadata.json.gz

Просмотр содержимого bson-файла:

zcat posts.bson.gz | bsondump —pretty

root@backup-server:/# zcat posts.bson.gz | bsondump --pretty
2021-08-03T22:23:57.113+0300	0 objects found

Способ 7 — Бекап всей базы HABR2 за исключением одной коллекции addressmongodump —gzip -d HABR2 —excludeCollection=address

root@backup-server:/tmp/backup/dump/HABR2# mongodump --gzip -d HABR2 --excludeCollection=addres
2021-08-04T00:16:41.482+0300	writing HABR2.phone to dump/HABR2/phone.bson.gz
2021-08-04T00:16:41.483+0300	writing HABR2.posts to dump/HABR2/posts.bson.gz
2021-08-04T00:16:41.484+0300	done dumping HABR2.phone (0 documents)
2021-08-04T00:16:41.485+0300	done dumping HABR2.posts (0 documents)

Надеюсь, что любому кто столкнется с такой задачей, прочитав эту статью будет понятно, как работать с этой СУБД. Всем спасибо за чтение данной статьи!

27 августа, 2021 12:12 пп
1 353 views
| Комментариев нет

Development, Ubuntu

MongoDB – одна из самых популярных систем управления баз данных NoSQL. MongoDB известна своей масштабируемостью, ошибкоустойчивостью, надежностью и простотой использования. В этом мануале вы узнаете, как создать резервную копию БД, восстановить и переместить образец данных MongoDB.

Импорт и экспорт БД подразумевает работу с данными в удобочитаемом формате, совместимом с другими программами. Операции резервного копирования и восстановления, напротив, создают или используют двоичные данные, специфичные для MongoDB – это позволяет сохранить не только согласованность и целостность данных, но и их атрибуты. Таким образом, для перемещения данных обычно рекомендуется использовать резервное копирование и восстановление (если исходная и целевая системы совместимы).

Требования

  • Виртуальный сервер (мы используем сервер Ubuntu 20.04, настроенный в соответствии с этим руководством, включая пользователя sudo и брандмауэр).
  • Подготовленная к работе установка MongoDB (читайте мануал Установка MongoDB в Ubuntu 20.04)
  • Тестовая база данных MongoDB, импортированная согласно мануалу Импорт и экспорт баз данных MongoDB.

В этом руководстве все команды, требующие привилегий root, должны запускаться от имени пользователя с привилегиями sudo (не указано иное).

1: Использование JSON и BSON

Прежде чем продолжить работу с этим мануалом, необходимо разобраться с базовыми понятиями. Если у вас есть опыт работы с другими системами баз данных NoSQL, такими как Redis, вы заметите некоторое сходство между ними и MongoDB.

Во-первых, разберемся с форматами. Для хранения своей информации MongoDB использует форматы JSON и BSON (двоичный JSON). JSON – это удобочитаемый формат, который идеально подходит для экспорта и импорта ваших данных. Вы можете управлять экспортированными данными с помощью любого инструмента, поддерживающего JSON, включая простой текстовый редактор.

Документ json выглядит так:

{"address":[
    {"building":"1007", "street":"Park Ave"},
    {"building":"1008", "street":"New Ave"},
]}

С JSON удобно работать, но он поддерживает не все типы данных, доступные в BSON. Это означает, что при использовании JSON данные могут стать недостоверными. Потому для резервного копирования и восстановления лучше использовать формат BSON.

Во-вторых, вам не нужно явно создавать базу данных MongoDB. Если для импорта вы указали несуществующую БД, она будет создана автоматически. Еще лучше обстоит дело со структурой коллекций (таблиц базы данных). В отличие от других движков, MongoDB автоматически воссоздает структуру при вставке первого документа (строки данных).

В-третьих, в MongoDB чтение или вставка больших объемов данных, таких как задачи в этой статье, может потребовать значительных ресурсов и занять большую часть вашего процессора, памяти и дискового пространства. Это очень важно, учитывая, что MongoDB часто используется именно для больших баз данных. Самое простое решение этой проблемы – запускать экспорт и резервное копирование в ночное время или в непиковые часы.

В-четвертых, согласованность информации может быть проблемой, если ваш сервер MongoDB перегружен, а информация на нем изменяется во время экспорта или резервного копирования БД. Одним из возможных решений этой проблемы является репликация.

Функции импорта и экспорта можно использовать для резервного копирования и восстановления данных, однако есть более эффективные способы обеспечить полную целостность ваших БД MongoDB. Для резервного копирования следует использовать команду mongodump. Для восстановления – команду mongorestore. Давайте посмотрим, как они работают.

2: Резервное копирование MongoDB с помощью mongodump

Сначала мы рассмотрим резервное копирование базы данных MongoDB.

Важным аргументом команды mongodump является –db, он указывает имя БД, резервную копию которой вы хотите создать. Если вы не укажете имя базы данных, mongodump создаст резервную копию всех ваших баз данных.

Второй важный аргумент – это –out, он определяет каталог, в который будут выгружены данные. Для примера давайте создадим резервную копию БД newdb и сохраним ее в каталоге /var/backups/mongobackups. В идеале каждая из наших резервных копий появится в каталоге с текущей датой (в формате /var/backups/mongobackups/10-29-20).

Сначала создайте необходимый каталог /var/backups/mongobackups:

sudo mkdir /var/backups/mongobackups

А теперь запустите команду mongodump:

sudo mongodump --db newdb --out /var/backups/mongobackups/`date +"%m-%d-%y"`

Вы увидите такой вывод:

2020-10-29T19:22:36.886+0000    writing newdb.restaurants to
2020-10-29T19:22:36.969+0000    done dumping newdb.restaurants (25359 documents)

Обратите внимание, что в указанном выше пути к каталогу мы использовали date +”%m-%d-%y”, что автоматически установит текущую дату. Это позволит нам создавать резервные копии внутри каталога, например, /var/backups/10-29-20/.   Это особенно удобно, когда мы автоматизируем резервное копирование.

На этом этапе у вас есть полная резервная копия базы данных newdb в каталоге /var/backups/mongobackups/10-29-20/newdb/. В этой резервной копии есть все, чтобы правильно восстановить newdb и сохранить точность и целостность данных.

Как правило, резервные копии должны создаваться регулярно, желательно тогда, когда сервер наименее загружен. Чтобы сделать это, можно установить команду mongodump как задание cron, чтобы она выполнялась в определенное время, например, каждый день в 03:03.

Для этого откройте crontab, редактор cron:

sudo crontab -e

Обратите внимание, что при запуске sudo crontab вы будете редактировать задания cron для пользователя root. Рекомендуется делать именно так, потому что если вы установите crons для другого пользователя, задачи могут работать некорректно (особенно если ваш профиль sudo требует проверки пароля).

В командную строку crontab введите следующую команду mongodump:

3 3 * * * mongodump --out /var/backups/mongobackups/`date +"%m-%d-%y"`

В приведенной выше команде мы намеренно опускаем аргумент –db, потому что резервное копирование обычно требуется всем базам данных.

В зависимости от размеров вашей БД MongoDB, рано или поздно у вас закончится дисковое пространство – накопится много резервных копий. Вот потому также рекомендуется регулярно удалять или сжимать старые копии.

Например, чтобы удалить все резервные копии старше семи дней, вы можете использовать следующую команду bash:

find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \;

Как и предыдущую команду mongodump, вы можете добавить ее как задачу cron. Она должна запускаться непосредственно перед началом следующего резервного копирования, например, в 03:01. Снова откройте crontab:

sudo crontab -e

После этого вставьте следующую строку:

1 3 * * * find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \;

Сохраните и закройте файл.

Все эти задачи обеспечат правильное выполнение резервного копирования ваших баз данных MongoDB.

3: Восстановление и миграция MongoDB с помощью mongorestore

После восстановления БД MongoDB из резервной копии у вас будет точная копия данных MongoDB, созданная в определенное время (включая все индексы и типы). Это особенно полезно, если вы хотите перенести свои базы данных MongoDB на другую машину. Для восстановления MongoDB мы будем использовать команду mongorestore, которая работает с двоичными резервными копиями, созданными командой mongodump.

Давайте продолжим работу с нашей тестовой БД newdb и посмотрим, как восстановить ее из ранее созданной резервной копии. Сначала укажем имя базы данных с помощью аргумента –nsInclude (в нашем случае это newdb). Символ * нужен в команде для восстановления всех коллекций. Чтобы восстановить одну коллекцию, например restaurants, используйте вместо звездочки newdb.restaurants.

Затем, используя аргумент –drop, мы сбросим целевую БД, чтобы резервная копия была восстановлена ​​в чистой базе данных. В качестве последнего аргумента мы укажем каталог последней резервной копии, который будет выглядеть примерно так: /var/backups/mongobackups/10-29-20/newdb/.

Если у вас есть резервная копия с меткой времени, вы можете восстановить ее с помощью этой команды:

sudo mongorestore --db newdb --drop /var/backups/mongobackups/10-29-20/newdb/

Вы получите примерно такой вывод:

2020-10-29T19:25:45.825+0000    the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2020-10-29T19:25:45.826+0000    building a list of collections to restore from /var/backups/mongobackups/10-29-20/newdb dir
2020-10-29T19:25:45.829+0000    reading metadata for newdb.restaurants from /var/backups/mongobackups/10-29-20/newdb/restaurants.metadata.json
2020-10-29T19:25:45.834+0000    restoring newdb.restaurants from /var/backups/mongobackups/10-29-20/newdb/restaurants.bson
2020-10-29T19:25:46.130+0000    no indexes to restore
2020-10-29T19:25:46.130+0000    finished restoring newdb.restaurants (25359 documents)
2020-10-29T19:25:46.130+0000    done

В приведенном выше примере мы восстанавливаем данные на том же сервере, на котором мы создали резервную копию. Если вы хотите перенести данные на другой сервер и использовать тот же метод, вам следует скопировать на новую машину ваш каталог резервных копий (в нашем мануале это /var/backups/mongobackups/10-29-20/newdb/).

Заключение

Ни один сервер MongoDB в среде производства не должен работать без надежной стратегии резервного копирования. В этом мануале вы выполнили важные задачи, связанные с резервным копированием, восстановлением и переносом баз данных MongoDB.

Tags: MongoDB, NoSQL

How to Backup and Restore Mongodb Databases 💾

Table of contents:

  1. Introduction
  2. Mongodb Database Tools
  3. mongodump Command
    • List of mongodump Options
  4. mongorestore Command
    • List of mongorestore Options
  5. bsondump Command

Introduction

In the world of MongoDB, ensuring the safety and availability of your data is crucial. MongoDB provides two powerful command-line tools, mongodump and mongorestore that simplify the process of creating backups and restoring data.

In this article, we will explore how these tools work, their key features, and how they can be leveraged to backup and restore your MongoDB databases.

Whether you are a developer, system administrator, or database manager, understanding the capabilities of mongodump and mongorestore is essential for maintaining data integrity and accuracy .

Table of contents ☝️


Mongodb Database Tools

The MongoDB Database Tools is a collection of command-line utilities provided by MongoDB that assist in various database operations such as backups, restores, data migrations, performance analysis, and more.

These tools include mongodump, mongorestore, mongoexport, mongoimport, mongostat, bsondump, and others, each serving a specific purpose to help developers, administrators, and database managers efficiently manage and interact with MongoDB databases.

In this article, our focus will be on two key components mongodump and mongorestore.

mongodump is responsible for creating backups, while mongorestore is used to restore data from those backups into MongoDB. Together, these tools offer a comprehensive backup and restore solution for MongoDB databases.

To add mongodump and mongorestore to your system, you need to follow these steps:

  1. Download MongoDB: Visit the official MongoDB website (https://www.mongodb.com/) and download the appropriate MongoDB Community Edition for your operating system.

  2. Download MongoDB Database Tools: From the following link :
    (https://www.mongodb.com/try/download/database-tools)

  3. According to your system, you can download a zip file or a msi file, the following steps describe how to add the tools in a windows environment :

    • Extract the zip file and after extracting it, copy both mongodump.exe and mongorestore.exe to the location where you your mongodb server is installed (e.g., C:\Program Files\MongoDB\Server{{version}}\bin)

    • Or install the msi file. then setup all the tools. This will create a folder called tools in C:\Program Files\MongoDB that contain all the command-line utilities provided by MongoDB. Then you need to add the directory of those binaries files to your system’s PATH environment variable.

Table of contents ☝️


mongodump

The mongodump is a utility for creating database backups for Mongodb. To start using mongodump, you need to open a command prompt or terminal window and run the mongodump command followed by the desired options and arguments. Here’s the basic syntax:

Running mongodump command without any specific options or arguments will dump all databases from the MongoDB server.

By default, mongodump creates a folder called dump in the current working directory (the same directory you run the mongodump command), and within that directory, it creates a subdirectory for each database being dumped.

Inside each database directory, it creates:

  1. BSON files (binary representation of MongoDB documents) for each collection in that database. This file contains the actual data. The bsondump utility tool is useful for reading the output BSON files generated by mongodump.

  2. JSON file contains metadata information about the database and its collections. It includes details such as collection names, indexes, options, and other metadata related to the structure and organization of the database.

Table of contents ☝️

List of mongodump options

Here are some commonly used options for mongodump:

  • --version: check version
  • --out: backup to specific folder
  • --db: backup specific database
  • --collection: backup specific collection
  • --excludeCollection: exclude specific collections
  • --query: filter data before backup with query
  • --queryFile: filter data before backup with query file
  • --gzip: backup compression
  • --archive: backup to a compressed archive
  • --authenticationDatabase: using authentication
  • --host --port: backup from a remote server

--version
check version

To display the version information for mongodump installed on your system, you can use the --version option:

mongodump options ☝️

--out
backup to specific folder

We use the flag -o, --out <directory>.

To dump the entire databases to a folder called backup:

To dump the entire databases to a folder called mongodata in drive D:

mongodump --out D:\mongodata

mongodump options ☝️

--db
backup specific database

We use the flag -d, --db <database>.

mongodump --db blog --out backup

The command creates a backup of a specific database named blog inside a folder called backup:

Unfortunately, Cannot use --db multiple time in the same command, in other words, we can backup only a single database per command.

mongodump options ☝️

--collection
backup specific collection

We use the flag -c, --collection <collection>.

mongodump --db blog --collection comments --out D:\

The commnad will dump a collection called comments exist in a database called blog. You will find the dumped data in a folder has the same name as the database called blog in drive D, so it is better to specify name of the folder you want to backup your data to.

Unfortunately, Cannot use --collection multiple time in the same command, we can backup only a single collection per command.

mongodump options ☝️

--excludeCollection
exclude specific collections when backup

We use the flag --excludeCollection <collection>.

mongodump --db blog --excludeCollection comments

This command will create a backup of the blog database, excluding the comments collection. The resulting backup will include all other collections from the blog database, except for comments.

unlike --collection option, you can specify multiple --excludeCollection options to exclude multiple collections in a single backup command.

mongodump --db blog --excludeCollection comments --excludeCollection posts

This command creatse a backup for all the collections the blog database except for comments and posts collections.

mongodump options ☝️

--query
filter documents included in the backup with a query

The --query option in mongodump allows you to specify a query expression to filter the documents that will be included in the backup based on certain criteria. Only the documents matching the query will be backed up.

For example, suppose you have a database named blog with a collection named posts. If you want to back up only the documents from the posts collection that have a field called category equal to "technology", you would run the following command:

mongodump -d blog -c posts --query '{\"category\":\"technology\"}'

To avoid any issues with the shell interpreting the json string, we use \ before any double quotation " and enclose the query brackets {} between a single quotation '

mongodump options ☝️

--queryFile
filter documents included in the backup with a query file

Alternatively, you can also store the query filter in a separate file and pass it to the mongodump command using the --queryFile option. Here’s an example:

  1. Create a file named query.json with the following contents:

    { "category": "technology" }
  2. Use the mongodump command with the --queryFile option:

    mongodump -d blog -c posts --queryFile query.json -o backup

This should avoid any issues with the shell interpreting the quotes in the query filter.

mongodump options ☝️

--gzip
backup compression

The --gzip option enables compression of the output files during the backup process. This can significantly reduce the size of the backup files and save storage space.

mongodump --gzip -d blog -o backup

Using the --gzip option is especially beneficial when dealing with large databases, as it helps reduce the backup file size, making it more efficient for storage and transfer.

By default, it creates a .gz file for each BSON and JSON file in the backup.

mongorestore automatically detects and decompresses the gzip-compressed BSON files during the restore process.

mongodump options ☝️

--archive
backup to a compressed archive

The --archive option allows you to create a single archive file that contains the backup data instead of generating individual files for each collection. You can specify the path and filename for the archive file you want to create. This can be useful for situations where you want to store the backup in a compressed format or transfer it as a single file.

mongodump --archive=backup.archive 

This command will create a single archive file named backup.archive that contains the backup data for the all databases.

You can specify other options suh as : --db, --collection, --excludeCollectio, …etc but the out option are not allowed to be used with the --archive option.

mongodump --db blog --collection posts --archive=D:\backup\blog.archive 

The command creates a single archive file called blog.archive that contains the backup data for the posts collection in the blog database, saved at the location "D:\backup". Just make sure the backup folder in drive D is existed.

mongodump options ☝️

--authenticationDatabase
using authentication

The --authenticationDatabase option is used to specify the <authentication database> when connecting to a MongoDB server that requires authentication. This option is necessary when you are connecting with a username and password to a MongoDB deployment that uses authentication.

The <authentication database> is the database where you created the user or where the user has the necessary privileges to perform operations. It is often admin database.

mongodump --authenticationDatabase admin --username myuser --password mypassword  --db mydatabase --out /backup

The command authenticate against the admin database, and then perform the backup of the specified mydatabase. The resulting backup files will be stored in the backup directory.

mongodump options ☝️

--host and --port
backup from a remote server

We use the both --host and --port options to connect to a remote server in order to perform the backup:

mongodump --host <remote_host> --port <remote_port>
  • The --host option specifies the hostname or IP address of the MongoDB server.
  • The --port option specifies the port number on which the MongoDB server is running (default is 27017).
mongodump --host 123.45.67.89 --port 27017 --username myuser --password mypassword --authenticationDatabase admin --db mydatabase --out /path/to/backup

This command will connect to the remote server at IP address 123.45.67.89 on port 27017, authenticate with the provided username and password, backup the mydatabase database, and store the backup files in the /path/to/backup/mydatabase directory on your local machine.

mongodump options ☝️

Table of contents ☝️


mongorestore

The mongorestore is a utility to restore backups created by mongodump. To start using mongorestore, you need to open a command prompt or terminal window and run the mongorestore command followed by the desired options and arguments. Here’s the basic syntax:

Running mongorestore command without any specific options or arguments will will search for folder called dump in the current working directory (the terminal directory) to restore data from it.

The command reads the data from the backup file and inserting it back into a MongoDB server. It recreates the database, collections, indexes, and other database structures present in the backup.

Before running this command without any options, make sure you have a folder called dump resulted from executing the mongodump command or other compatible backup tools.

List of mongorestore options

Here are some commonly used options for mongorestore:

  • --version: check version
  • </path/to/backup/>: restore from a specific folder
  • --nsInclude: restore a specific collection
  • --nsInclude: restore a specific database
  • --drop: drop collections before restore
  • --archive: restore archived file

--version
check version

To display the version information for mongorestore installed on your system, you can use the --version option:

mongorestore options ☝️

</path/to/backup/>
restore from a specific folder

To start restoring data from a specific folder, just enter the path to the backup data or folder name in the current directory without any option after the mongostore command.

This command restore databases from a folder named backup located in drive D.

mongorestore options ☝️

--nsInclude
restore a specific collection

We use the --nsInclude option to specify the namespace filter. The namespace represents a combination of a database name and a collection name and follows the format <database_name>.<collection_name_pattern>.

mongorestore backup --nsInclude blog.posts

This command restores only a collection called posts from a database called blog from a folder named backup exist in the current directory. However, it will not restore any collections from the same database.

We can specify multiple --nsInclude options to restore multiple collection from the same database.

mongorestore --nsInclude blog.posts --nsInclude blog.comments

The command will restore posts and comments collections from the blog database and ignore restoring other collections from the same databas.

mongorestore options ☝️

--nsInclude
restore a specific database

In the --nsInclude option, We can use the * symbol after the <database_name> to include all collections exist in the database.

mongorestore --nsInclude blog.*

The command will restore all collections exist in the blog database from the backup. However, it will not restore any collections from other databases.

We can specify multiple --nsInclude options to restore multiple databases with different collections.

mongorestore --nsInclude blog.* --nsInclude test.customers --nsInclude test.orders

This command restores :

  1. blog.*: includes all collections within the blog database.
  2. test.customers: includes only the customers collection within the test database.
  3. test.orders: includes only the orders collection within the test database.

mongorestore options ☝️

--drop
drop collections before restore

The --drop option is used to drop (delete) the target database before restoring the backup.

By default, mongorestore adds or updates data from the backup without removing any existing data. However, when you include the --drop option, it first drops the target database and then performs the restore operation. This ensures a clean slate before restoring the backup.

mongorestore --drop --nsInclude blog.posts

This command will drop the posts collection from the blog database then restore it from the default dump folder that exist in the current directory.

This command will not drop any collections if they are not there in the backup.

mongorestore options ☝️

--archive
restore archived file

To restore a backup created with the --archive option using the mongodump command , you can use the mongorestore command with the --archive option as well:

mongorestore --archive=backup.archive

mongorestore options ☝️

Table of contents ☝️


bsondump commands

The bsondump command is used to convert BSON files to a human-readable format JSON format.

It takes the following options:

  • --bsonFile: specify the input BSON file to be processed.
  • --outFile : specify the output file where the converted data will be saved.
bsondump --bsonFile posts.bson --outFile posts.json

The command takes the input BSON file posts.bson and converts it to a human-readable JSON format, saving the converted data in the output file posts.json.

We can ignore the option --outFile, allowing you to view the contents of the BSON document inside the command prompt or terminal window.

Table of contents ☝️


Related Content:

✔️ User Authentication and Authorization in MongoDB (coming soon)


Links:

🕴️ Linkedin: Dragon Slayer 🐲
📝 Articles: All Articles written by D.S

MongoDB is a popular NoSQL database known for its flexibility, scalability, and ease of use. However, to protect our data from potential data loss or corruption, it’s critical to have a reliable MongoDB backup strategy in place.

In this article, we will go through the process of creating a MongoDB dump using the powerful mongodump tool, ensuring that our MongoDB database backup is secure and easy to restore in case of failure.

How to Create a MongoDB Dump of Database?

Data loss or corruption can occur for various reasons such as hardware failure, human error, or software bugs. Regularly backing up our MongoDB database is essential to minimize these risks. One of the best ways to backup your MongoDB database is by creating a MongoDB dump using the mongodump tool. Below are the steps that help us to create a backup for an entire server are as follow:

  1. Create Direct Backups Using Mongodump
  2. Backup a Remote MongoDB Instance
  3. Backup a Secure MongoDB Instance
  4. Select Databases & Collections
  5. Change the Backup Directory
  6. Create an Archive File
  7. Compress the MongoDB Backup
  8. Restore Database

Step 1: Create Direct Backups Using Mongodump

We can run the mongodump command using the below syntax from the system command line as follows:

mongodump <options> <connection-string>

This structure also permits us to connect to a Mongo database with the –uri command along with a formatted string or flag such as –user, –db, and –password. However, we can’t use more than 1 flag in a single command.

Alternatively, we can use the default configurations and create a Mongo Backup via Mongodump command as follows:

Mongodump

This statement operates on the assumption that your database resides in localhost (127.0.0.1), uses port 27017, and requires no access authentication. This backup process creates a dump folder directly in the current directory as shown in the below image.

mongodump

mongodump

Step 2: Backup a Remote MongoDB Instance

As mentioned in Step 1, you can customize a host and a port number with the –uri connection string using the following syntax:

mongodump --uri="mongodb://<host URL/IP>:<Port>" [additional options]

Moreover, you can set up a server connection with the host option using the following command:

mongodump --host="<host URL/IP>:<Port>"  [additional options]

If you also wish to specify the port number, implement the following statement:

mongodump --host="<host URL/IP>" --port=<Port> [additional options]

The following code demonstrates the process of backing up a remote MongoDB instance:

mongodump --host="10.10.10.59" --port=27017

Output:

Backup-a-Remote-MongoDB-Instance

Output

Step 3: Backup a Secure MongoDB Instance

MongoDB’s Mongodump command allows you to implement access control mechanisms for your data backups. This will require you to provide a Username, Password, and specific Authentication options in the following syntax:

mongodump --authenticationDatabase=<Database> -u=<Username> -p=<Password> [additional options

For instance, we can use a Username & Password to connect to a remote MongoDB instance using the following command:

mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword"

Output:

Backup-a-Secure-MongoDB-Instance

Output

Step 4: Select Databases & Collections

In Step 3 you learned how to back up a remote database using Mongodump. This step involves the –db and –collection options that indicate a database and a collection that requires backing up. You can run the –db option in a standalone manner, but to execute a collection, you have to specify a database. Moreover, you can remove a collection from the backup process, by using the –excludeCollection option.

To select a particular database use the following command:

mongodump  --db=<Backup Target - Database> [additional options]

If you wish to select a whole collection, execute the following:

mongodump  --db=<Backup Target - Database> --collection=<Collection Name> [additional options]

To exclude a particular collection, run the following command:

mongodump  --db=<Backup Target - Database> --excludeCollection=<Collection Name> [additional options]

Step 5: Change the Backup Directory

We can use the –out option to specify the backup folder’s location in the following way:

mongodump --out=<Directory Location> [additional options]

Now, if we wish to switch the backup directory with the “dbbackup” folder, run:

mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --out=dbbackup

Output:

Select-Databases-and-Collections

BackupDirectory

Step 6: Create an Archive File

The Mongodump utility provides us with a method to create an archive file. We can use the –archive option for specifying the file to be archived. In case no specification is given, the output will be in the standard form (stdout).

Keep in mind that you can’t use the –archive option along with the –out option.

mongodump --archive=<file> [additional options]

You can easily define any archive file using the following command:

mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --archive=db.archive

Output:

Create-an-Archive-File

Output

Step 7: Compress the MongoDB Backup

Now, since you know how to backup data using Mongodump, it’s time to understand the process of compressing these files. You can use the –gzip option to compress the JSON and BSON files individually using the following command:

mongodump --gzip [additional options]

For instance, the below statement will compress a complete Mongo database:

mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --gzip

Output:

Compress-the-MongoDB-Backup

Output

Step 8: Restore Database

Apart from excellent backups, MongoDB also provides us the facility to restore our data. The Mongorestore command will seamlessly load data from the Mongodumb backups and restore our Mongo database. The mongorestore command, however, can not overwrite documents present in the database if the id for the document already exists. Otherwise, Mongorestore will generate a new database or will add data to the existing one.

The Mongorestore command requires you to mention the path to your dump directory and the syntax is s follows:

mongorestore dump/

Moreover, in the Mongostore command:

  • We must specify the –uri flag or provide every standard connection flag.
  • We can directly use the most preferred –nsInclude option to restore different collections. It allows users to choose a namespace when restoring the collections for a database.

For instance, in the following command, you can isolate and import the db1 database to restore it in your local environment:

mongorestore --db=redbase --nsInclude="db1.*" dump/

This will restore all the collections of db1 that were earlier dumped. However, it won’t touch the data stored in db2 even both dn1 and db2 have their data stored in the same dump directory.

Benefits of Using Mongodump for MongoDB Backup

The mongodump tool is a powerful and flexible way to create MongoDB backups. Some benefits of using mongodump include:

  • Efficient Data Backups: Whether you want to back up a single collection, query, or an entire MongoDB database, mongodump supports various backup strategies.
  • Oplog Support: By including the oplog file, you can create a point-in-time snapshot of your MongoDB data for easy recovery.
  • Small-Scale Backups: mongodump works exceptionally well for small MongoDB databases, and with the oplog feature, it’s ideal for incremental backups.

Conclusion

In conclusion, backing up your MongoDB database is a crucial part of database management, ensuring data safety and integrity. The mongodump tool makes it simple to create secure, efficient, and scalable MongoDB database backups, whether you’re dealing with local, remote, or secure instances. Regularly backing up your data with MongoDB dumps will protect you from data loss and help ensure smooth recovery when needed.

mongodump is a utility that creates a binary export of
a database’s contents. mongodump can export data from:

  • Standalone deployments

  • Replica sets

  • Sharded clusters

  • Flex clusters

You can use the MongoDB Database Tools to migrate from a self-hosted deployment
to MongoDB Atlas. MongoDB
Atlas is the fully managed service for MongoDB deployments in the
cloud. To learn more, see Seed with mongorestore.

To learn all the ways you can migrate to MongoDB Atlas, see
Migrate or Import Data.

mongodump can connect to mongod and
mongos instances.

You can restore the BSON files generated from mongodump into MongoDB
deployments running the same major version or feature compatibility version as
the source deployment.

Run mongodump from the system command line, not the
mongo shell.

See also:

mongorestore, which allows you to import data that was
exported from mongodump.

If you are archiving stale data to save on storage costs, consider
Online Archive in
MongoDB Atlas. Online
Archive automatically archives infrequently accessed data to
fully-managed S3 buckets for cost-effective data tiering.

mongodump dumps:

  • Collection documents, metadata, and options.

  • Index definitions.

  • Writes that occur during the export, if run with the mongodump
    --oplog option.

mongodump dumps data to a directory or a binary archive file.

Important

You can’t use mongodump with a collection that uses Queryable Encryption.

Example mongodump directory dump structure and files:

dump
├── easternSalesDatabase
│ ├── sales.bson
│ ├── sales.metadata.json
│ └── salesByMonthView.metadata.json
├── westernSalesDatabase
│ ├── sales.bson
│ ├── sales.metadata.json
│ └── salesByMonthView.metadata.json
└── oplog.bson

For a directory dump, mongodump creates:

  • A root directory with the default name dump. You can set the name
    with the mongodump --out option.

  • A subdirectory in the root directory for each database. For example,
    if a database name is easternSalesDatabase, the subdirectory name
    is also easternSalesDatabase.

  • A BSON file with documents for each collection. For example, if a
    collection name is sales, the BSON file is sales.bson.

  • A metadata JSON file for each collection in each database directory.
    For example, a metadata sales.metadata.json file. The file
    contains a document with the exported collection metadata, options,
    and indexes.

  • A metadata JSON file for each view. For example, a metadata
    salesByMonthView.metadata.json file. A view doesn’t have a BSON
    file.

  • An optional oplog oplog.bson file, located in the root
    directory, which contains write operations that occurred during the
    mongodump run. To output an oplog.bson file, use the
    mongodump --oplog option.

If you use the mongodump --gzip option,
the BSON files and JSON metadata files are compressed. The compressed
exported files have bson.gz and metadata.json.gz at the end of
the names.

To dump data to a binary archive file, use the mongodump
--archive option. mongodump creates
a binary file that contains the archived data.

mongodump syntax:

mongodump <options> <connection-string>

To connect to a local MongoDB instance running on port 27017 and
use the default settings to export the content, run mongodump
without any command-line options:

To specify a host and/or port of the MongoDB instance, you can:

  • Specify the hostname and port in the --uri connection string option:

    mongodump --uri="mongodb://mongodb0.example.com:27017" [additional options]

  • Specify the hostname and port in the --host option:

    mongodump --host="mongodb0.example.com:27017" [additional options]

  • Specify the hostname and port in the --host and --port options:

    mongodump --host="mongodb0.example.com" --port=27017 [additional options]

To connect to a replica set to export its data, you can:

  • Specify the replica set name and members in the
    --uri connection string option:

    mongodump --uri="mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myReplicaSetName" [additional options]

  • Specify the replica set name and members in the
    --host option:

    mongodump --host="myReplicaSetName/mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com" [additional options]

By default, mongodump reads from the primary of the
replica set. To override the default, you can specify the read
preference:

  • You can specify the read preference in the
    --uri connection string option:

    mongodump --uri="mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myReplicaSetName&readPreference=secondary" [additional options]

    If specifying the read preference tags, include the
    readPreferenceTags option:

    mongodump --uri="mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myReplicaSetName&readPreference=secondary&readPreferenceTags=region:east" [additional options]

  • You can specify the read preference using the
    --readPreference command-line
    option. The command-line option takes a string if specifying only the read preference mode:

    mongodump --host="myReplicaSetName/mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017" --readPreference=secondary [additional options]

    Or, the command-line option can take a quote-enclosed document
    '{ mode: <mode>, tagSets: [ <tag1>, ... ], maxStalenessSeconds:<num>}'
    to specify the mode, the optional read preference tag
    sets
    , and the optional
    maxStalenessSeconds:

    mongodump --host="myReplicaSetName/mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017" --readPreference='{mode: "secondary", tagSets: [ { "region": "east" } ]}' [additional options]

Note

To avoid data inconsistencies, pause the following actions on your
sharded cluster when you run mongodump:

  • Cross-shard transactions

  • Data definition language operations (operations that create and modify
    collections)

  • Chunk balancing

To stop the balancer, use the sh.stopBalancer() method.

To connect to a sharded cluster to export its data, you can:

  • Specify the hostname of the mongos instance in the
    --uri connection string option:

    mongodump --uri="mongodb://mongos0.example.com:27017" [additional options]

  • Specify the hostname and port of the mongos
    instance in the --host option:

    mongodump --host="mongos0.example.com:27017" [additional options]

By default, mongodump reads from the primary of the
shard replica set. To override the default, you can specify the read
preference:

  • You can specify the read preference in the
    --uri connection string option:

    mongodump --uri="mongodb://mongos0.example.com:27017/?readPreference=secondary" [additional options]

    If specifying the read preference tags, include the
    readPreferenceTags option:

    mongodump --uri="mongodb://mongos0.example.com:27017/?readPreference=secondary&readPreferenceTags=region:east" [additional options]

  • You can specify the read preference using the
    --readPreference command-line
    option. The command-line option takes a string if specifying only the read preference mode:

    mongodump --host="mongos0.example.com:27017" --readPreference=secondary [additional options]

    Or, the command-line option can take a quote-enclosed document
    '{ mode: <mode>, tagSets: [ <tag1>, ... ], maxStalenessSeconds: <num>}'
    to specify the mode, the optional read preference tag
    sets
    , and the optional
    maxStalenessSeconds:

    mongodump --host="mongos0.example.com:27017" --readPreference='{mode: "secondary", tagSets: [ { "region": "east" } ]}' [additional options]

--help

Returns information on the options and use of mongodump.

--verbose, -v

Increases the amount of internal reporting returned on standard output
or in log files. Increase the verbosity with the -v form by
including the option multiple times. For example: -vvvvv.

--quiet

Runs mongodump in a quiet mode that attempts to limit the amount
of output.

This option suppresses:

  • Output from database commands

  • Replication activity

  • Connection accepted and closed events

  • All logs, including error messages, except for those that occur when
    parsing options

--version

Returns the mongodump release number.

--config=<filename>

New in version 100.3.0.

Specifies the full path to a YAML configuration file containing
sensitive values for the following options to mongodump:

  • --password

  • --uri

  • --sslPEMKeyPassword

This is the recommended way to specify a password to mongodump,
aside from specifying it through a password prompt.

The configuration file takes the following form:

password: <password>
uri: mongodb://mongodb0.example.com:27017
sslPEMKeyPassword: <password>

Specifying a password to the password: field and providing a
connection string in the uri: field which contains a conflicting
password will result in an error.

Be sure to secure this file with appropriate filesystem permissions.

Note

If you specify a configuration file with --config and
also use the --password, --uri or
--sslPEMKeyPassword option to mongodump, each
command line option overrides its corresponding option in the
configuration file.

--uri=<connectionString>

Specifies the resolvable URI connection string of the MongoDB deployment, enclosed
in quotes:

--uri="mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]"

Starting with version 100.0 of mongodump, the connection
string may alternatively be provided as a positional parameter,
without using the --uri option:

mongodump mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

As a positional parameter, the connection string may be specified
at any point on the command line, as long as it begins with either
mongodb:// or mongodb+srv://. For example:

mongodump --username joe --password secret1 mongodb://mongodb0.example.com:27017 --ssl

Only one connection string can be provided. Attempting to
include more than one, whether using the --uri option or as
a positional argument, will result in an error.

For information on the components of the connection string, see
the Connection String URI Format documentation.

Note

Some components in the connection string may
alternatively be specified using their own explicit command-line
options, such as --username and --password.
Providing a connection string while also using an explicit option and
specifying conflicting information will result in an error.

Note

If using mongodump on Ubuntu 18.04, you may experience a
cannot unmarshal DNS error message when using
SRV connection strings (in the
form mongodb+srv://) with the --uri option. If so, use
one of the following options instead:

  • the --uri option with a non-SRV connection string (in the form
    mongodb://)

  • the --host option to specify the host to connect to
    directly

Warning

On some systems, a password provided in a connection string
with the --uri option may be visible to system status
programs such as ps that may be invoked by other users. Consider
instead:

  • omitting the password in the connection string to receive an
    interactive password prompt, or

  • using the --config option to specify a configuration file
    containing the password.

--host=<hostname><:port>, -h=<hostname><:port>

Default: localhost:27017

Specifies the resolvable hostname of the MongoDB deployment. By
default, mongodump attempts to connect to a MongoDB
instance running on the localhost on port number 27017.

To connect to a replica set, specify the
replSetName and a seed list of set members, as in
the following:

--host=<replSetName>/<hostname1><:port>,<hostname2><:port>,<...>

When specifying the replica set list format, mongodump always connects to
the primary.

You can also connect to any single member of the replica set by specifying
the host and port of only that member:

--host=<hostname1><:port>

If you use IPv6 and use the <address>:<port> format, you must
enclose the portion of an address and port combination in
brackets. For example: [<address>].

Alternatively, you can also specify the hostname directly in the
URI connection string. Providing a connection
string while also using --host and specifying conflicting
information will result in an error.

--port=<port>

Default: 27017

Specifies the TCP port on which the MongoDB instance listens for
client connections.

Alternatively, you can also specify the port directly in the
URI connection string. Providing a connection
string while also using --port and specifying conflicting
information will result in an error.

--ssl

Enables a connection to a mongod or mongos that has
TLS/SSL support enabled.

Alternatively, you can also configure TLS/SSL support directly in the
URI connection string. Providing a connection
string while also using --ssl and specifying conflicting
information will result in an error.

--sslCAFile=<filename>

Specifies the .pem file that contains the root certificate chain
from the Certificate Authority. Specify the file name of the
.pem file using relative or absolute paths.

Alternatively, you can also specify the .pem file directly in the
URI connection string. Providing a connection
string while also using --sslCAFile and specifying conflicting
information will result in an error.

--sslPEMKeyFile=<filename>

Specifies the .pem file that contains both the TLS/SSL certificate
and key. Specify the file name of the .pem file using relative
or absolute paths.

This option is required when using the --ssl option to connect
to a mongod or mongos that has
CAFile enabled without
allowConnectionsWithoutCertificates.

Alternatively, you can also specify the .pem file directly in the
URI connection string. Providing a connection
string while also using --sslPEMKeyFile and specifying conflicting
information will result in an error.

--sslPEMKeyPassword=<value>

Specifies the password to de-crypt the certificate-key file (i.e.
--sslPEMKeyFile). Use the `--sslPEMKeyPassword option only if the
certificate-key file is encrypted. In all cases, mongodump will
redact the password from all logging and reporting output.

If the private key in the PEM file is encrypted and you do not
specify the --sslPEMKeyPassword option, mongodump will
prompt for a passphrase. See TLS/SSL Certificate Passphrase.

Alternatively, you can also specify the password directly in the
URI connection string. Providing a connection
string while also using --sslPEMKeyPassword and specifying conflicting
information will result in an error.

Warning

On some systems, a password provided directly using the
--sslPEMKeyPassword option may be visible to system status
programs such as ps that may be invoked by other users. Consider
using the --config option to specify a configuration file
containing the password instead.

--sslCRLFile=<filename>

Specifies the .pem file that contains the Certificate
Revocation List. Specify the file name of the .pem file using
relative or absolute paths.

--sslAllowInvalidCertificates

Bypasses the validation checks for server certificates and allows the
use of invalid certificates. When using the
allowInvalidCertificates setting, MongoDB logs as
a warning the use of the invalid certificate.

Warning

Although available, avoid using the
--sslAllowInvalidCertificates option if possible. If the use
of --sslAllowInvalidCertificates is necessary, only use the
option on systems where intrusion is not possible.

Connecting to a mongod or
mongos instance without validating server
certificates is a potential security risk. If you only need to
disable the validation of the hostname in the TLS/SSL
certificates, see --sslAllowInvalidHostnames.

Alternatively, you can also disable certificate validation directly in the
URI connection string. Providing a connection
string while also using --sslAllowInvalidCertificates and specifying conflicting
information will result in an error.

--sslAllowInvalidHostnames

Disables the validation of the hostnames in TLS/SSL certificates.
Allows mongodump to connect to MongoDB instances even if the
hostname in their certificates do not match the specified hostname.

Alternatively, you can also disable hostname validation directly in the
URI connection string. Providing a connection
string while also using --sslAllowInvalidHostnames and specifying conflicting
information will result in an error.

--username=<username>, -u=<username>

Specifies a username with which to authenticate to a MongoDB database
that uses authentication. Use in conjunction with the --password
<mongodump --password>
and --authenticationDatabase <mongodump
--authenticationDatabase>
options.

Alternatively, you can also specify the username directly in the
URI connection string. Providing a connection
string while also using --username and specifying conflicting
information will result in an error.

If connecting to a MongoDB Atlas cluster
using the MONGODB-AWS authentication mechanism, you can specify your AWS access key ID
in:

  • this field,

  • the connection string, or

  • the AWS_ACCESS_KEY_ID environment variable.

See Connect to a MongoDB Atlas Cluster using AWS IAM Credentials for an example of each.

--password=<password>, -p=<password>

Specifies a password with which to authenticate to a MongoDB database
that uses authentication. Use in conjunction with the --username
<mongodump --username>
and --authenticationDatabase <mongodump
--authenticationDatabase>
options.

To prompt the user for the password, pass the --username <mongodump
--username>
option without --password <mongodump --password> or
specify an empty string as the --password <mongodump --password>
value, as in --password "" .

Alternatively, you can also specify the password directly in the
URI connection string. Providing a connection
string while also using --password and specifying conflicting
information will result in an error.

If connecting to a MongoDB Atlas cluster
using the MONGODB-AWS authentication mechanism, you can specify your AWS secret access
key in:

  • this field,

  • the connection string, or

  • the AWS_SECRET_ACCESS_KEY environment variable.

See Connect to a MongoDB Atlas Cluster using AWS IAM Credentials for an example of each.

Warning

On some systems, a password provided directly using the
--password option may be visible to system status programs
such as ps that may be invoked by other users. Consider instead:

  • omitting the --password option to receive an interactive
    password prompt, or

  • using the --config option to specify a configuration file
    containing the password.

--awsSessionToken=<AWS Session Token>

If connecting to a MongoDB Atlas cluster
using the MONGODB-AWS authentication mechanism, and using session tokens in addition to
your AWS access key ID and secret access key, you can specify your AWS
session token in:

  • this field,

  • the AWS_SESSION_TOKEN authMechanismProperties
    parameter to the connection string, or

  • the AWS_SESSION_TOKEN environment variable.

See Connect to a MongoDB Atlas Cluster using AWS IAM Credentials for an example of each.

Only valid when using the MONGODB-AWS
authentication mechanism.

--authenticationDatabase=<dbname>

Specifies the authentication database where the specified
--username <mongodump --username> has been created. See
Authentication Database.

If you do not specify an authentication database, mongodump
assumes that the database specified to export holds the user’s credentials.

If you do not specify an authentication database or a database to
export, mongodump assumes the admin database holds the user’s
credentials.

If using the GSSAPI (Kerberos),
PLAIN (LDAP SASL), or MONGODB-AWS
authentication mechanisms, you
must set --authenticationDatabase to $external.

Alternatively, you can also specify the authentication database directly in the
URI connection string. Providing a connection
string while also using --authenticationDatabase and specifying conflicting
information will result in an error.

--authenticationMechanism=<name>

Default: SCRAM-SHA-1

Specifies the authentication mechanism the mongodump instance uses to
authenticate to the mongod or mongos.

Changed in version 100.1.0: Starting in version 100.1.0, mongodump adds support for
the MONGODB-AWS authentication mechanism when connecting
to a MongoDB Atlas cluster.

Alternatively, you can also specify the authentication mechanism directly in the
URI connection string. Providing a connection
string while also using --authenticationMechanism and specifying conflicting
information will result in an error.

--gssapiServiceName

Specify the name of the service using GSSAPI/Kerberos. Only required if the service does not use the
default name of mongodb.

This option is available only in MongoDB Enterprise.

Alternatively, you can also specify the service name directly in the
URI connection string. Providing a connection
string while also using --gssapiServiceName and specifying conflicting
information will result in an error.

--gssapiHostName

Specify the hostname of a service using GSSAPI/Kerberos
</core/kerberos>
. Only required if the hostname of a machine does
not match the hostname resolved by DNS.

This option is available only in MongoDB Enterprise.

--db=<database>, -d=<database>

Specifies a database to backup. If you do not specify a database,
mongodump copies all databases in this instance into the dump
files.

Alternatively, you can also specify the database directly in the
URI connection string. Providing a connection
string while also using --db and specifying conflicting
information will result in an error.

--collection=<collection>, -c=<collection>

Specifies a collection to backup. If you do not specify a collection,
this option copies all collections in the specified database or instance
to the dump files.

--query=<json>, -q=<json>

Provides a JSON document as a query that optionally limits
the documents included in the output of mongodump. To
use the --query option, you must also specify the
--collection <mongodump --collection> option.

You must enclose the query document in single quotes ('{ ... }') to ensure that it does
not interact with your shell environment.

The query must be in Extended JSON v2 format
(either relaxed or canonical/strict mode)
, including enclosing the field
names and operators in quotes. For example:

mongodump -d=test -c=records -q='{ "a": { "$gte": 3 }, "date": { "$lt": { "$date": "2016-01-01T00:00:00.000Z" } } }'

To use $regex with mongodump, use the following syntax:

mongodump -d=sample_mflix -c=movies -q='{ "year": { "$regex": "20" } }'

Note

When you use the --query option on a time series
collection
, you can only query the
field specified as the metaField.

--queryFile=<path>

Specifies the path to a file containing a JSON document as a query
filter that limits the documents included in the output of
mongodump. --queryFile enables you to create
query filters that are too large to fit in your terminal’s buffer.

Note

When you use the --queryFile option on a time series
collection, you can only query the
field specified as the metaField.

--readPreference=<string|document>

Default: primary

Specifies the read preference for
mongodump. The --readPreference option can take:

  • A string if specifying only the read preference mode:

    --readPreference=secondary

  • A quote-enclosed document to specify the mode, the optional
    read preference tag sets, and the
    optional maxStalenessSeconds:

    --readPreference='{mode: "secondary", tagSets: [ { "region": "east" } ], maxStalenessSeconds: 120}'

    If specifying the maxStalenessSeconds, the value must be greater than or equal to 90.

mongodump defaults to primary
read preference.

If the read preference is also included in the
--uri connection string <--uri>, the command-line
--readPreference overrides the read preference specified in
the URI string.

--gzip

Compresses the output. If mongodump outputs to the dump
directory, the new feature compresses the individual files. The files
have the suffix .gz.

If mongodump outputs to an archive file or the standard
out stream, the new feature compresses the archive file or the data
output to the stream.

--out=<path>, -o=<path>

Specifies the directory where mongodump will write
BSON files for the dumped databases. By default,
mongodump saves output files in a directory named
dump in the current working directory.

To send the database dump to standard output, specify «-» instead of
a path. Write to standard output if you want process the output before
saving it, such as to use gzip to compress the dump. When writing
standard output, mongodump does not write the metadata that
writes in a <dbname>.metadata.json file when writing to files
directly.

You cannot use the --archive option with the
--out option.

--archive=<file>

Writes the output to a specified archive file or, if the archive
file is unspecified, writes to the standard output (stdout). An
archive file is a single-file alternative to multiple BSON files.

  • To output the dump to an archive file, run
    mongodump with the --archive <mongodump
    --archive>
    option and the archive filename.

    mongodump --archive=<file>
  • To output the dump to the standard output stream in order to pipe
    to another process, run mongodump with the
    --archive <mongodump --archive> option but omit the
    filename.

You cannot use the --archive <mongodump --archive> option
with the --out <mongodump --out> option.

Note

If mongodump writes to an archive file,
mongodump performance can improve. For more information on
mongodump performance impacts, see Output Format
Considerations.

--oplog

Creates a file named oplog.bson as part of the mongodump
output. The oplog.bson file, located in the top level of the
output directory, contains oplog entries that occur during the
mongodump operation.

To apply oplog entries from the oplog.bson file in the restore
operation, use mongorestore --oplogReplay. You can use
mongodump --oplog together with mongorestore --oplogReplay to
ensure the data is current and has all the writes that occurred
during the dump.

Without --oplog, if there are write operations during the dump
operation, the dump will not reflect a single moment in time. Changes
made to the database during the update process can affect the output of
the backup.

To back up individual replica sets while still accepting writes, use
--oplog.

Important

--oplog has no effect when running mongodump on a mongos
instance to dump the entire contents of a sharded cluster.

--oplog only works against nodes that maintain an
oplog. This includes all members of a replica set.

--oplog does not dump the oplog collection.

You can’t run mongodump with --oplog on a sharded cluster. To back up
sharded clusters with mongodump, see Back Up a Self-Managed Sharded Cluster with a Database Dump.

Note

To use mongodump with --oplog, you must create a full dump of
a replica set member. mongodump with --oplog fails
if you use any of the following options to limit the data to be dumped:

  • --db

  • --collection

  • --dumpDbUsersAndRoles

  • --query

See also:

--dumpDbUsersAndRoles

Includes user and role definitions in the database’s dump directory
when performing mongodump on a specific database. This
option applies only when you specify a database in the
--db option. MongoDB always includes user and role
definitions when mongodump applies to an entire instance
and not just a specific database.

--excludeCollection=<string>

Excludes the specified collection from the mongodump output.
To exclude multiple collections, specify the --excludeCollection multiple times.

To use the --excludeCollection option, you must specify a database. You can specify
a database with the --db option or in the
--uri connection string.

--excludeCollectionsWithPrefix=<string>

Excludes all collections with a specified prefix from the mongodump
outputs. To specify multiple prefixes, specify the --excludeCollectionsWithPrefix multiple
times.

To use the --excludeCollectionsWithPrefix option, you must specify a database. You can specify
a database with the --db option or in the
--uri connection string.

--numParallelCollections=<int>, -j=<int>

Default: 4

Number of collections mongodump should export
in parallel.

--viewsAsCollections

When specified, mongodump exports views as collections.

Note

Only views are exported. By default, mongodump only
exports a view’s metadata. To export the documents in a view, use
the --viewsAsCollections option.

For each view, mongodump creates a BSON file containing
the documents in the view. If you use mongorestore
with a BSON file created by mongodump, the view is
restored as a collection.

If you do not include --viewsAsCollections,
mongodump captures each view’s metadata. If you include a
view’s metadata file in a mongorestore operation, the view
is recreated.

--compressors=<string>

Specifies the compression algorithm that mongodump uses for network
communication between the mongodump client and the MongoDB server.
You can use one or more of these values for the --compressors
option:

  • snappy

  • zlib

  • zstd

If you specify multiple compression algorithms, mongodump uses the
first one in the list supported by your MongoDB deployment.

For more information on compressors, see the Go driver network
compression documentation.

For more information about mongodump, see:

  • mongodump Compatibility and Installation

  • mongodump Behavior, Access, and Usage

  • mongodump Examples

For a tutorial, see Back Up a Self-Managed Sharded Cluster with a Database Dump.

For more information about TLS/SSL and MongoDB, see
Configure mongod and mongos for TLS/SSL and
TLS/SSL Configuration for Clients.

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Утилита для разбивки жесткого диска windows 10 на русском
  • Windows 7 не прошла проверку на подлинность как это убрать
  • Apache windows test conf
  • Passion and romance windows of the heart смотреть онлайн
  • Изменения внесенные в компьютер отменяются windows 10 бесконечно что делать компьютер