Back up and restore MongoDB
This page describes how to back up and restore the MongoDB database used by Itential Platform. The following strategy minimizes downtime, maximizes efficiency, and helps prevent service level agreement (SLA) breaches.
Backup strategy
Itential recommends these backup practices:
- Perform a daily
mongodumpto capture all database data once per day. - Take hourly snapshots of the jobs and tasks collections to capture automation results throughout the day.
- Retain backups for no longer than seven days, as backup files can become large.
You can capture hourly snapshots in binary format or JSON format. Itential prefers binary format and recommends using it whenever possible.
Identify critical directories and files
Ensure you identify and back up the following files before restoration. The locations below are defaults and may vary in your installation.
Back up MongoDB data
You can only export and import from the primary MongoDB server in the replica set. Connect to the primary server, then use mongoexport or mongodump to export content to the appropriate directory. Move the data to the target server and use mongoimport or mongorestore to recover from these backups.
For complete MongoDB documentation, see the MongoDB backup and restore tools guide.
Back up the entire database
Back up a specific collection
Use mongodump for binary backups or mongoexport for human-readable JSON format.
Binary format
JSON format
Restore the data
When you restore a member of a replica set, MongoDB ensures that the new member doesn’t become the primary until it syncs with the existing replica set. MongoDB controls this automatically through member states, but you can also manually control whether a member can become primary by setting its priority.
Member states
If you add a new member to a replica set, or if an existing member has been out of service for an extended period, MongoDB initiates a series of steps to bring that member in line with the existing members.
STARTUP
- Each replica set member starts in this state.
mongodloads the member’s replica set configuration and transitions the member toSTARTUP2.- Members cannot vote and are not recognized as replica set members yet.
STARTUP2
- Members enter this state after
mongodfinishes loading the configuration. - The member becomes an active replica set member.
- If the member begins initial sync, it remains in
STARTUP2until all data is copied and all indexes are built, then transitions toRECOVERING.
RECOVERING
- Members are not ready to accept reads.
- This state can occur during normal operation and does not indicate an error.
- Members can vote in elections but cannot become primary.
- Members transition to
SECONDARYafter replicating enough data for consistent client reads. The only difference betweenRECOVERINGandSECONDARYis thatRECOVERINGprohibits client reads whileSECONDARYpermits them.
Control member priority
To prevent a secondary member from becoming primary during failover, assign it a priority lower than the primary member’s priority.
Example: Your current primary is m1.example.net and you want to make m3.example.net the primary. You have a three-member replica set with the following configuration:
Verify the election
The following sequence occurs automatically:
m3.example.netandm2.example.netsync withm1.example.net(typically within 10 seconds).m1.example.netdetects it no longer has the highest priority and steps down. Ifm3.example.net’s sync is far behind,m1.example.netwaits untilm3.example.netis within 10 seconds of its optime before stepping down.- The step-down forces an election where
m3.example.netbecomes primary based on its priority setting.
Complete the restoration
Use the appropriate restore command based on your backup method.
Restore from binary format
If you used mongodump to create the backup, use mongorestore to restore it:
Restore from JSON format
If you used mongoexport to create the backup, use mongoimport to restore it:
For more information, see the MongoDB replica set restoration documentation.
Next steps
After completing the backup and recovery process, consider these additional measures:
- Set up automated backup monitoring to ensure backups complete successfully.
- Test your recovery procedures regularly to verify they work as expected.
- Document your specific configuration details for future reference.
- Consider implementing additional security measures for backup storage.