我们使用的是 Teamcity 6.5.6 专业版,它让我可以选择运行备份,但我没有看到任何将其安排到特定时间的选项。
我不确定这个版本的 teamcity 是否支持计划备份。如果无法通过teamcity GUI,我想知道是否还有其他选择?
有人可以帮忙吗?
谢谢。
我为 TeamCity 自动备份编写了 Powershell 脚本,您可以安排和运行备份。
这是代码:
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
)
$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 "USER" "PASSWORD"
}
$server = "http://YOUR_SERVER"
$addTimestamp = $true
$includeConfigs = $true
$includeDatabase = $true
$includeBuildLogs = $true
$includePersonalChanges = $true
$fileName = "TeamCity_Backup_"
Execute-TeamCityBackup $server $addTimestamp $includeConfigs $includeDatabase $includeBuildLogs $includePersonalChanges $fileName
您可以使用REST API来运行备份。我们实际上使用 TeamCity 在每天午夜运行预定的构建。该构建调用其余 api 进行备份。
如果您不想编写程序来执行任务,只需运行以下命令:
wget --user=*** --password=*** "http://localhost:8085/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true&includeBuildLogs=true&fileName=ScheduledBackup-" --post-data=
其中星星应替换为您的 TeamCity 登录信息。
在 Windows 上,您可以将 WGET 作为 Cygwin 包的一部分
对于那些想要从 Mac OS 触发构建的人(在 TeamCity 上使用“命令行”运行程序):
curl --basic --user user:password -X POST "http://team.city.server:8111/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true&includeBuildLogs=true&fileName=ScheduledBackup"
我们运行maintainDB.cmd 并使用Windows 任务计划程序对其进行计划,它是一个单行命令,不需要额外的软件。维护数据库在 TeamCity 文档中有完整记录。
您还可以使用环境变量在构建时解析团队城市服务器地址:
curl --basic --user user:pasword -X POST "%teamcity.serverUrl%/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true&includeBuildLogs=true&fileName=ScheduledBackup"
从@Ivan Leonenko 脚本开始,我添加了一些代码行以等待备份在退出前结束。
function Execute-HTTPCommand() {
param(
[string] $method,
[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 = $method
if ($method -ne "GET")
{
$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);
Write-Host "Starting TeamCity backup"
Execute-HTTPCommand "POST" $TeamCityURL $username $password
}
function Wait-TeamCityBackup() {
param(
[string] $server,
[string] $username,
[string] $password
)
$GetBackupStatusUrl = [System.String]::Format("{0}/httpAuth/app/rest/server/backup",
$server);
do {
Start-Sleep -Seconds 1
$backupStatus = Execute-HTTPCommand "GET" $GetBackupStatusUrl $username $password
Write-Host $backupStatus
} while ($backupStatus -eq 'Running')
}
$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
Wait-TeamCityBackup $server $username $password