I've got a number of python scripts that download copies of ArcOnline feature services as part of a nightly backup routine (in lieu of anything simpler). At present, the python script logs into ArcOnline, accesses the hosted feature service and exports a copy of the data to a feature class in a GDB on our server. This python script is then called by Task Scheduler on a nightly basis (user logged off with highest privledges). Last month (while I was on leave), the server was moved from physical on-premises to cloud based, and we started getting the below Error:
Exception: 'LOCALAPPDATA'
I can't find anything online that really points out what the error is or how to fix it. I assume it's an issue accessing the temporary data environments. When I log in and run the script manually (even through Task Scheduler), it works without any problems. It only happens once logged off.
I tried running the following script in IDLE, but that also didn't yield any positive results.
import arcpy
# Reset geoprocessing environment settings
arcpy.ResetEnvironments()
I've tried finding inbuilt environment settings in IDLE (for ArcGIS Pro) but couldn't so don't know where else to look. I've included my script below so that you can analyse it more in depth.
from datetime import datetime, timedelta, date
# *****Update the below 6 lines*****
name = "FS Name" #Name of dataset for use in error email notification
url_fl = "https://services3.arcgis.com/#X#X#X#X#X#X#X#X#X/arcgis/rest/services/FSName/FeatureServer/0" # Service URL for feature layer to download as feature class
destGDB = r"\\server\ArcGIS Online\AGOL Backups\Backups.gdb" #The GDB where the backup feature class will be created
destFC = "FC Name" #The backup feature class name (no spaces - user _ or CamelCase)
monthlimit = datetime.today() - timedelta(days=30) # number of days to keep daily backups
yearlimit = datetime.today() - timedelta(days=365) # number of days to keep monthly backups (1st day of month only) - everything older will be deleted
while True: #If something fails in the main script under "try", the "except" section emails a notification to the GIS Inbox
try:
import arcpy
from arcpy import env
from arcgis import gis
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
import getpass
import json
import requests
from time import strftime
import datetime
def getSecrets():
# Secrets from file stored outside of revison control
with open(r"\\server\file.json") as f:
secrets = json.load(f)
return secrets
secrets = getSecrets()
# Get login credentials # http://docs.python-requests.org/en/latest/user/advanced/
s = requests.Session()
url_gis="https://org.maps.arcgis.com"
s.user = (secrets["username"])
s.pw = (secrets["password"])
#SIGNING INTO ARCGIS ONLINE
print ("Signing into ArcGIS Online")
source = gis.GIS(url_gis, s.user, s.pw) #signing in
print ("Signed into ArcGIS Online")
# CREATING BACKUP OF FEATURE SERVICE # https://community.esri.com/t5/python-questions/using-arcpy-to-copy-a-portal-feature-service-to-a-fgdb-feature/m-p/4285#M394
fl = FeatureLayer(url_fl)
fs = fl.query()
print ("Exporting backup of feature service")
Outputfs = destFC + "_" + strftime("%Y%m%d_%H%M%S")
fs.save(destGDB, Outputfs)
time.sleep(10) #add 10 seconds delay to allow export to complete
print (name + " feature service exported to backup GDB: " + destGDB + "\\" + Outputfs)
print ("Filtering past backups")
arcpy.env.workspace = r"\\server\ArcGIS Online\AGOL Backups\Backups.gdb"
FClist = arcpy.ListFeatureClasses(destFC + "*")
for fc in FClist:
datestamp = datetime.strptime(('{}'.format(fc))[-15:], "%Y%m%d_%H%M%S")
day = (datestamp.day)
print (fc + "..........Backup date:" + str(datestamp))
if datestamp < yearlimit: #data more than 365 days old
print (" Older than 12 months: delete backup")
arcpy.management.Delete(fc)
print ("Deleted")
elif datestamp < monthlimit: #fc more than 90 days old
if day == 1:
print (" 1st of month & 4-12 months old: retain as monthly backup") #fc from 1st of Month and more than 90 days old
else:
print (" Older than 3 months and not the 1st of the month: delete backup")
arcpy.management.Delete(fc) #fc NOT from 1st of Month and more than 90 days old
print ("Deleted")
else:
print (" Less than 3 months old: retain backup")
print ("Script finished")
break # Stops script here
except Exception as e:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
fromaddr = "us@org.com.au"
toaddr = "us@org.com.au"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = name + " backup process failed"
# Enter email body text below
body = "There has been an error backing up the feature service. Please check the script to troubleshoot any problems. Exception: " + str(e)
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.org.com.au')
#No login required so this section is commented out
#server.login("youremailusername", "password")
server.sendmail(fromaddr, toaddr, str(msg))
print ("Script failed - email notification sent")
print ("Exception: " + str(e))
break # Stops script here