MongoDB provides multiple ways to perform backups and restores.
MongoDB provides command-line tools mongodump and mongorestore for logical database backups and restores.
It is recommended to perform backups during periods of low activity to avoid inconsistencies.
To create a logical backup of the MongoDB database, the following command can be used:
mongodump \
--host <host> \
--port <port> \
--username <user> \
--password <password> \
--authenticationDatabase admin \
--db <database> \
--out /path/for/backup/mongodb_backup
To restore the backup, the following command can be used:
mongorestore \
--host <host> \
--port <port> \
--username <user> \
--password <password> \
--authenticationDatabase admin \
--db <database> \
--drop \
/path/for/backup/mongodb_backup/<database>
Alternatively, a filesystem-level backup of MongoDB data can be performed by archiving the data directory. This approach requires that MongoDB is stopped before copying files to ensure data consistency. If running a replica set, backups can be performed from a secondary node without stopping the primary.
systemctl stop mongod
cd /var/lib/xbat/mongodb
tar -czvf /path/for/backup/mongodb_backup.tar.gz *
systemctl start mongod
To restore from a filesystem backup:
systemctl stop mongod
tar -xzvf mongodb_backup.tar.gz -C /var/lib/xbat/mongodb
chown -R mongodb:mongodb /var/lib/xbat/mongodb
systemctl start mongod
In addition to using CLI tools or filesystem-level backups, MongoDB backups and restores can also be performed via the xbat user interface or the REST API.
Administrators can trigger a full MongoDB backup directly from the UI menu, or by using the following REST API endpoint:
POST /api/v1/benchmarks/backup
The response will return a .tgz file containing the backup. Example response header:
Content-Disposition: attachment; filename="MongoDB_backup_<timestamp>.tgz"
Check the MongoDB documentation and the xbat REST API documentation for further details.
TODO