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
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:
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:
Execute-BackupArchive "<SOURCE_DIRECTORY>" "<DESTINATION_DIRECTORY>" 7
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.