How To Backup Azure DevOps Git Repositories

6 Min. Read

In this article, I will show you how to automate the backup of Azure DevOps Git repositories and then verify the restore operation.


DevOps has been an emerging trend in the software development world for the past several years. While the term may be relatively new, it is really a convergence of a number of practices that have been evolving for decades. DevOps is a revolutionary way to release software quickly and efficiently while maintaining a high level of security.

Azure DevOps — formerly known as Visual Studio Team Services (VSTS)— is a Software as a service (SaaS) platform from Microsoft that provides an end-to-end DevOps toolchain for developing and deploying software. It also integrates with most leading tools on the market and is a great option for orchestrating a DevOps toolchain. It also integrated perfectly with both Visual Studio and Visual Studio Code. While the code is perfectly safe on Azure infrastructure as committed by Microsoft here (Azure DevOps data protection overview), including periodic backups and Geo-replication, but we do not have any control over it. And it does not prevent unintentional or malicious actions leading to data loss.

There are also cases where a centralized local backup of all projects and repositories is needed. These might include corporate policies, business continuity, and disaster recovery plans (BCDR).

Unfortunately, there is no out-of-the-box backup solution as confirmed by Microsoft here. However, what we could do is to download the repository manually from Azure DevOps as a Zip file as shown in the figure below.

How To Backup Azure DevOps Git Repositories 2

But this may not be practical if we have a considerable amount of projects and repositories and need them backed up on a regular basis.

Let’s explore other methods and see how to automate the backup of the repositories.


To follow this article, you need to have the following:

1) Azure subscription – If you don’t have an Azure subscription, you can create a free one here.

2) Azure storage account – To create a general-purpose storage account, follow the instructions described here.

3) Azure DevOps – If you don’t have one, you can create a free one here.

  • You have an existing Azure Repos.
  • You are familiar with Azure Pipelines and YAML syntax.

Automate Azure DevOps backup repository

To automate the backup repository, you have a couple of different options as follows:

1) Programmatically back up source code from Azure DevOps using Azure DevOps REST API. Then you can use Azure Functions with a time trigger to run this on schedule.

2) Create a git bash script to mirror and backup the Git repository.

3) Clone the latest repository on your machine and then automate the backup using your favorite on-premises backup application such as SCDPM, Veeam, etc. OR,

4) You can leverage the Azure DevOps service to automate the backup and copy the repository to Azure Blob storage or to an Azure VM.

For the remainder of this article, I will use option 4 to automate the backup of the repository using the YAML Pipeline and command-line tasks. At a high level, the process looks like this:

1) Create an Azure DevOps Personal Access Token (PAT).

2) Create a YAML Pipeline including the following tasks:

  • Clone the Git repository.
  • Archive the Git repository.
  • Copy the archive file to Azure Blob storage.

Create an Azure DevOps Personal Access Token

Sign in to the Azure DevOps portal and take the following steps:

On the top right corner of the portal, you have user settings next to your picture account. Clicking on it reveals the account menu where you can find the ‘Personal access tokens‘ option as shown in the figure below.

How To Backup Azure DevOps Git Repositories 3

The ‘Personal access tokens‘ page will open. Click on ‘New Token‘ to create one. For this example, we only need to check the ‘Read‘ for Code as shown in the figure below. Next, give a ‘Name‘ to the token, then set the ‘Expiration‘ date and click ‘Create‘.

How To Backup Azure DevOps Git Repositories 4

Once the token is created you will get the value, but since it won’t be shown again, you must copy and save it elsewhere. We will need this token in the next section to clone the repository.

Create a YAML Pipeline

Azure Pipelines supports continuous integration (CI) and continuous delivery (CD) to constantly and consistently test and build your code and ship it to any target. You accomplish this by defining a pipeline. And if there’s any update with your repository, the pipeline will be triggered to run and copy the entire repository.

To create a YAML pipeline, take the following steps:

1) Navigate to your team project on Azure DevOps in a new browser tab.

2) Navigate to the Pipelines hub on the left-hand side.

3) Click ‘New pipeline‘. We will use the wizard to automatically create the YAML definition based on our project.

How To Backup Azure DevOps Git Repositories 5

4) Next, select the ‘Azure Repos Git‘ as the source hosting platform.

How To Backup Azure DevOps Git Repositories 6

5) Then select your repository. In my example, the repo is called ‘infra-code-cn‘.

How To Backup Azure DevOps Git Repositories 7

6) Next, select the ‘Starter pipeline‘ template as the starting point for the backup pipeline.

How To Backup Azure DevOps Git Repositories 8

7) The starter pipeline will open including the following default YAML definition.

How To Backup Azure DevOps Git Repositories 9

8) Next, we need to update the YAML definition. The full YAML definition syntax to monitor and backup all branches looks like this (the PAT, Azure Subscription, and Storage account have been intentionally changed in this example). Please refer to the logic details below the figure.

How To Backup Azure DevOps Git Repositories 10

  • Trigger (Continuous Integration): The wildcard (*) can monitor all your branches. So if there’s any update with your repository, the pipeline will be triggered to run.
  • Pool (windows-latest): This task is written in PowerShell and thus works only when running on Windows agents.
  • The CmdLine Task: This command calls ‘git clone –mirror‘ to make a copy of your git repository. Here you need to use the personal access token that we created in the previous steps. The full syntax of the command line task looks like this:
