Automatically Backing Up TeamCity

September 23rd 2013 TeamCity Administration

After switching to TeamCity one of the first tasks was setting up proper backup of its configuration. In CruiseControl.NET all of the configuration is defined in configuration files which can be easily backed up; or even better: stored in source control, since they are edited manually anyway.

There are multiple ways to backup TeamCity data. The only one that can be performed remotely is exposed over TeamCity's web UI. Although it has some limitations, it should be enough for all practical purposes. Fortunately it can also be triggered via REST API for easier automation. The simplest way to call it from command line is by using cURL (line breaks added for readability):

curl -d "" -u username:password 
  "http://localhost/httpAuth/app/rest/server/backup?
    includeConfigs=true&
    includeDatabase=true&
    includeBuildLogs=true&
    fileName=TeamCity_Backup"

Running the above script results in a new file every time it is run because of the timestamp that is automatically appended to filename. I didn't like that because it would require me to somehow automatically delete older files if I didn't want them to pile up. The web UI offers an option to disable the timestamp suffix:

Add timestamp suffix

The same option is exposed over REST API as well; it can be changed by passing addTimestamp URL parameter (again, line breaks were added later):

curl -d "" -u username:password 
  "http://localhost/httpAuth/app/rest/server/backup?
    addTimestamp=false&
    includeConfigs=true&
    includeDatabase=true&
    includeBuildLogs=true&
    fileName=TeamCity_Backup"

Now the backup is saved with the same filename every time. Though, the old file must be deleted before triggering the backup, otherwise it doesn't overwrite the original file. This can be solved by deleting the old backup just before creating the new one. Of course, you should only do this if you have already copied it to a permanent location beforehand. In my case I'm copying generated backup files to a central backup location on another server using SyncBackSE. There I am using CrashPlan to keep multiple older versions just in case I ever need to revert back a change.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Copyright
Creative Commons License