使用 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:
你可以在 Web 界面中指定要备份的设置。