How to Create Azure CosmosDb/MongoDb Replica?
Periodic Backup
Following script can be used to provision the Mongo Database inside Azure CosmosDb with enabled geo replica:
az cosmosdb create --subscription '***' --resource-group $rg --name harmandb --kind MongoDB --capabilities EnableAggregationPipeline --default-consistency-level "Eventual" --backup-policy-type Periodic --backup-redundancy Geo --locations regionName="northeurope" failoverPriority=0 isZoneRedundant=False --locations regionName="centralus" failoverPriority=1 isZoneRedundant=False
The read and write location (primary) is the north Europe location (failover priority=0). The write location is central us (failover priority=1).
If this is not enough, you can easily add another fail-over location in Japan:
az cosmosdb create --subscription '***' --resource-group $rg --name harmandb --kind MongoDB --capabilities EnableAggregationPipeline --default-consistency-level "Eventual" --backup-policy-type Periodic --backup-redundancy Geo --locations regionName="northeurope" failoverPriority=0 isZoneRedundant=False --locations regionName="centralus" failoverPriority=1 isZoneRedundant false --locations regionName="japaneast" failoverPriority=2 isZoneRedundant=False =False
Continious Backup
Previous scripts created the database, which is doing the periodical geo-replicated backup.
--backup-policy-type Periodic --backup-redundancy Geo
However, the CosmosDb offers also the point-in-time restore backup. If you want to change the backup strategy, you can execute the following script, which will change the backup from periodical one to a continuous one (Point-In-Time-Recovery)
To do this the backup-policy-type should be changed to Continuous.
az cosmosdb create --subscription '***' --resource-group $rg --name harmandb --kind MongoDB --capabilities EnableAggregationPipeline --default-consistency-level "Eventual" --backup-policy-type Continuous --backup-redundancy Geo --locations regionName="northeurope" failoverPriority=0 isZoneRedundant=False --locations regionName="centralus" failoverPriority=1 isZoneRedundant false --locations regionName="japaneast" failoverPriority=2 isZoneRedundant=False =False
After the reprovisioning (update of the existing DB) you can see that the blade backup and restore is missing and the new one Point in Time Restore appears.
Upgrading existing backup policy
If you want to change the policy arguments on the existing database, please use the command cosmod update. Following script removes the centralus replica created in the preavious script.
az cosmosdb update --subscription "****" --resource-group $rg --name cpdmdev --backup-policy-type Continuous --locations regionName="northeurope" failoverPriority=0 isZoneRedundant=False --locations regionName="japaneast" failoverPriority=1 --enable-automatic-failover true
Following script will fail, because it adds two new replicas westus and japaneast and removes the existing primary one notheurope.
az cosmosdb update --subscription "****" --resource-group $rg --name cpdmdev --backup-policy-type Continuous --locations regionName="westus" failoverPriority=1 isZoneRedundant=False --locations regionName="japaneast" failoverPriority=2 --enable-automatic-failover true
The script will fail, becaus ethe primary node cannto be removed.
(BadRequest) Failover priorities must be unique