0

我正在尝试开发一个 python 脚本来运行 REST 备份过程,如http://confluence.jetbrains.com/display/TW/REST+API+Plugin#RESTAPIPlugin-DataBackup所示

这是我的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib
import urllib2

"""
Data Backup
+++++++++++
Start backup: POST http://teamcity:8111/httpAuth/app/rest/
server/backup?includeConfigs=true&includeDatabase=true&
includeBuildLogs=true&fileName=<fileName> 
where <fileName> is the prefix of the file to save backup to. 
The file will be created in the default backup directory (see more).
"""

url = "http://localhost/httpAuth/app/rest/server/backup"

# === EDITED code = now working (see my answer below) ===
url = "http://localhost/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true&
includeBuildLogs=true&fileName=TCBACKUP"
# === /EDITED code (see my answer below) ===

params = {
'fileName':'TCBACKUP',
'includeBuildLogs':'false',
'includeDatabase':'true',
'includeConfigs':'true'
}

post_data = urllib.urlencode(params)

req = urllib2.Request(url, post_data, headers={'Content-Type': 'application/xml'})

password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, url, 'user', 'pass')

auth_manager = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_manager)

urllib2.install_opener(opener)

handler = urllib2.urlopen(req)
handler.close()

无法弄清楚为什么它会失败

urllib2.HTTPError: HTTP Error 400: Bad Request

和 REST 日志显示

WARN [hon-urllib/2.7 ] - est.jersey.ExceptionMapperUtil - 
Error 'Invalid request. Please check the request URL and 
data are correct.' for request http://server/app/rest/server/backup. 
Sending Bad Request error in response: jetbrains.buildServer.server.rest.errors.BadRequestException: 
No target file name specified.

没有目标文件名?真的吗?打印 post_data 给了我:

includeConfigs=true&includeDatabase=true&includeBuildLogs=false&fileName=TCBACKUP

任何指针将不胜感激

4

2 回答 2

0

Python 代码很好——从 TC 文档中唯一不清楚的是——URL 仍然必须构建为

"POST http://teamcity:8111/httpAuth/app/rest/server/backup?
includeConfigs=true&includeDatabase=true&includeBuildLogs=true&
fileName=backup" 

即使 urllib2 POST 参数被传递给请求。

那好吧

于 2013-04-15T13:57:51.903 回答
0

就我而言,除了 URL 没有用双引号括起来之外,我做的一切都很好。

所以我必须做这样的事情curl才能让它工作

curl -d "" -X POST "http://<TeamCityUserName>:<TeamCityPassword>@teamcity.pi.home/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true&includeBuildLogs=true&fileName=testBackup.bak"
于 2020-07-19T08:27:37.203 回答