MongoDB Backup and Recovery

MongoDB Backup and Recovery

Overview

This article provides the standard procedures for creating a compressed backup of the cloudmon database and the steps required to perform a failback (restore) in the event of a system failure or unsuccessful update.

1. Backup Procedure 

Before performing any system upgrades or major configuration changes, you must take a manual backup to ensure data integrity.

Command:

mongodump --uri="mongodb://localhost:27017/cloudmon" --gzip --archive="cloudmon_dump.gz"

Parameters Explained:

  • --uri: Specifies the connection string to the local MongoDB instance and the specific database (cloudmon).

  • --gzip: Compresses the output to save disk space.

  • --archive: Directs the output into a single archive file named cloudmon_dump.gz instead of a folder of BSON files.

2. Failback/Restore Procedure

If the application fails after an update, use the following steps to restore the database to the previous "known-good" state.

Command:

mongorestore --uri="mongodb://localhost:27017" --gzip --archive="cloudmon_dump.gz"

Important Notes for Restore:

  1. File Location: Ensure you are running the command from the same directory where the cloudmon_dump.gz file is located.

  2. Existing Data: If the database already contains data, mongorestore will merge the data. To completely overwrite the existing database with the backup, you may need to add the --drop flag (use with caution).

  3. Verification: After restoration, restart the Cloudmon services to ensure they reconnect to the restored database correctly.

Troubleshooting

  • Permission Denied: Ensure you are running the commands with the necessary user privileges (e.g., use sudo if required).

  • Connection Refused: Verify that the MongoDB service is running using systemctl status mongod.

  • Nginx Errors: If you see Nginx errors after a restore, it may indicate that the database structure has reverted but the application cache or web server config is still pointing to new (broken) assets. Clear the Nginx cache if necessary.