Skip to content

This project provides a small console tool that enables you to backup your Azure DevOps Repos (Git based) using the API for Azure DevOps. The tool is built using C# and backups your repos to your local server/machine for an "offline" backup, and it can send you an e-mail report for backup status, deleted old backup and so on.

License

michaelmsonne/AzureDevOpsBackupTool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 

Repository files navigation

Azure DevOps Backup


Table of Contents

Introduction

Azure DevOps is a cloud service to manage source code and collaborate between development teams. It integrates perfectly with both Visual Studio and Visual Studio Code and other IDEĀ“s and tools there is using the "Git". While your code is perfectly safe on the Azure infrastructure hosted by Microsoft, there are cases where a centralized local backup of all projects and repositories is needed. These might include Corporate Policies, Disaster Recovery and Business Continuity Plans. We can download a repository from Azure DevOps as a Zip file, 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. To do this, we can use the Azure DevOps API, as explained here: https://learn.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-7.1 šŸ’Ŗ

Contents

Outline the file contents of the repository. It helps users navigate the codebase, build configuration and any related assets.

File/folder Description
AzureDevOpsBackup Source code.
docs Documents/pictures.
.gitignore Define what to ignore at commit time.
CHANGELOG.md List of changes to the sample.
CONTRIBUTING.md Guidelines for contributing to the TEMPLATE.
README.md This README file.
SECURITY.md Security file.
LICENSE The license for the AzureDevOpsBackupTool.

Features

Overall:

  • Asynchronous Resolution: Utilizes asynchronous processing for improved performance and responsiveness, allowing users to continue working while backups are being created.
  • Simplicity and Ease of Use: Provides a straightforward and user-friendly method for creating backups from Azure DevOps repositories.

List:

  • Backup Functionality:

    • Repository Backup: Enables users to create local backups of Azure DevOps repositories.
    • Customizable Backup Options: Offers various command-line options to tailor the backup process, including specifying backup directories, token authentication, cleanup, backup retention period, and more.
    • Zip File Handling: Ability to download repository items as ZIP files and organize them locally, optionally extracting them into a directory structure using the --unzip argument.
    • Automated Cleanup: Supports cleanup functionality to manage the disk space occupied by the backup files.
    • Backup Retention: Allows users to specify the number of days to retain backups, automatically removing older backups based on the set retention period using the --daystokeepbackup argument.
  • Email Reporting:

    • Email Notifications: Sends detailed email reports upon backup completion, providing status updates, backup job information, and relevant details.
    • Email Customization: Offers different email layouts (--simpelreportlayout) to cater to varying preferences or requirements.
    • Priority Configuration: Provides the option to set email priority (--priority) for the backup completion notification.
  • Logging:

    • Job Logging: Stores logs for backup jobs in a designated folder (.\Log) for a defined period (default: 30 days) beside the AzureDevOpsBackup.exe executable.

Download

Download the latest version

Version History

Getting Started

  1. Downloaded the installer from releases to any location on a Windows machine where you want to use the tool
  2. Run the downloaded installer and select where you will install the tool
  3. When installed you need an access token to connect to Azure DevOps (read the guide under here)
  4. Then run the main AzureDevOpsBackup.exe with the appropriate command line options to connect to your Azure DevOps and download the backup use with command line options in CMD, PowerShell or Windows Terminal!
  5. You now have an local backup!

How to get a local backup of your repos on your backup/home server? šŸ¤”

1. Get an Azure DevOps API personal access token

On the top right corner of the Azure DevOps portal we have our account picture. When clicking on it reveals the account menu where we find a Security option. Inside Security, we have Personal access tokens. We click on New token to create one. For this example we only need to check the Read, write & manage permission for Code. When we name the token and click Create, we get the token value, but since it wonā€™t be shown again, we must copy and save it elsewhere. We will need this token later, to access the API!

2. Clone the repository, configure arguments and dependencies

To get the sample code, we can clone this GitHub repository or another and open it with etc. Visual Studio. To test the solution in debug mode, we still have to configure a few arguments in the project properties. These arguments will define the access tokenā€Šā€”ā€Šobtained on the previous stepā€Šā€”ā€Š, organizationā€Šā€”ā€Ši.e. the Azure DevOps domainā€Šā€”ā€Šand the local directory where we want to write the backed up data. There is a fourth optional argument to state if we want to unzip the downloaded filesā€Šā€”ā€Šmore on that later.

N.B: Keep your generated access token in a safe place!

Hereā€™s how the argument list will look like:

.\AzureDevOpsBackup.exe --token "our-auth-token" --org "our-org" --outdir "C:\backup\out-directory" --server "smtp.server.com" --port "25" --from "azure-devops-backup@domain.com" --to "backupmail@domain.com" --unzip --cleanup --daystokeepbackup 50 --priority high

This solution uses two external libraries we need to reference: RestSharp to facilitate the API calls and Newtonsoft JSON to deserialize the API responses into objects.

3. Analyze and run the code

We start by declaring the data structures for the API responses. Then, the main function cycles through four nested levels of hierarchyā€Šā€”ā€Šproject / repository / branch / itemā€Šā€”ā€Šcalling these API endpoints and deserializing the results into the corresponding structures. Hence, for each project, we get a list of the repositories it contains and for each repository, we get a list of the items it contains.

When we get to the repository level we donā€™t need to make individual API calls to download every single item on the repository. Instead, we can download all the items, packed into a Zip file with a single API call. In order to do this, we still need the item list, because we have to build a JSON structure containing every item on the list where the property gitObjectType equals ā€œblobā€. This JSON structure will then be posted to the /blobs endpoint to obtain the Zip file as a response.

