Endjin - Home

A Step by Step Guide to Automating TeamCity Backups

by Howard van Rooijen

Now that you’ve set-up your TeamCity environment, or migrated it from one machine to another, you probably want to make sure that if anything happens to the hardware you’ve installed it on, you can get back up and running quickly.

Step 1: Configure Execute-TeamCityBackup.ps1

Execute-TeamCityBackup.ps1 (by Ivan Leonenko) is a great little script for calling the TeamCity REST API and executing the backup.

The script contains a series of variables that need to be set in order for the script to run successfully. Edit the following settings with the details of your TeamCity environment and enable / disable the various backup options:

One thing to note is that the TeamCity user credentials you supply must that of a TeamCity admin role. I would suggest creating a specific new TeamCity backup user.

Once configured correctly, save the file.

The backup can be run either on the TeamCity Server or any other server that can connect to the TeamCity Server as the backup is executed via the TeamCity Server’s REST API.

Add the file to a Utilities directory on whichever machine you choose. Within the instructions this folder will be referred to as <PATH-TO-FILE>

Step 2: Create a Scheduled Task

In order to run the backup PowerShell script in an automated fashion, we need to create a scheduled task that can run at a pre-defined time.

Open up Task Scheduler and select the option to create a “Basic Task”:


Next add the name of the task (and optionally a description):


Next, specify when you want the task to run:


Next, specify that you want to start a program:


Next, specify the following parameter:

Powershell – file "<PATH-TO-FILE>\Execute-TeamCityBackup.ps1"


When you click “Next” you will be prompted:


Select “Yes”.

Next, you will be presented the summary screen, review and click “Finish”:


Step 3: Test the backup

Select the Scheduled Task from the list, right click and select “Run”:


You should see a PowerShell window appear and the script should be run. If the script errors (and you can see red text), go back through this guide and double check your settings.

Next, navigate to the Backup history tab via the TeamCity Admin interface. You should see that there is a backup in progress:


Refresh the screen and you should see when the backup completes:


Step 4: Create a Scheduled Task for Backup Archive Management

Now that you have successfully created an automated backup task, backup archives will soon start to mount up and consume all available free space on your TeamCity Server.

Here is the source for Execute-BackupArchive.ps1:

This script makes use of a handy MINAGE feature in robocopy that allows you to select files based on their age. The script moves any files older than n days (where n is configurable) into a separate folder, then we delete that folder. We then mirror the remaining files to our destination directory.

First you need to configure the Execute-BackupArchive.ps1 file to configure which directory you want to copy backups from, which directories you want to copy backups too and how many days’ worth of backups you want to keep in your source directory:


Once you have made these changes, save the Execute-BackupArchive.ps1 to a “utilities” folder on the TeamCity Server. This path will be referred to as <PATH-TO-FILE> in the instructions.

Follow the process listed in Step 2, to create a new Scheduled Task, except when you are prompted select a time, enter a time that is later than the backup task (to enable it to run to completion before the archive task runs). When you get to the “Start a Program” use the following settings instead:

Powershell – file "<PATH-TO-FILE>\Execute-BackupArchive.ps1"

Finally, test the archive process, by selecting and running the scheduled task, as listed in Step 3, but selecting your newly listed scheduled task.

Work Smarter, Not Harder.

@HowardvRooijen | @endjin

About the author

Howard spent 10 years as a technology consultant helping some of the UK's best known organisations work smarter, before founding endjin in 2010. He's a Microsoft Accelerator Mentor, and a Microsoft Azure MVP. You can follow him on Twitter via @HowardvRooijen