如何在teamcity中安排自动备份?

时间:2012-05-11 09:20:36

标签: backup teamcity schedule

我们正在使用Teamcity 6.5.6专业版,这使我可以选择运行备份,但我没有看到任何选项将其安排到特定时间。

我不确定此版本的teamcity是否支持计划备份。如果通过teamcity GUI无法实现,我想知道是否还有其他选择?

有人可以帮忙吗?

感谢。

7 个答案:

答案 0 :(得分:19)

我写了Powershell script for TeamCity auto backups,您可以安排并运行备份。

以下是代码:

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

答案 1 :(得分:9)

您可以使用REST API来运行备份。我们实际上使用TeamCity在每天午夜运行预定的构建。该构建调用其余的api进行备份。

答案 2 :(得分:9)

如果您不想编写程序来执行任务,只需运行以下命令:

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软件包的一部分

答案 3 :(得分:6)

对于那些想要从Mac OS触发构建的人(使用"命令行" TeamCity上的runner):

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"

答案 4 :(得分:5)

我们运行maintainDB.cmd并使用Windows任务计划程序安排它,它是一行命令,不需要额外的软件。 maintainDB完整记录在TeamCity文档中。

答案 5 :(得分:3)

您还可以使用环境变量在构建时解析团队城市服务器地址:

curl --basic --user user:pasword -X POST "%teamcity.serverUrl%/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true&includeBuildLogs=true&fileName=ScheduledBackup"

答案 6 :(得分:0)

从@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