There are multiple ways to take backups of mongodb is different configuraitions, one of the configuration that I have been involved recently is replica-sets. When mongodb is running in replica-set configuration, there is a single primary node and multiple secondary nodes. To take backup of the replica-set we can either do a mongodump of one of the nodes or shutdown one of the secondary nodes and take file copies, since in a replica-set all nodes have the same data (except arbiter). Lets see how we could deal with mongodump method of taking backup. Sync the primary node so that all writes are flused to disk and lock the database for writes, doing a fsync allows for all writes to be persisted to the disk.
now we can issue the mongodump command, there are many more options for mongodump that can be changed, using defaults here
once the mongodump command is done we can unlock the database so that writes can be issued.
1 2 3 4 5 6 7 8 9 10
This dynamic script allows us to find the node that we want to take backup from, either the primary or secondary.