The Wild

MongoDB Backup Restore Migrate on CentOS

· 4 min read · Mammals

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Verify MongoDB is running: Run sudo systemctl status mongod to confirm the service is active. If not, start it with sudo systemctl start mongod.
  2. Create a backup directory: Use sudo mkdir -p /backup/mongodb to create a dedicated folder for your backup files.
  3. Run mongodump: Execute sudo mongodump --out /backup/mongodb to dump all databases. To back up a specific database, add --db yourdbname (replace with your actual database name).
  4. Verify the backup: Check the output directory with ls -l /backup/mongodb. You should see subdirectories for each database, containing .bson files.
  5. Optional: Compress the backup: Use sudo tar -czf mongodb-backup.tar.gz -C /backup/mongodb . to create a compressed archive, useful for transfer.
  6. Restore on the same server: To restore, run sudo mongorestore --drop /backup/mongodb. The --drop flag removes existing collections before restoring, preventing duplicate key errors.
  7. Migrate to a new server: Copy the backup directory or tar file to the destination server using scp or rsync. For example: scp -r /backup/mongodb user@newserver:/backup/mongodb.
  8. Restore on the destination server: On the new server, ensure MongoDB is running, then run the same mongorestore command as in step 6.
  9. Verify the restored data: Connect with mongosh and run show dbs to list databases, and db.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 mongod user, 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 -h before 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 --oplog during backup to capture point-in-time consistency. For a standalone, this is not needed.

Tips

  • Use --gzip with mongodump: Add --gzip to compress the output files, saving disk space and speeding up transfer.
  • Schedule regular backups: Use cron to run mongodump nightly, 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 --archive can 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 27017 if needed.
  • Permission denied when writing backup: Ensure the backup directory is writable by the user running the command. Use sudo chown -R $(whoami) /backup/mongodb or run with sudo.
  • Restore gives “error applying oplog”: This happens when you use --oplog but the oplog file is missing or corrupted. Re-run the backup without --oplog for 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 --db flag matches the original.
  • Transfer is slow: Compress the backup with tar -czf before copying, or use rsync with 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.