21

We are using Teamcity 6.5.6 professional version, which gives me the option to run a backup but I do not see any option to schedule it to a particular time.

I am not sure if this version of teamcity even supports scheduled backups. If it is not possible through teamcity GUI, I wonder if there is any other option?

Could someone please help?

Thanks.

kranthi
  • 1,419
  • 6
  • 29
  • 51

7 Answers7

19

I wrote Powershell script for TeamCity auto backups, which you can schedule and run backup.

Here's the code:

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
Ivan Leonenko
  • 2,047
  • 2
  • 27
  • 35
  • Where does the zip file go? – Paul Zahra Oct 12 '16 at 11:15
  • 1
    @PaulZahra, by default backup files are created in /backup And here's a link how to find it https://confluence.jetbrains.com/display/TCD10/TeamCity+Data+Directory – Ivan Leonenko Oct 12 '16 at 11:27
  • 1
    @IvanLeonenko By the way... just found your script here... this may interest you (it has some minor changes)... https://blogs.endjin.com/2013/08/a-step-by-step-guide-to-automating-teamcity-backups/ – Paul Zahra Oct 12 '16 at 11:29
9

You could use the REST API to run the backup. We actually use TeamCity to run a scheduled build at midnight each day. That build makes a call to the rest api to do the backup.

Mike Two
  • 41,318
  • 7
  • 77
  • 93
  • Thanks.The REST API says I should make a POST request to my teamcity server with all the parameters required. If I set up a build for this in teamcity, what type of build runner I should use? – kranthi May 11 '12 at 11:54
  • @kranthi - we wrote a simple python script and use the command line runner. Any scripting language will do. – Mike Two May 11 '12 at 13:24
  • 4
    Thanks to this suggestion I was able to build a solution based on the REST API. The source-code is hosted on github and here is a blog post describing how it workds: http://daniellang.net/how-to-automate-teamcity-backups-using-its-rest-api/ – Daniel Lang Sep 10 '12 at 17:28
9

If you don't want to write programs to perform your task, simply run this command:

wget --user=*** --password=*** "http://localhost:8085/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true&includeBuildLogs=true&fileName=ScheduledBackup-" --post-data= 

where stars should be replaced with your TeamCity login information.

On Windows, you can get WGET as a part of Cygwin package

Alexander Kosenkov
  • 1,560
  • 1
  • 10
  • 19
  • `wget` is also included in [unxutils](http://unxutils.sourceforge.net/) if you want a lighter-weight option than Cygwin. Get both the [unxutils.zip](http://unxutils.sourceforge.net/UnxUtils.zip) and [unxupdates.zip](http://unxutils.sourceforge.net/UnxUpdates.zip); unzip the utils, unzip the updates, move the updates into the `usr/local/wbin` folder for unxutils and then register it on your $PATH. – Damon Jul 15 '14 at 19:42
  • For anyone using UnxUtils: the `--user`, `--password` and `--post-data` parameters aren't accepted by WGET (I don't know if they are in the actual Unix WGET or not). Try using `--http-user` and `--http-passwd`. Didn't find a replacement for `--post-data` in the wget help from UnxUtils. Also, where does the resulting file go?? – Brandon Mar 24 '15 at 12:59
6

For those who wants to trigger builds from Mac OS (use "command line" runner on 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"
Denis Gladkiy
  • 1,823
  • 1
  • 23
  • 37
  • 1
    `curl` should be an option for *(just about?)* any OS. On Windows, you can get ith through [msysgit](http://msysgit.github.io/) which uses MinGW(http://www.mingw.org/), if you let it install the Git commands for all command prompts, making this a candidate for everyone. Also, according to the manual, `curl` defaults to basic auth mode so you can probably drop `--basic` if keeping the command simple. YMMV so test it out on your OS. – Damon Jul 15 '14 at 19:36
5

We run maintainDB.cmd and schedule it with the Windows Task Scheduler, it's a one line command and requires no extra software. maintainDB is fully documented in the TeamCity documentation.

user2570487
  • 111
  • 2
  • 1
  • 1
    maintainDB requires the TeamCity service to be stopped in order to use it, but it is possible to stop and start the service from the command line – WhiteKnight Sep 17 '14 at 13:00
  • 1
    This is (imo) by far the best answer, mainly because it does not require keeping user/password and becuase it uses a super simple built-in tool – stijn Mar 30 '15 at 11:59
3

You can also use the environment variable to have the team city server address resolve at build time:

curl --basic --user user:pasword -X POST "%teamcity.serverUrl%/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true&includeBuildLogs=true&fileName=ScheduledBackup"
Sveinung Kval Bakken
  • 3,676
  • 1
  • 22
  • 30
0

Starting from @Ivan Leonenko script I added some lines of code to wait that the backup ends before exit.

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
bubi
  • 6,035
  • 2
  • 23
  • 38