English

How To: Back up hosted content by looping through and downloading hosted feature services in FGDB format

Summary

The recommended and supported workflows for backing up content in ArcGIS Online are to export and download hosted feature services, or to copy hosted feature services to ArcGIS Enterprise through Distributed Collaboration. This article presents a code sample showing how to export and download Hosted Feature Services in file geodatabase format.

Procedure

Using the ArcGIS API for Python, it is possible to automate exporting and downloading every hosted feature service in the organization as a file geodatabase to a location on a local drive. The code sample below shows how to execute this in a Jupyter Notebook environment:

from arcgis.gis import GIS
gis = GIS("https://arcgis.com", "Username")

Returning this block of code prompts entering a password. Following this, search for feature services to download. Set the max_items parameter to a high value to ensure all Feature Services are returned in the list. In this example, the query searches for only the signed-in user's items. Connect with administrative privileges and remove owner:Username from the query to download non-shared/non-export-enabled services that others in the organization own. 

items = gis.content.search(query="type:Feature Service, owner:Username", max_items=1000,)
display(items)

The following list is returned:

[<Item title:"Title1" type:Feature Layer Collection owner:Username>,
 <Item title:"Title2" type:Feature Layer Collection owner:Username>,
 <Item title:"Title3" type:Feature Layer Collection owner:Username>,
+ about 100 more in my case...]

Iterate through the list of Feature Services, generate FGDB titles, export and download each Feature Service as a FGDB, and finally delete the FGDB Items from My Content in ArcGIS Online:

for item in items:
    try:
        service_title = item.title
        version = "1"
        fgdb_title = service_title+version
        result = item.export(fgdb_title, "File Geodatabase")
        result.download(r'C:\Users\Username\Desktop\SomeLocation')
        result.delete()
    except:
        print("An error occurred downloading"+" "+service_title)

Result in the local drive (we set the local variable version to "5" in this example):

Exemptions are printed without interrupting the script. Most errors are a keyerror: 'exportItemId' errors, thrown for proxied ArcGIS Enterprise services:

The above Python calls can be slightly modified to run as a standalone script from IDLE:

#Connect to the GIS

from arcgis.gis import GIS
gis = GIS("https://arcgis.com", "Username")

#List items to download

items = gis.content.search(query="type:Feature Service, owner:Username", max_items=1000,)
print(items)

#Iterate through the list and download the Feature Services as FGDBs

for item in items:
    try:
        service_title = item.title
        version = "1"
        fgdb_title = service_title+version
        result = item.export(fgdb_title, "File Geodatabase")
        result.download(r'C:\Users\Username\Desktop\SomeLocation')
        result.delete()
    except:
        print("An error occurred downloading"+" "+service_title)

print("Script has completed downloading the items")

Related Information

ArcGIS API for Python - Content Manager