Select to view content in your preferred language

to export a portal site using python

2875
8
02-08-2017 12:00 AM
ЕвгенийДенисов
Emerging Contributor

Can someone help me with a python script to export a portal site.

# -*- coding: utf-8 -*-
import arcrest

tokenUrl = "https://{domain}/{web-adaptor}/sharing/rest/generateToken"
org_url = "https://{domain}/{web_adaptor}/sharing/rest"

sh = arcrest.PortalTokenSecurityHandler(username="{user}", password="{pass}", org_url=org_url,
                                        token_url=tokenUrl)

org = arcrest.manageportal.PortalAdministration("https://{domain}/{web-adaptor}/portaladmin", sh)
org.exportSite(r"D:\backups\ags_portal")

Result:

I changed the script, and now there are no errors:

sh = arcrest.PortalTokenSecurityHandler(username="user", password="pass", org_url=org_url,
                                        token_url=tokenUrl)
psh = sh.portalServerHandler("https://servername:6443/arcgis")

arcrest.manageportal.PortalAdministration("https://domain/web-adaptor/portaladmin", 
                                          psh).exportSite(r"D:\backups\ags_portal")


The script is executed, the result is not.

Links, name of portal admin and password are correct.

0 Kudos
8 Replies
JonathanQuinn
Esri Notable Contributor

I haven't used arcrest, but if you were to run Fiddler as you run the script, you should see the requests that your script is making to the Portaladmin API.  You should be then able to confirm that you're providing a token with the exportSite request.  You can also print what the sh variable stores, as I'm guessing sh stores a token and then arcrest.manageportal.PortalAdministration extracts the token somehow.  Personally, I would write my own script using the urllib2 and the json modules.

0 Kudos
ЕвгенийДенисов
Emerging Contributor

Thank you.

Details of the error in variable "org":

PortalAdministration.federation:

Unfortunately I did not get the portal token by using standard libraries. Please, give an example of how you do it.

0 Kudos
LukeWebb
Frequent Contributor

Have never used these features, but the getting started page on ArcRest handles tokens not via a url:

import arcrest
from arcresthelper import securityhandlerhelper  
config = {'username': 'myusername', 'password': 'myp4ssword'} 
token = securityhandlerhelper.securityhandlerhelper(config) 
admin = arcrest.manageorg.Administration(securityHandler=token.securityhandler) 
content = admin.content 
userInfo = content.users.user() 
userInfo.folders

This securityhandlerhelper takes a token URL as a optional parameter:

arcresthelper package — ArcREST 3.5.4 documentation 

e.g. you can do:

token = securityhandlerhelper.securityhandlerhelper(config, token_url=tokenURL) 
0 Kudos
ЕвгенийДенисов
Emerging Contributor
import arcrest
from arcresthelper import securityhandlerhelper

token = securityhandlerhelper.securityhandlerhelper({"username": "username", "password": "password",
                                                     "org_url": "https://domen/gisportal"})

org = arcrest.manageportal.PortalAdministration("https://domen/gisportal/portaladmin",
                                                securityHandler=token.securityhandler)
org.exportSite(r"D:\backups\ags_portal")

When I create a token, getting an exception "Admistrative access denied, unable to check if hosting servers". Export does not work still.

0 Kudos
JonathanQuinn
Esri Notable Contributor

Can you sign into the Portaladmin API with the credentials you're using in the script?  The error indicates the account you're using isn't an Administrator in the portal.  If you can sign into the Portaladmin API, then you shouldn't have a problem in the script.

0 Kudos
LukeWebb
Frequent Contributor

Please try using this handler instead:

username = "A USERNAME"     
pw = "PASSWORD"     
adminUrl = "https:///portal/sharing/rest     
tokenUrl = "https:///portal/sharing/rest/generateToken"
sh = arcrest.PortalTokenSecurityHandler(username=username, password=pw, org_url=adminUrl, token_url=tokenUrl)
0 Kudos
ЕвгенийДенисов
Emerging Contributor

I decided my task. I do not know how the arcrest works, it is unuseful here.

import requests

session = requests.session()
t1 = session.post("https://domain/web-adaptor/portaladmin/login",
                 [('username', 'username'), ('password', 'password')])
t2 = session.post("https://domain/web-adaptor/portaladmin/exportSite",
                  data=[('location', r"D:\backups"), ('f', 'HTML')])
0 Kudos
JonathanQuinn
Esri Notable Contributor

I've used the requests module, but haven't used requests.sessions(), but you should request the JSON format so that you can parse it, (I assume the contents of "t2"?), so you know if it was successful or not.  If you're on 10.4 or later, you can also use the Web GIS DR tool to backup your Portal.  This will back up all federated Servers and ArcGIS Data Store.

0 Kudos