MongoDB Backup Restore Migrate on CentOS
In this article
This guide walks you through backing up, restoring, and migrating a MongoDB database on CentOS. You’ll use the built-in mongodump and mongorestore tools, which work for both standalone and replica set deployments. Before starting, ensure MongoDB is installed and running, and that you have sufficient disk space for the backup files.
What You'll Need
- A CentOS server with MongoDB installed (version 4.x or later recommended).
- Access to a terminal with sudo or root privileges.
- Enough free disk space to hold the backup (typically at least the size of your database).
- If migrating to a different server, network access between the source and destination machines.
Step-by-Step
- Verify MongoDB is running: Run
sudo systemctl status mongodto confirm the service is active. If not, start it withsudo systemctl start mongod. - Create a backup directory: Use
sudo mkdir -p /backup/mongodbto create a dedicated folder for your backup files. - Run mongodump: Execute
sudo mongodump --out /backup/mongodbto dump all databases. To back up a specific database, add--db yourdbname(replace with your actual database name). - Verify the backup: Check the output directory with
ls -l /backup/mongodb. You should see subdirectories for each database, containing.bsonfiles. - Optional: Compress the backup: Use
sudo tar -czf mongodb-backup.tar.gz -C /backup/mongodb .to create a compressed archive, useful for transfer. - Restore on the same server: To restore, run
sudo mongorestore --drop /backup/mongodb. The--dropflag removes existing collections before restoring, preventing duplicate key errors. - Migrate to a new server: Copy the backup directory or tar file to the destination server using
scporrsync. For example:scp -r /backup/mongodb user@newserver:/backup/mongodb. - Restore on the destination server: On the new server, ensure MongoDB is running, then run the same
mongorestorecommand as in step 6. - Verify the restored data: Connect with
mongoshand runshow dbsto list databases, anddb.collection.countDocuments()to check document counts.
Common Mistakes
- Forgetting the --drop flag: If you restore into an existing database without
--drop, you may get duplicate key errors for collections that already exist. - Not using sudo: MongoDB data files are often owned by the
mongoduser, so running commands without sudo may cause permission errors. - Insufficient disk space: Backing up a large database to a full disk will fail. Always check available space with
df -hbefore starting. - Restoring to a different MongoDB version: Backups from older versions may not be fully compatible with newer versions. Always test after restore.
- Ignoring replica set considerations: If your MongoDB is a replica set, you must use
--oplogduring backup to capture point-in-time consistency. For a standalone, this is not needed.
Tips
- Use --gzip with mongodump: Add
--gzipto compress the output files, saving disk space and speeding up transfer. - Schedule regular backups: Use cron to run
mongodumpnightly, and keep a rotation of old backups. - Test restores regularly: Periodically restore a backup to a test instance to ensure your backups are valid.
- Use --archive for streaming: For large databases,
mongodump --archivecan stream directly to a file or even another server via pipes. - Document your backup strategy: Write down the commands and schedules so you or your team can follow them consistently.
Troubleshooting
- mongodump fails with “Failed: error connecting to db server”: Check that MongoDB is running and that you’re using the correct host and port. Use
--host 127.0.0.1 --port 27017if needed. - Permission denied when writing backup: Ensure the backup directory is writable by the user running the command. Use
sudo chown -R $(whoami) /backup/mongodbor run with sudo. - Restore gives “error applying oplog”: This happens when you use
--oplogbut the oplog file is missing or corrupted. Re-run the backup without--oplogfor a full snapshot. - Database appears empty after restore: Check the restore logs for errors. Ensure you restored to the correct database name and that the
--dbflag matches the original. - Transfer is slow: Compress the backup with
tar -czfbefore copying, or usersyncwith compression (rsync -avz).
Frequently Asked Questions
Can I back up a single collection?
Yes, use mongodump --db yourdb --collection yourcollection --out /backup to back up just that collection.
Does mongodump lock the database?
No, mongodump does not lock the database. It reads data while the server is running, which may lead to inconsistent snapshots if writes occur during the backup. For consistent backups, use --oplog on a replica set.
Can I restore a backup to a different database name?
Yes, use mongorestore --db newdbname /backup/mongodb/olddbname to restore into a new database name.
How long does a backup take?
It depends on the database size and server performance. A 10 GB database might take a few minutes, while a 1 TB database could take hours. Monitor progress with the verbose output.
Is it safe to run mongodump on a production server?
Yes, it’s safe because it doesn’t lock writes. However, for critical systems, consider running it during low-traffic periods or use a secondary replica set member to avoid any performance impact.
What if I need to migrate to a MongoDB Atlas cluster?
You can use the same mongodump backup and then use mongorestore with the Atlas connection string. Ensure your Atlas cluster has the correct IP whitelist and credentials.