Note we are also saving the original JSON item list we got from the repository call. This will be needed to map the files inside the Zip package, because these are presented in a single flat directory and their names are the object ids and not their actual names and extension. This is where the --unzip argument enters. If itā€™s omitted, the process does not go further and we get a simple backup: for every repository of each project we get a Zip file with the content and a JSON file to map it.

Usage

Paramenters:

  • --token
    • token.bin: Use an encrypted .bin file (based on hardware IDĀ“s) with your personal access token in. (Remember to run --tokenfile <token.data> to create the file first beside the application .exe!)
    • <token.data>: Replace this with your Azure DevOps personal access token.
  • --org: Replace this with your Azure DevOps organization name.
  • --backup: Specify the local directory where you want to store the Azure DevOps repository backups.
  • --server: IP address or domain name of the SMTP server for sending email notifications.
  • --port: The SMTP server port
  • --from: The email address for the notification emails to send from.
  • --toemail: The email address to report is send to.
  • --unzip: Use this option to extract the downloaded Zip files into a directory structure.
  • --cleanup: Use this option to cleanup the blob files downloaded from the API in the backup folder.
  • --daystokeepbackup: Set the number of days to retain backup files before automatic removal (default is 30 days if not specified).
  • --simpelreportlayout: Use this option to use the old email report layout.
  • --priority <high/low>: Specify the email priority for notifications (e.g., high, normal, low). Default (normal) if not set.
  • --help, /h or /?: Show the help menu
  • --info or /about: Show the about menu

Mandatory arguments is: --token, --org, --outdir, --server, --port, --from and --to

A bit more information about some arguments:

If the --unzip argument is present, the program will create a directory for each repository based on the information provided by each Zip/JSON file pair. In this directory, we will get the original file and folder structure with real file names and extensions. Looping through all the items on the JSON list file, we consider a simple condition: if the item is a folder we create the directory according to the item.path property. Otherwise, we assume itā€™s a blob and we extract it from the Zip archive into the corresponding directory assigning the original file name and extension.

If the --cleanup argument is present, the program will cleanup the blob files downloaded from the API in the backup folder. If not present, the downloaded original .json and .zip files is still the backup folder. The argument --unzip id needed for this to work.

If the --daystokeepbackup argument is present, the program will keep that number of days in the backup folder. If not present the default days of backups to keep is 30 days. It looks at the backup folder, and see when last changed. If the days matches the days you set, the program will delete the old backup in the backup folder you had set.

If the --simpelreportlayout argument is present, the program will use the old email report layout, else it will use the new default.

Logs

Logs for backup jobs is keept in the .\Log folder for 30 days beside AzureDevOpsBackup.exe.

Final thoughts

This is not an exhaustive method to retrieve every artifact on Azure DevOps. Thereā€™s a lot more to be done to make this a complete solution. However, itā€™s a good starting point to backup your Azure DevOps projects and keep a local repository of these like I do! šŸ˜œšŸ˜‰

There is send an email report to the specified email address when the backup is done with status and usefull information about the backup job and more information.

Examples

Check out the examples here:

Email report sample:

Full layout:

Screenshot

Simpel layout:

Screenshot

Console use:

Help menu:

Screenshot

About menu:

Screenshot


Building

  • Get Visual Studio 2022 (Community Edition is fine)
  • Install ".NET desktop development" workload
    dotnet-desktop-develoment.png
  • Remember to select: Install .NET Framework 4.8 SDK
  • Build the solution in Visual Studio

Contributing

If you want to contribute to this project, please open an issue or submit a pull request. I welcome contributions :)

See CONTRIBUTING for more information.

First off, thanks for taking the time to contribute! Contributions are what makes the open-source community such an amazing place to learn, inspire, and create. Any contributions you make will benefit everybody else and are greatly appreciated. Feel free to send pull requests or fill out issues when you encounter them. I'm also completely open to adding direct maintainers/contributors and working together! :)

Please try to create bug reports that are:

  • Reproducible. Include steps to reproduce the problem.
  • Specific. Include as much detail as possible: which version, what environment, etc.
  • Unique. Do not duplicate existing opened issues.
  • Scoped to a Single Bug. One bug per report.

Support

Commercial support

This project is open-source and I invite everybody who can and will to contribute, but I cannot provide any support because I only created this as a "hobby project" ofc. with tbe best in mind. For commercial support, please contact me on LinkedIn so we can discuss the possibilities. Itā€™s my choice to work on this project in my spare time, so if you have commercial gain from this project you should considering sponsoring me.

Thanks.

Reach out to the maintainer at one of the following places:

License

This project is licensed under the MIT License - see the LICENSE file for details.

See LICENSE for more information.

Sponsors

Advanced Installer

The installer is created from a Free Advanced Installer License for Open-Source from https://www.advancedinstaller.com/ - check it out!

Advanced Instzaller

JetBrains

JetBrains specialises in intelligent, productivity-enabling tools to help you write clean, quality code across . NET, Java, Ruby, Python, PHP, JavaScript, C# and C++ platforms throughout all stages of development. https://www.jetbrains.com/ - check it out!

SAST Tools

PVS-Studio - static analyzer for C, C++, C#, and Java code.

About

This project provides a small console tool that enables you to backup your Azure DevOps Repos (Git based) using the API for Azure DevOps. The tool is built using C# and backups your repos to your local server/machine for an "offline" backup, and it can send you an e-mail report for backup status, deleted old backup and so on.

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published