Last active
August 29, 2015 14:17
-
-
Save onjin/bdc09e07ca78920354f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
$ ./export_confulence_space.py -l http://wiki.some.com -u username -p password -s SPACEKEY | |
$ ./export_confulence_space.py -l http://wiki.some.com -u username -p password -a # all spaces available for given user | |
""" | |
import os | |
import urllib2 | |
import xmlrpclib | |
from urllib2 import URLError, HTTPError | |
import argparse | |
def get_spaces(token): | |
return s.confluence1.getSpaces(token) | |
def export_space(token, space_key): | |
print "Exporting Space: " + space_key | |
# Perform the export and get a download URL | |
downloadURL = s.confluence1.exportSpace(token, space_key, "TYPE_XML") | |
print "Download URL: " + downloadURL | |
# We'll need to authenticate to download the zip file | |
downloadURL = downloadURL + "?os_username=" + username + "&os_password=" + \ | |
password | |
# Configure export file name | |
filePath = exportDir + space_key + ".zip" | |
# Delete any old copy if it exists | |
if os.path.exists(filePath): | |
print "File exists at " + filePath + ". Deleting..." | |
os.remove(filePath) | |
print "Exporting to: " + filePath | |
req = urllib2.Request(downloadURL) | |
try: | |
f = urllib2.urlopen(req) | |
localFile = open(filePath, "w") | |
localFile.write(f.read()) | |
localFile.close() | |
except HTTPError, e: | |
print "Error downloading file - HTTP response: ", e.code | |
return False | |
except URLError, e: | |
print "URL Error: ", e.reason, downloadURL | |
return False | |
print "Export of " + space_key + " complete" | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
'-l', '--url', action='store', required=True, | |
help='wiki url f.i. http://wiki.some.com' | |
) | |
parser.add_argument( | |
'-u', '--username', action='store', required=True | |
) | |
parser.add_argument( | |
'-p', '--password', action='store', required=True | |
) | |
parser.add_argument( | |
'-s', '--space', action='store', required=False, | |
help='space key' | |
) | |
parser.add_argument( | |
'-a', '--all', action='store_true', required=False, | |
help='export all available spaces' | |
) | |
args = parser.parse_args() | |
# e.g. http://wiki.example.com | |
wikiURL = args.url | |
# User must have rights over the space | |
username = args.username | |
password = args.password | |
# Path to store exported zip files (must exist) | |
exportDir = "./" | |
# Initialise XMLRPC session | |
s = xmlrpclib.ServerProxy(wikiURL + "/rpc/xmlrpc") | |
print "Logging in to " + wikiURL + " as user " + username | |
token = s.confluence1.login(username, password) | |
if args.space: | |
print "Exportin space %s" % args.space | |
keys = [args.space] | |
elif args.all: | |
keys = [space['key'] for space in get_spaces(token)] | |
print keys | |
print "Exportin all spaces(%d)" % len(keys) | |
for key in keys: | |
export_space(token, key) | |
print "Logging out..." | |
s.confluence1.logout(token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment