使用 TeamCity API 進行備份

首先,確保將執行此呼叫的使用者具有 Change backup settings and control backup process 許可權。

# 
# TC Backup Launcher
# Script to launch a backup on the TeamCity Server
#
Param(
    [Parameter(Mandatory=$true)][string]$username,
    [Parameter(Mandatory=$true)][string]$password
)
Begin
{
    $url = "http://teamcity:8111/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true&includeBuildLogs=false&fileName=TeamCity_Backup"
    $username = <username>
    $password = <password>
    
    # Function to realize a POST Operation
    function Execute-HTTPPostCommand() {
        param(
            [string] $target = $null
        )
        $request = [System.Net.WebRequest]::Create($target)
        Write-Host "POST: " $request.RequestUri
        
        $request.PreAuthenticate = $true
        $request.Method = "POST"
        $request.ContentType = "application/xml"
        $request.Headers.Add("AUTHORIZATION", "Basic");
        $request.Accept = "*"
        $request.Credentials = New-Object System.Net.NetworkCredential($username, $password)
         
        $response = $request.GetResponse()
        $xmlout = ""
        
          if($response)
          {
              $sr = [Io.StreamReader]($response.GetResponseStream())
              $xmlout = $sr.ReadToEnd()
          }
        return $xmlout;
    }
    
    Write-Host "Creating a new Backup:"
    
    Execute-HTTPPostCommand $url
}

重要的部分是使用方法 POST 呼叫的 URL:

HTTP://的 TeamCity:8111 / httpAuth /應用/ REST /伺服器/備份 includeConfigs =真 includeDatabase =真 includeBuildLogs =假檔名= TeamCity_Backup?

你可以在 Web 介面中指定要備份的設定。