1. 使用 mongodump 和 mongorestore
mongodump 是 MongoDB 自带的一个备份工具,它将数据库数据导出为 BSON 文件,可以在需要时用 mongorestore 恢复。
备份整个数据库:
bash
mongodump --uri="mongodb://username:password@localhost:27017/dbname" --out /path/to/backup/
其中:
--uri:指定 MongoDB 的连接 URI。--out:指定备份文件的输出目录。
备份指定集合:
mongodump --uri="mongodb://username:password@localhost:27017/dbname" --collection collectionname --out /path/to/backup/
恢复备份: 使用 mongorestore 来恢复备份:
mongorestore --uri="mongodb://username:password@localhost:27017/dbname" /path/to/backup/