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 hosted feature services as file geodatabases 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
import datetime as dt
username = input("Input your username: ")
gis = GIS("https://arcgis.com", username)

Returning this block of code prompts entering a password. Following this, search for feature services to download. Input a high value for the max_items parameter 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 modify the query to download non-shared/non-export-enabled services that others in the organization own. The below cell also asks for a file path, which can be entered regularly as Jupyter Notebook accounts for escape characters when the string is used as the output location in the download function.

folder_path = input("Please enter the file location to store the backups: ")
num_items = int(input("How many items do you want to back up? "))
query_string = "type:Feature Service, owner:{}".format(username)
items = gis.content.search(query=query_string, max_items=num_items, sort_field='modifed', sort_order='desc')
print(str(len(items)) + " items will be backed up to " + folder_path +". See the list below:")
items

A list similar to the following 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...]

Lastly, we create a function that iterates through the list of Feature Services (there is logic in the script below to avoid downloading hosted feature layer views, as this would create duplicate backups), generates FGDB titles, exports and downloads each Feature Service as a FGDB, and finally, deletes the FGDB Items from My Content in ArcGIS Online:

def download_as_fgdb(item_list, backup_location):
    for item in item_list:
        try:
            if 'View Service' in item.typeKeywords:
                print(item.title + " is view, not downloading")
            else: 
                print("Downloading " + item.title)
                version = dt.datetime.now().strftime("%d_%b_%Y")
                result = item.export(item.title + "_" + version, "File Geodatabase")
                result.download(backup_location)
                result.delete()
                print("Successfully downloaded " + item.title)
        except:
            print("An error occurred downloading " + item.title)
    print("The function has completed")

download_as_fgdb(items, folder_path)

Related Information

ArcGIS API for Python - Content Manager

Last Published: 7/23/2020

Article ID: 000022524

Software: ArcGIS Online 8.2