Select to view content in your preferred language

Backup AGOL Hosted Feature Layers (aka Hosted Feature Services, aka Hosted Feature Collections)

05-28-2021 01:01 PM
Occasional Contributor II

Here is an example python script that can be run to specifically backup hosted feature layers from ArcGIS Online to a local drive or network drive on your computer or network. Although not ideal to code in credentials, if it is stored and ran from a secure machine, it can be scheduled to run automatically with Windows Task Scheduler. Alternatively, you can run this script manually and alter it to prompt you for credentials. Thanks to Adam Koelker for presenting the core of this script via YouTube. I adapted to query and loop through a specific set, and added some print statements. 




from time import strftime
print (strftime("%c"))
from arcgis.gis import GIS

###Authenticate to ArcGIS Online

###Query to for all items to be downloaded. In this case, it's searching for all feature layers marked as authoritative"contentstatus:org_authoritative",item_type="Feature Layer Collection",max_items=1000)

###Output location for downloaded backups

###Initiate cycle to export, download, and delete backups
for item in myFeatureCollections:
		print('Exporting '+str(item.title))
		### Create Backup
		dataitem.export(title=tempfile,export_format="File Geodatabase",parameters=None,wait=True)
		print("Could not create backup for "+str(item.title)+' ('+str(item.itemid)+')')
		### Find and download export,item_type="File Geodatabase")
		print("Downloaded "+str(fgdb.title)+" to "+output)
		print("Could not download export for "+str(myexport[0]))		
		###Delete export
		print("Could not delete export for "+str(fgdb))

print("Script completed at {}".format(strftime("%c")))




EDIT: This query no longer retrieves explicitly hosted feature layers, but also stored web layers. See for updated item types that can be queried



4 Replies
New Contributor III

Hi @NorthSouthGIS, this is exactly what I have been looking for, I currently run a similar script that I've picked up somewhere online in which I need to enter in my credentials, the location to save the data to and the number of hosted feature layers to backup. So this is great. 
I have added the script to a notebook in AGOL so that I can schedule it to run when I am away. I've done a test run and everything seems to complete without an issue. However, when I go to check my backup location (D:\Northumberland_PP\Back_ups\Automated_backups) there is nothing there. 
You don't happen to have any thoughts on why this might be do you?

0 Kudos
New Contributor

Hi Andrew,

Did you ever figure this out? Having the same issue. Tried reversing the backslashes, using a simple Temp folder on my C: drive, using a UNC file path, etc.


0 Kudos
New Contributor III

Hi @TeriL, I didn't find a way to get it to work unfortunately and then it dropped off my radar. Perhaps I'll contact ESRI to see if they can advise

0 Kudos
New Contributor III

Hi @Teri , I contacted ESRI abouth this and they suggested I post on here. See the below. 

I have had a further look into your case, and as you are using ArcGIS Api for Python you require a developer support subscription for further support on this. 
I would recommend that you post a thread on the page that you found the notebook script on to find a solution. 
As this is out of scope of regular support I will move to close the case

You haven't happened to find a solution have you?
0 Kudos