How To: Create and download a file geodatabase replica from a hosted feature service using Python


Hosted feature services can be downloaded as replica geodatabases from the REST endpoint of the service when sync is enabled on the service. In some circumstances (such as period data backups), it may be useful to programmatically create replicas of services.


To create and download a file geodatabase replica from a hosted feature service with Python, follow the steps below.

  1. Copy and paste the following Python script to a text or Python editor.
    import urllib, urllib2, json, time, os
    username = "username"                                             #CHANGE
    password = "password"                                             #CHANGE
    replicaURL = "feature service url/FeatureServer/createReplica"    #CHANGE
    replicaLayers = [0]                                               #CHANGE
    replicaName = "replicaTest"                                       #CHANGE
    def sendRequest(request):
        response = urllib2.urlopen(request)
        readResponse = response.read()
        jsonResponse = json.loads(readResponse)
        return jsonResponse
    print("Generating token")
    url = "https://arcgis.com/sharing/rest/generateToken"
    data = {'username': username,
            'password': password,
            'referer': "https://www.arcgis.com",
            'f': 'json'}
    request = urllib2.Request(url, urllib.urlencode(data))
    jsonResponse = sendRequest(request)
    token = jsonResponse['token']
    print("Creating the replica")
    data = {'f' : 'json',
        'replicaName' : replicaName,
        'layers' : replicaLayers,
        'returnAttachments' : 'true',
        'returnAttachmentsDatabyURL' : 'false',
        'syncModel' : 'none',
        'dataFormat' : 'filegdb',
        'async' : 'true',
        'token': token}
    request = urllib2.Request(replicaURL, urllib.urlencode(data))
    jsonResponse = sendRequest(request)
    print("Pinging the server")
    responseUrl = jsonResponse['statusUrl']
    url = "{}?f=json&token={}".format(responseUrl, token)
    request = urllib2.Request(url)
    jsonResponse = sendRequest(request)
    while not jsonResponse.get("status") == "Completed":
        request = urllib2.Request(url)
        jsonResponse = sendRequest(request)
    userDownloads = os.environ['USERPROFILE'] + "\\Downloads"
    print("Downloading the replica. In case this fails note that the replica URL is: \n")
    jres = jsonResponse['resultUrl']
    url = "{0}?token={1}".format(jres, token)
    f = urllib2.urlopen(url)
    with open(userDownloads + "\\" + os.path.basename(jres), "wb") as local_file:
    print("\n Finished!")
  2. For the first five lines of the script, modify the following:
    • Modify "username" and "password" to match the name and credentials of the administrative account with the hosted feature service.
    • Modify the "feature service url" in "feature service url/FeatureServer/createReplica" with the URL of the hosted feature service to replicate.
    • Modify the [0] with the list of layers and table to include in the replica.
    • Modify "replicaTest" to the users' name for the replica.
  3. Select File > Save.
  4. Navigate to the .py file and double-click the file to run the script.
The script assumes the replica is created asynchronously. After the create replica request is submitted, the server is pinged every five seconds until the zipped file is fully populated, at which point the zipped file is downloaded in the Downloads folder.

Related Information