A Step by Step Guide to Automating TeamCity Backups
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.
function Execute-HTTPPostCommand() {
param(
[string] $url,
[string] $username,
[string] $password
)
$authInfo = $username + ":" + $password
$authInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::Default.GetBytes($authInfo))
$webRequest = [System.Net.WebRequest]::Create($url)
$webRequest.ContentType = "text/html"
$PostStr = [System.Text.Encoding]::Default.GetBytes("")
$webrequest.ContentLength = $PostStr.Length
$webRequest.Headers["Authorization"] = "Basic " + $authInfo
$webRequest.PreAuthenticate = $true
$webRequest.Method = "POST"
$requestStream = $webRequest.GetRequestStream()
$requestStream.Write($PostStr, 0, $PostStr.length)
$requestStream.Close()
[System.Net.WebResponse] $resp = $webRequest.GetResponse();
$rs = $resp.GetResponseStream();
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
[string] $results = $sr.ReadToEnd();
return $results;
}
function Execute-TeamCityBackup() {
param(
[string] $server,
[string] $addTimestamp,
[string] $includeConfigs,
[string] $includeDatabase,
[string] $includeBuildLogs,
[string] $includePersonalChanges,
[string] $fileName,
[string] $userName,
[string] $password
)
$TeamCityURL = [System.String]::Format("{0}/httpAuth/app/rest/server/backup?addTimestamp={1}&includeConfigs={2}&includeDatabase={3}&includeBuildLogs={4}&includePersonalChanges={5}&fileName={6}",
$server,
$addTimestamp,
$includeConfigs,
$includeDatabase,
$includeBuildLogs,
$includePersonalChanges,
$fileName);
Execute-HTTPPostCommand $TeamCityURL $username $password
}
$server = "http://YOUR_SERVER"
$addTimestamp = $true
$includeConfigs = $true
$includeDatabase = $true
$includeBuildLogs = $true
$includePersonalChanges = $true
$fileName = "TeamCity_Backup_"
$username = "USERNAME" # Must be a TeamCity Admin
$password = "PASSWORD"
Execute-TeamCityBackup $server $addTimestamp $includeConfigs $includeDatabase $includeBuildLogs $includePersonalChanges $fileName $username $password
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:
$server = "http://YOUR_SERVER"
$addTimestamp = $true
$includeConfigs = $true
$includeDatabase = $true
$includeBuildLogs = $true
$includePersonalChanges = $true
$fileName = "TeamCity_Backup_"
$username = "USERNAME" # Must be a TeamCity Admin
$password = "PASSWORD"
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:
function Execute-BackupArchive
{
param
(
[string] $sourcePath,
[string] $destinationPath,
[int] $minimumAge
)
$archiveFilesPath = ($sourcePath + "\to_delete")
robocopy $sourcePath $archiveFilesPath /e /MOVE /MINAGE:$minimumAge
Remove-Item -Recurse -Force $archiveFilesPath
robocopy $sourcePath $destinationPath /e /MIR
}
Execute-BackupArchive "<SOURCE_DIRECTORY>" "<DESTINATION_DIRECTORY>" 7
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.
@HowardvRooijen | @endjin