HOW TO
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.
from arcgis.gis import GIS import arcpy import os import zipfile import glob import shutil arcpy.env.overwriteOutput = True
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"
#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)
for folder in folders: with zipfile.ZipFile(folder) as zf: folder.replace('_','') zf.extractall(destination)
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)
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")
Get help from ArcGIS experts
Download the Esri Support App