Performing Backups in Azure Cosmos DB
Tags: cosmosdb, database, azure, tutorial
Published: 2020-03-27 23:06:42

---
title: Performing Backups in Azure Cosmos DB
published: true
date: 2020-03-27 21:02:58 UTC
tags: cosmosdb,database,azure,tutorial
canonical_url: 
---

![](https://cdn-images-1.medium.com/max/560/0*DCCKpu4-gAkUo2gb.png)

When it comes to backups in [Azure Cosmos DB](https://docs.microsoft.com/en-us/azure/cosmos-db/introduction), we have two options. We can rely on the Automatic Backups that come with Azure Cosmos DB or we can manage our own backups. This article will go over both of these processes so you can determine which route is best for your application’s needs.

**Automatic Backups**

Azure Cosmos DB automatically makes backups of your data every 4 hours without it affecting performance or availability. These backups are stored in Azure Blob Storage in the same region that your Cosmos DB write region is (If you have multi-master configured for your Cosmos DB account, the backups are stored in one of the write locations).

These backups are taken every 4 hours and the latest two backups are stored. If you delete a container or database within your account, the existing snapshots for those containers or databases are stored for 30 days.

Thankfully, these snapshots are taken without consuming any provision throughput on your account, so you don’t need to provision any extra throughput on your Cosmos DB databases or containers in order for backups to happen.

**How to restore data from an automatic backup**

Azure Cosmos DB can restore data should any of the following situations occur:

- The entire account is deleted
- One or more databases are deleted.
- One or more containers are deleted.
- Items within a container are deleted or modified.
- A shared throughput offer container within a shared offer database is deleted or corrupted.

According to the documentation, a new Cosmos DB account will be created in order to hold the restored data. If you’re in the portal at the time, you’ll see a Cosmos DB account with the following name:

```
<Azure\_Cosmos\_Account\_Name>-restored1
```

the last digit will show the amount of restore attempts that have been made. Unfortunately, you can restore data to a pre-created Cosmos DB account

The Cosmos DB team can restore data into an account with the same name. **Do not recreate the account yourself and then ask for a restore!** The team won’t be able to restore the data to the newly created account.

When a database is deleted, whole databases or some collections within that database can be restored. However, if you provision throughput at the Database level, you can’t restore the individual containers.

The tricky part comes when restoring items within a container. In this scenario, we need to specify the time to restore our data to. With backups only having a retention period of 8 hours in Cosmos DB, the more you delay the more likely your backups will be overwritten.

**Managing your own Backups**

For your workload requirements, you may want more control over how you backup your Azure Cosmos DB data. If you’re using the SQL API for your Cosmos DB account, you can use either [Azure Data Factory](https://azure.microsoft.com/en-us/services/data-factory/) to move data to another Cosmos DB account or another alternative storage mechanism, or you can use the [Azure Cosmos DB Change Feed](https://docs.microsoft.com/en-us/azure/cosmos-db/change-feed) to listen to a container and then move any changes in that container to another type of storage.

Let’s go through two basic examples for both the Data Factory and Change Feed route and see how we can set this up.

**Performing Backups with Azure Data Factory**

Azure Data Factory is a managed cloud service that you can use to build awesome and complex ETL, ELT and data integration pipelines with.

We can backup our data periodically using a Copy and Transform data job that essentially copies the data within our Azure Cosmos DB container and then stores those documents in another location.

I’ve created a Data Factory account and I’ll use that to take my documents from Azure Cosmos DB and store it into Blob Storage. To create new pipelines, we go to Data Factory and click on ‘Author & Monitor”

![](https://cdn-images-1.medium.com/max/1024/1*BtczahOYaf5eaKdqUqi5Sg.png)

We’ll be redirected to the Azure Data Factory UI where we can set up our pipelines and data flows. For this tutorial, I’m just going to perform a simple Copy Data activity to copy the data in my Cosmos DB collection into a Blob container in Blob Storage. Click on ‘Copy Data’ to get started.

![](https://cdn-images-1.medium.com/max/1024/1*fI7NE5uLf_8SWE90ZNFw4A.png)

To create a new Copy Data activity, we need to set up some configuration for our pipeline. We first have to give it some properties. Below, I’ve given my activity a name and configured it to run on a schedule. If we are doing a 1 time backup, we can just run this job once.

![](https://cdn-images-1.medium.com/max/1024/1*KW07NBcrrGUyQkhybQ7zKA.png)

Now that we’ve given our activity a name, we need to define a data source and a data destination. Essentially where we are getting the data from and where we are sending it to. Let’s set up our data source which will be our Cosmos DB container. Click on ‘Create New Connection’ and choose Azure Cosmos DB SQL API as our linked service.

![](https://cdn-images-1.medium.com/max/1024/1*NcS8_-WC4kBUSh6eJjw0kg.png)

Once you’ve configured your account, you need to choose which collection that you want to get your data from. I’ve only got one container so I’ll pick that. We can also choose to export our data as JSON files so I’ll choose that option as well.

![](https://cdn-images-1.medium.com/max/1024/1*dEY2QulTSKkxXbtTN77w5A.png)

Now that I’ve got my source sorted, I need to configure my destination. I’m going to create a new linked service and choose ‘Azure Blob Storage’

![](https://cdn-images-1.medium.com/max/1024/1*a3wU8K-bwJAoAr3M2U4jvQ.png)

Once I’ve configured my Storage account, I need to choose where I’m going to send my Cosmos DB data as JSON files to. I’ve got two containers in my Storage account (One for the Data Factory demo, another for my Change Feed demo). I’m going to choose ‘datafactoryfiles’ as my destination.

![](https://cdn-images-1.medium.com/max/1024/1*h__4HpcZgFETkFmPRGXlvQ.png)

Once everything has been set up, Azure Data Factory will validate the pipeline and provided everything is good, it’ll start running! We can monitor the pipeline to see if it’s still running and restart it if necessary by clicking on ‘Monitor’

![](https://cdn-images-1.medium.com/max/1024/1*mDVtJIb2OWzry7t8fVE3oA.png)

Once the pipeline has finished running, we will see our JSON file containing our backups in our Storage account.

![](https://cdn-images-1.medium.com/max/1024/1*cEe1mbDpskNw1KRy698XIg.png)

**Performing Backups with the Change Feed**

The Change Feed is a feature in Cosmos DB that listens to any container that you configure the Change Feed for any changes. The changes are then outputted in the order that they were modified.

I’ve written up a couple of articles about the Change Feed, which you can read here:

- [Working with the Azure Cosmos DB Change Feed Processor in C#](https://dev.to/treyhuffine/working-with-the-azure-cosmos-db-change-feed-processor-in-c-1o24-temp-slug-1072757)
- [Implementing Cosmos DB Change Feed using Azure Functions in C#](https://towardsdatascience.com/implementing-cosmos-db-change-feed-using-azure-functions-in-c-d76a7f3c4ac)

For the purposes of this article, I’m going to write a basic Azure Function that listens to a container and then persists the document to another container in our Cosmos database. Azure Functions are by far the easiest way to get started with the Change Feed in Cosmos DB,

Here’s the code:

{% gist https://gist.github.com/willvelida/973b577551bd77615684b1f9622982e5 %}

In this Function, I’m connecting to my storage account that I want to persist my backups to. I’m then iterating through each of the changes on this container captured by the Change Feed and then writing those changes to a JSON file. I’m then uploading that file to blob storage.

If you want to see the whole sample, please checkout out this [GitHub Repo](https://github.com/willvelida/telementry-demo).

We should now see our backup file in our Storage account:

![](https://cdn-images-1.medium.com/max/1024/1*EOxYQP6D8uDjI_Rm96rOwA.png)

In this article, I discussed two routes that you can use to handle backups for your Cosmos DB accounts. My recommendation when it comes to backups is to take matters into your hands as much as possible. I’d recommend exploring the Data Factory route or Change Feed route, particularly if your backup requirements are unique and require special handling.

If you do need the Cosmos DB team to backup anything for you, I’d recommend that you do it as soon as possible. Automatic backups are limited in time frame, so the sooner you get onto it the better.

Hopefully you’ve enjoyed this article. As always, if you have any questions, please feel free to ask in the comments below.