## Inverting Master and Slave 

If you have a master ( A ) and a slave ( B ) and you would like to reverse their roles, follow this procedure. The procedure assumes A is healthy, up-to-date and available. 

If A is not healthy but the hardware is okay (power outage, server crash, etc.), skip steps 1 and 2 and in step 8 replace all of A ‘s files with B ‘s files in step 8. 

If A is not healthy and the hardware is not okay, replace A with a new machine. Also follow the instructions in the previous paragraph. 

To invert the master and slave in a deployment: 

1. Halt writes on A using the fsync command. 

2. Make sure B is up to date with the state of A . 

3. Shut down B . 

4. Back up and move all data files that begin with local on B from the dbPath to remove the existing local.sources data. 

Warning: Removing local. * is irrevocable and cannot be undone. Perform this step with extreme caution. 

5. Start B with the --master option. 

6. Do a write on B , which primes the oplog to provide a new sync start point. 

7. Shut down B . B will now have a new set of data files that start with local . 

8. Shut down A and replace all files in the dbPath of A that start with local with a copy of the files in the dbPath of B that begin with local . 

Considering compressing the local files from B while you copy them, as they may be quite large. 

9. Start B with the --master option. 

10. Start A with all the usual slave options, but include fastsync . 

## Creating a Slave from an Existing Master’s Disk Image 

If you can stop write operations to the master for an indefinite period, you can copy the data files from the master to the new slave and then start the slave with --fastsync . 

Warning: Be careful with --fastsync . If the data on both instances is not identical, a discrepancy will exist forever. 

fastsync is a way to start a slave by starting with an existing master disk image/backup. This option declares that the administrator guarantees the image is correct and completely up-to-date with that of the master. If you have a full and complete copy of data from a master you can use this option to avoid a full synchronization upon starting the slave. 

## Creating a Slave from an Existing Slave’s Disk Image 

You can just copy the other slave’s data file snapshot without any special options. Only take data snapshots when a mongod process is down or locked using db.fsyncLock() . 

