HOW TO

Back up ArcGIS Online items to a database connection with ArcGIS API for Python

Last Published: January 5, 2021

Summary

Items in a zipped file geodatabase (FGDB) may have limited functionality for data backup. Having the data in a database allows easier access to view data. Backing up ArcGIS Online items to a database connection with ArcGIS API for Python enables multiple items to be downloaded, extracted and copied simultaneously.

Procedure

  1. Import the necessary modules, and set the overwriting output option to True.
from arcgis.gis import GIS 
import arcpy 
import os 
import zipfile 
import glob 
import shutil 

arcpy.env.overwriteOutput = True
  1. Specify the path to the project and the login credentials.
gis=GIS("https://arcgis.com","username","password") 
folders = [] 
root = r"Path To Location Where AGOL Items are Downloaded" 
items = ['ItemID1', 'ID2', 'ID3', 'ID4'] 
destination = r"Path To Location Where Downloaded zipped folders are extracted" 
outputPath = r"Full File Path to SDE Connection\<connectionname>.sde"
  1. Download the desired items, which are then stored as zipped FGDBs, and create a list of the downloaded items.
#the variable 'items' can be replaced to get all items in ArcGIS Online if the need is to back up all items 
for item in items: 
  test = gis.content.get(item) 
  fgdb_title = test.title 
  fgdb_title=fgdb_title.replace('_', '') 
  result = test.export(fgdb, "File Geodatabase")
  result.download(root) 
  result.delete() 
  folders.append(root +'\\' +fgdb_title + '.zip') 
  print(folders)
  1. Extract the downloaded zipped FGDBs to a new folder.
for folder in folders: 
  with zipfile.ZipFile(folder) as zf: 
    folder.replace('_','') 
    zf.extractall(destination)
  1. Remove the local files (optional).
Note:
Step 5 to remove the local files is only necessary if an error occurs while running the script due to existing files.
for FGDB in FGDBlist: 
  item_delete = (destination+FGDB+'\\') 

  #Removes FGDBs in Destination folder(optional) 
  shutil.rmtree(item_delete)
  1. Retrieve the extracted FGDBs and loop through them. Rename the feature classes if needed, and copy the feature class to the SDE connection.
FGDBlist=os.listdir(destination) 

for FGDB in FGDBlist: 
  gdb_var=(destination+FGDB+'\\') 
  arcpy.env.workspace=(gdb_var) 
  featureclasses = arcpy.ListFeatureClasses() 
  print(featureclasses) 
  for FC in featureclasses: 
    rename = arcpy.Rename_management(FC, FC + "_copy", "FeatureClass") 
    print(rename) 
    arcpy.FeatureClassToFeatureClass_conversion(gdb_var + FC + "_copy", outputPath, FC + "_copy")

Article ID: 000024135

Software:
  • ArcGIS Online
  • ArcGIS API for Python 1 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options