Hello,
I don't really do any Python scripting but I found this script to backup a hosted feature layer on AGO to my computer. I'm using the python window in ArcPro 3.53 and I'm getting Layer 'King George BMPs' not found. When I run the script I have the right info for org URL, username, password, and backup directory in place. Anyone have any ideas on why it can't find the layer title?
# coding: utf-8
from arcgis.gis import GIS
import os
import datetime
# --- Configuration ---
AGOL_ORG_URL = "https://place-name.maps.arcgis.com"
USERNAME = "username"
PASSWORD = "password" # Storing credentials in a script is less secure; consider alternative authentication methods for production environments
BACKUP_DIR = r"BMPBackup" # Local directory for backups
LAYER_TITLE = "King George BMPs" # Title of the layer to backup
# Ensure backup directory exists
if not os.path.exists(BACKUP_DIR):
os.makedirs(BACKUP_DIR)
# Authenticate to ArcGIS Online
gis = GIS(AGOL_ORG_URL, USERNAME, PASSWORD)
# Search for the hosted feature layer
# Note: You may need to refine the search query if the title isn't unique
search_result = gis.content.search(query=f"title:{LAYER_TITLE} AND type:Feature Layer", max_items=1)
if not search_result:
print(f"Layer '{LAYER_TITLE}' not found.")
exit()
item = search_result[0]
print(f"Found item: {item.title}")
# Export data to a File Geodatabase (FGDB) format
# This creates a new item in AGOL content, which will be deleted later
export_title = f"{item.title}_Backup_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}"
export_item = item.export(export_title, "File Geodatabase", wait=True)
# Download the exported file
download_path = export_item.download(BACKUP_DIR)
print(f"Downloaded backup to: {download_path}")
# Delete the temporary export item from AGOL content
export_item.delete()
print("Temporary export item deleted from ArcGIS Online.")
print("Backup process complete.")
Layer 'King George BMPs' not found.