'git clone --mirror https://{yourPAT}{yourOrganizationName}/{yourProjectName}/_git/{yourRepoName}'
  • The Archive Files Task: This will take the git repository which was cloned from the previous step and then zipped to and overwrite the existing archive file on the VM image used in the Pool. The archive files task includes a range of standard archive formats such as .zip, .jar, .war, .ear, .tar, .7z, and more.
  • The File Copy Task: The copy task will take the archive file and send it over to the Azure Blob storage. In this task, you need to specify the target Azure subscription, storage account, and container name.

9) Finally, click Save and run and then commit.

How To Backup Azure DevOps Git Repositories 11

10) Once you commit, you need to give the pipeline the needed permission to access the resource before it can run for the first time. Click ‘View‘.

How To Backup Azure DevOps Git Repositories 12

11) On the Waiting for review page, click ‘Permit‘, and then click ‘Permit‘ on the confirmation access window.

How To Backup Azure DevOps Git Repositories 13

12) Once you permit, the pipeline will kick in and run the job. The job should be completed in about 2 minutes.

How To Backup Azure DevOps Git Repositories 14

13) You will also receive an email if the Build failed or succeeded similar to the below.

How To Backup Azure DevOps Git Repositories 15

Verify the Backup

Now to verify the backup, sign in to the Azure Portal and browse to the storage account. Go to Containers under Blob service.

You will see a new container named ‘azure-devops-container‘ as we set in the YAML definition.

How To Backup Azure DevOps Git Repositories 16

Click on the container name and then open the folder which is the Blob Prefix (az-devops-backup), you will see archive file as shown in the figure below.

How To Backup Azure DevOps Git Repositories 17

Restore from Backup

To restore from backup, you need first to download the archive file from the container and extract it locally on your machine.

Once you extract the file, you will see the repository name with the .git extension. Open command prompt window assuming you already have Git installed on your machine, browse to the .git folder and then run the following command to push and restore your repository. Please note that you can restore to the same project/repository or you can restore to a new project and new repository.

How To Backup Azure DevOps Git Repositories 18

The --mirror option is used with both the clone for backup and push command. This option ensures that all branches and other attributes are replicated in the new repo.

The good news is, once you restore the repository, it also brings along all the revision information (full-fidelity history) as shown in the figure below.

How To Backup Azure DevOps Git Repositories 19

That’s it there you have it!


The logic of the YAML Pipeline is, if there’s any change to your source repository, the pipeline will be triggered and it takes the latest copy in System.DefaultWorkingDirectory (CmdLine task) and archive this copy into a file, then the Azure File copy task will copy the .zip file to Azure blob storage. You can also copy it to Azure VM which allows you to create a daily/weekly backup for your VM, please check the Azure File Copy task for more information.

With the help of simple tasks, we can automate and produce a full copy of the repository that could be easily restored or imported into the new project.

Thank you for reading my blog.

If you have any questions or feedback, please leave a comment.

-Charbel Nemnom-

Related Posts


What’s New in System Center 2019 Data Protection Manager Update Rollup 2

How To Lower Your Azure File Shares Costs With Hot and Cool Tiers


8 thoughts on “How To Backup Azure DevOps Git Repositories”

Leave a comment...

  1. Hi,
    I tried the solution you mentioned but the files in project source code and is not getting backup as expected.
    The zip folder is getting created in container but when I try to extract the folder I cannot see the source code.

    Could you please advise on same?

  2. Thanks for your comment! Could you please confirm that you have followed the exact steps as described in Create YAML Pipeline section?
    Please repeat those steps and confirm.

  3. Hi,
    Thanks for your response.

    I tried the same steps what is mentioned in Create YAML pipeline and it didn’t work for me.

    Let me know if you need any further information.


  4. Hello Archana, thanks for confirming!
    Could you please update the task: ArchiveFiles@2 rootFolderOrFile line only From: rootFolderOrFile: ‘$(System.DefaultWorkingDirectory)/yourRepoName’ To rootFolderOrFile: ‘$(System.DefaultWorkingDirectory)’
    Remove ‘Your Repository Name’ from the end of the rootFolderOrFile. Please try again, it should work now. Hope that helps!
    Thanks for reporting!

  5. Thanks Charbel. It worked for me. Thanks a ton.

  6. Thank you, Archana for confirming! I am glad to hear it works for you.

  7. Hi Charbel,

    Can you please let us know whether we can backup source code using private agents instead of hosted agents?

    Thanks & Regards

  8. Hello Swagath, thanks for the comment!
    Yes, you could back up the source code using private agents instead of hosted agents.
    Hope this helps!

Let me know what you think, or ask a question...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Stay in Touch

Never miss out on your favorite posts and our latest announcements!

The content of this website is copyrighted from being plagiarized!

You can copy from the 'Code Blocks' in 'Black' by selecting the Code.

Please send your feedback to the author using this form for any 'Code' you like.

Thank you for visiting!