Restoring a Cosmos DB database to a point in time after accidental deletion isn’t about recovering deleted data; it’s about creating a new database from a snapshot of the old one.
Let’s say you accidentally dropped your entire orders database in Cosmos DB. You panic, but you remember you had "Point-in-Time Restore" enabled. What happens next is that Cosmos DB takes a snapshot of your database’s state from a specific moment before the deletion. You then use this snapshot to provision a brand new database that contains all the data as it existed at that chosen past moment.
Here’s how you’d actually do it in the Azure portal:
-
Navigate to your Cosmos DB account: Go to the Azure portal and find the Cosmos DB account that housed your deleted database.
-
Find the "Backup" blade: On the left-hand menu for your Cosmos DB account, under "Settings," you’ll see an option called "Backup." Click on it.
-
Select "Restore": Within the Backup blade, you’ll see a "Restore" button. Click that.
-
Choose restore options:
- Restore to a point in time: This is your option.
- Source database: Select the name of the database that was deleted (e.g.,
orders). - Restore timestamp: This is crucial. You need to pick a timestamp before the accidental deletion. If you deleted it at 3:00 PM UTC, you might pick 2:59 PM UTC. The UI will show you available restore points.
- Target database name: You’ll need to give this newly restored database a name. It cannot be the same name as the original if the original still exists in some form (though in a full deletion scenario, it won’t). Let’s call it
orders-restored-20231027T145900Z. - Capacity mode: Choose between "Provisioned throughput" or "Serverless." For a restored database, you’ll likely want to provision throughput that matches your original, or at least enough to handle the initial data load and then scale down.
- Throughput: Specify the RU/s (Request Units per second) for your restored database.
-
Initiate the restore: Click "Review + create" and then "Create."
The restore operation will begin. Cosmos DB will provision a new database and populate it with data from the selected backup. This process can take some time, depending on the size of your database. You’ll be able to monitor its progress in the Azure portal. Once it’s complete, you’ll have a new database, orders-restored-20231027T145900Z, containing your data as it was at the chosen point in time. You can then rename this database back to orders if needed, after ensuring the original (now deleted) is truly gone and the new one is healthy.
The underlying mechanism relies on Cosmos DB’s continuous backup feature. When enabled, Cosmos DB periodically takes backups of your data. Point-in-time restore leverages these backups to reconstruct the database state at any point within the retention period. The key is that this isn’t an "undo" operation on the existing database. It’s a brand new database creation from a historical state.
A common pitfall is not enabling continuous backup before the accidental deletion. If continuous backup wasn’t active, point-in-time restore is impossible, and you’re looking at other, more complex recovery strategies (like if you had your own application-level backups). Another mistake is picking a restore timestamp after the deletion occurred – you’ll just get an empty database.
The next thing you’ll likely encounter is configuring the restored database’s indexing policy and potentially its throughput, as these might not be perfectly replicated from the original state and may need tuning for your current application needs.