Created
April 17, 2024 14:49
-
-
Save pmacMaps/fe4f534607593e529df8e286d73cac16 to your computer and use it in GitHub Desktop.
Export ArcGIS Server Site & ArcGIS Portal Site Sample Scripts
This file contains hidden or 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
# see https://developers.arcgis.com/rest/enterprise-administration/portal/export-site.htm | |
# import modules | |
from arcgis.gis import GIS | |
from os import environ | |
from os import path | |
from datetime import date | |
import logging | |
import sys | |
import requests | |
try: | |
# Date script is run | |
today = date.today() | |
# reformatted date for log file naming convetion | |
file_suffix = date.today().strftime("%m-%d-%Y") | |
# log file name | |
log_file_name = f'Export_ArcGIS_Portal_Site_{file_suffix}.log' | |
# log file | |
log_file = path.join(r'C:\ags-backups\portal', log_file_name) | |
# configure logging for script | |
logging.basicConfig(filename=log_file, filemode='w', format='%(asctime)s - %(message)s', level=logging.INFO) | |
# parameters to sign into Portal | |
portal_url = 'https://some.domain/portal' | |
# username and password are stored as environment variables on machine running the script | |
username = environ.get('portal_user') | |
password = environ.get('portal_password') | |
# output location for export site | |
export_location = r'D:\ags-backups\portal' | |
# validate output location | |
validate_location = True | |
# add message | |
logging.info('signing into Portal...') | |
# login to portal | |
gis = GIS(portal_url, username, password) | |
# add message | |
logging.info(f'signed into: {gis}') | |
# token for portal connection | |
portal_token = gis._con.token | |
# export site url | |
export_url = f'{portal_url}/portaladmin/exportSite' | |
# format in a web browser with paramaters | |
# export_url = f'https://some.domain/portal/portaladmin/exportSite?token={portal_token}&location={export_location}&validate={validate_location}&f=json' | |
req_params = { | |
"token" : portal_token, | |
"location" : export_location, | |
"validate" : validate_location, | |
"f" : "json" | |
} | |
# add message | |
logging.info('submitting POST request to run Portal site export...') | |
# Portal site export function request | |
post_req = requests.post(export_url, req_params) | |
# request is expected to return data | |
# convert response into json | |
resp = post_req.json() | |
# expect "status" and "location" keys | |
the_status = '' | |
the_location = '' | |
# check if keys exist in response dictionary | |
# "status" key | |
if 'status' in resp: | |
the_status = resp['status'] | |
else: | |
the_status = '"status" key not in response' | |
# "location" key | |
if 'location' in resp: | |
the_location = resp['location'] | |
else: | |
the_location = '"location" key not in response' | |
# add response messages to log | |
logging.info(f'response "status" is: {the_status}') | |
logging.info(f'response "location" is: {the_location}') | |
except (Exception, EnvironmentError) as e: | |
# information about the error | |
tbE = sys.exc_info()[2] | |
# write the line number the error occured on | |
logging.info(f"Failed at Line {tbE.tb_lineno}") | |
# write error message | |
logging.info(f"Error: {e}") |
This file contains hidden or 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
# see https://developers.arcgis.com/rest/enterprise-administration/server/exportsite.htm | |
# import modules | |
from arcgis.gis import GIS | |
from os import environ | |
from os import path | |
from datetime import date | |
import logging | |
import sys | |
import requests | |
try: | |
# Date script is run | |
today = date.today() | |
# reformatted date for log file naming convetion | |
file_suffix = date.today().strftime("%m-%d-%Y") | |
# log file name | |
log_file_name = f'Export_ArcGIS_Server_Site_{file_suffix}.log' | |
# log file | |
log_file = path.join(r'C:\ags-backups\server', log_file_name) | |
# configure logging for script | |
logging.basicConfig(filename=log_file, filemode='w', format='%(asctime)s - %(message)s', level=logging.INFO) | |
# parameters to sign into Portal (assumes primary site administrator for ArcGIS Server is disabled) | |
# see https://enterprise.arcgis.com/en/server/latest/administer/windows/disabling-the-primary-site-administrator-account.htm | |
portal_url = 'https://some.domain/portal' | |
# username and password are stored as environment variables on machine running the script | |
username = environ.get('portal_user') | |
password = environ.get('portal_password') | |
# output location for export site | |
export_location = r'D:\ags-backups\server' | |
# validate output location | |
# I found that setting this to True does not export site; it just validates the output location | |
validate_location = False | |
# add message | |
logging.info('signing into ArcGIS Portal...') | |
# login to portal | |
gis = GIS(portal_url, username, password) | |
# add message | |
logging.info(f'signed into: {gis}') | |
# token for portal connection | |
portal_token = gis._con.token | |
# export site url - POST url | |
export_url = 'https://some.domain/server/admin/exportSite' | |
# format in a web browser with paramaters | |
# export_url = f'https://some.domain/server/admin/exportSite?token={portal_token}&location={export_location}&validate={validate_location}&f=json' | |
# parameters to put in POST request | |
req_params = { | |
"token" : portal_token, | |
"location" : export_location, | |
"validate" : validate_location, | |
"f" : "json" | |
} | |
# add message | |
logging.info('submitting POST request to run Server site export...') | |
# run AGS Server site export request | |
post_req = requests.post(export_url, req_params) | |
# request is expected to return data | |
# convert response into json | |
resp = post_req.json() | |
# expect "status" and "location" keys | |
the_status = '' | |
the_location = '' | |
# check if keys exist in response dictionary | |
# "status" key | |
if 'status' in resp: | |
the_status = resp['status'] | |
else: | |
the_status = '"status" key not in response' | |
# "location" key | |
if 'location' in resp: | |
the_location = resp['location'] | |
else: | |
the_location = '"location" key not in response' | |
# add response messages to log | |
logging.info(f'response "status" is: {the_status}') | |
logging.info(f'response "location" is: {the_location}') | |
except (Exception, EnvironmentError) as e: | |
# information about the error | |
tbE = sys.exc_info()[2] | |
# write the line number the error occured on | |
logging.info(f"Failed at Line {tbE.tb_lineno}") | |
# write error message | |
logging.info(f"Error: {e}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment