Script to backup - export Mongo Atlas Database free-tier
This script to help you export the Mongo database collections to json file, I also using it to export database store in Mongo DB Atlas (aka Mongo DB Cloud) in-case I using a free-tier cluster (M0) or want to pull database to local.
Backup / export
This backup script to run on Windows OS, you'll need donwload database tools, extract and add bin
folder to OS PATH to make command mongoexport
available in CMD.
update
MONGO_URI
with correct connection string,name_of_collection*
with real database collection list you want to export/backup, andyour_database_name
with an accordant name.
@echo OFF
echo === EXPORT MONGO DB ATLAS COLLECTIONS ===
SET "MONGO_URI=mongodb+srv://<**redacted**>@clusterxxx.mongodb.net/your_database_name"
SET "TODAY=%date:~10,4%-%date:~4,2%-%date:~7,2%"
SET "BACKUP_TO_DIR=%CD%/%TODAY%"
if not exist %BACKUP_TO_DIR% mkdir %TODAY%
for %%x in (
name_of_collection_1
name_of_collection_2
name_of_collection_3
) do (
mongoexport --uri="%MONGO_URI%" --collection="%%x" --out="%BACKUP_TO_DIR%/your_database_name_%%x_export_%TODAY%.json"
)
echo === export done ===
pause
Restore
using
mongoimport
tool
Example we restore collection name events
from file events.json
:
mongoimport --uri="mongodb://mongos0.example.com:27017/reporting" --collection=events --file=events.json