Code sample below
from datetime import datetime, timedelta, date
# *****Update the below 7 lines*****
name = "ArcOnline Related Table" #Name of dataset for use in error email notification
url_fl = "https://services3.arcgis.com/1GMgEwg21aGmaBQf/arcgis/rest/services/ArcOnlineRelatedTable/FeatureServer/1" # Service URL for feature layer (TABLE/RELATED TABLE ONLY) to download as feature class
destGDB = r"\\a\folder\on\our\server\Data.gdb" #The GDB where the backup feature class will be created
destFC = "ArcOnlineRelatedTable" #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
timeout = 60 # number of seconds delay to allow the export to complete
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 #Instead of signing in as follows (gis = GIS("https://org.maps.arcgis.com", "Username", "Password") this breaks it up so that you are able to call the sign in details independently
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
import getpass
import requests
from time import strftime
import datetime
### Access the stored password with keyring and sign into the GIS # https://community.esri.com/t5/arcgis-online-blog/connect-to-the-gis-in-python-scripts-without/ba-p/889867
import keyring
pw = keyring.get_password("ArcGISOnline", "my_username")
gis = GIS("https://org.maps.arcgis.com", "my_username", pw)
print("Connected to the GIS")
# CREATING BACKUP OF FEATURE SERVICE TABLE / RELATED TABLE
print ("Exporting backup of feature service")
Outputfs = destFC + "_" + strftime("%Y%m%d_%H%M%S")
arcpy.conversion.TableToTable(url_fl, destGDB, Outputfs)
time.sleep(timeout) #add time delay to allow export to complete
print (name + " feature service exported to backup GDB: " + destGDB + "\\" + Outputfs)
print ("Filtering past backups")
arcpy.env.workspace = destGDB
FClist = arcpy.ListFeatureClasses(destFC + "*")
for fc in FClist:
datestamp = datetime.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 = "me@org.wa.com.au"
toaddr = "me@org.wa.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 to me@org.com.au")
print ("Exception: " + str(e))
break # Stops script here
Lindsay Raabe
GIS Officer
Forest Products Commission WA