2006-07-31

Zope export outside zope/使用wget来导出zope文件

以前每次导出zope的某个文件夹都要进入到ZMI的web interface里,一直希望能有脚本在命令行下完成。今天在网上找到一个脚本,可以实现在命令行下导出的功能。

使用wget来导出:
wget -O 3urts96.zexp http://username:password@urts96.uni-trier.de:8080/Projects/2urts96/manage_exportObject?download%3Aint=1&submit=Export'

Code: ---- begin zope_export.py ----
#!/usr/local/bin/python
# Command line args:
# -d optional -- debug mode. If supplied, then just print the wget command
# -s SERVER optional -- default 'localhost'
# -f FOLDER required -- remote folder to backup
# -o LOCALDIR optional -- local output directory, default '.'
# -p PORT optional -- default 8080
# -u USER:PASS optional -- default prompt; username and password in format 'user:pass'
#
# Example usage:
# ./zope_export.py -f 'SRP2002/Calendar' -s myhost.com -o /backup/
import os, time, sys, getopt, getpass
optlist, args = getopt.getopt(sys.argv[1:], 'df:o:p:u:s:')
# opts is a map from opts to default values. You can overrise the
# default values, with, for example, myscript.py -s myhost.com. The
# options with default value None must be supplied at the command line
# or an error will be generated.
opts = {}
opts['d'] = 0 # debug mode
opts['s'] = 'localhost' # remote zope server
opts['f'] = None # remote folder to backup
opts['o'] = '.' # output directory
opts['p'] = 8080 # zope port
opts['u'] = 0 # username:passwd
for (opt, val) in optlist:
#for binary opts, convert to 1
if val == '':
val = 1
opts[opt[1]] = val
for (key, val) in opts.items():
if val==None:
print 'Error: You must supply the -%s option' % key
sys.exit()
if opts['u']==0:
print 'Supply a username and password for %s in format user:pass' % opts['s']
opts['u'] = getpass.getpass('User:Pass ')
folder = opts['f']
server = opts['s']
port = opts['p']
userpass = opts['u']
outDir = opts['o']
debug = opts['d']
script = 'manage_exportObject'
cgi = """download%3Aint=1&submit=Export"""
ymd = time.strftime('%Y-%m-%d', time.gmtime())
outFile = '%s/%s.%s.zexp' % ( outDir, folder.replace('/', '.'), ymd )
url = 'http://%s@%s:%d/%s/%s' % \
( userpass, server, port, folder, script)
if len(cgi)>0:
url = url + '?' + cgi
command = 'wget -q -O %s %s' % ( outFile, url)
if debug:
print command
else:
os.popen(command)
---- end zope_export.p ----


Here is an example file from /etc/cron.daily which calls this script.
If you hardcode your user:pass, make sure you make the file 'chmod 600'

--- begin /etc/cron.daily/zope_course_export ---
#!/bin/sh
# Export the course folder to /backup/
/usr/local/bin/python2.1 /usr/local/bin/zope_export.py -u myuser:mypass -o /backup -f SRP2002 -s myhost.com
--- end /etc/cron.daily/zope_course_export ---

没有评论:

发表评论