Enterprise Geodatabase Maintenance

I have several enterprise level geodatabases (GDB) to manage. Instead of setting up maintenance scripts for each project GDB, I decided to write one script to manage them all. (Get the movie reference?)

This script requires two directories to be setup 1) OwnerFileDir and 2) AdminFileDir. The first will store several SDE owner connection files; one per database. Owner is usually "dbo" or "sde". The second will store several Esri object owner connection files; one per database. The Esri object owner is the user that owns each object's schema e.g. <DatabaseName>.<Schema>.<PolygonFeatureClassName>.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#------------------------------------------------------------------------------------------------
# Name: Enterprise GDB Maintenance
# Purpose: This script will compress a list of enterprise GDBs, update statistics, and  rebuild table indexes.
# Author:   Nicole Ceranek 
# Date Created: 12/28/2015
# Last Modified:    01/05/2016
# ArcGIS Version    10.1
# Python Version:   2.7
#------------------------------------------------------------------------------------------------
## Import modules
import arceditor
import os, arcpy, datetime, sys,time, os.path, logging, fnmatch, traceback
from arcpy import env
from datetime import date, timedelta, datetime
from time import ctime
from os import listdir
from os.path import isfile, join

## Set global variables
datetime120 = time.strftime("%Y%m%d_%H%M%S")
time108 = time.strftime("%H:%M:%S")
arcpy.env.overwriteOutput = True
# Owner directory
OwnerFileDir = "<enter your file path here>"
AdminFileDir = "<enter your file path here>"
# Admin directory
LogFile = open("<enter your file path here>/EGDBmaintenance_"+datetime120+".csv","w")
## Begin Script
ownerDB = os.listdir(OwnerFileDir)
for DB in ownerDB:
    try:
        arcpy.env.workspace = OwnerFileDir
        arcpy.AddMessage("Disconnecting users from "+DB+"\n")
        LogFile.write("Disconnecting users from,"+DB+"\n")
        arcpy.AcceptConnections(DB, False)
        arcpy.DisconnectUser(DB, "ALL")
        arcpy.AddMessage("Compressing "+DB+"\n")
        LogFile.write("Compressing,"+DB+"\n")
        arcpy.Compress_management(DB)
        arcpy.AddMessage("Allowing connections to "+DB+"\n")
        LogFile.write("Allowing connections to,"+DB+"\n")
        arcpy.AcceptConnections(DB, True)
    except arcpy.ExecuteError:
        msgs = arcpy.GetMessages(2)
        arcpy.AddError(msgs)
        arcpy.AddMessage(msgs)
        LogFile.write(msgs)
        ErrorCount=ErrorCount+1
    except PythonError:
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
        msgs = arcpy.GetMessages(2) + "\n"
        arcpy.AddError(pymsg)
        arcpy.AddError(msgs)
        arcpy.AddMessage(pymsg)
        LogFile.write(pymsg)
        arcpy.AddMessage(msgs)
        LogFile.write(msgs)
        ErrorCount=ErrorCount+1
    except:
        if (arcpy.DisconnectUser == "true"):
            arcpy.AcceptConnections(DB, True)
    finally:
        pass
# Rebuild indexes and analyze the states and states_lineages system tables for each EGDB connection file found in the admin directory
adminDB = os.listdir(AdminFileDir)
for DB in adminDB:
    try:
        arcpy.env.workspace = AdminFileDir+DB
        userName = arcpy.Describe(arcpy.env.workspace).connectionProperties.user
        oDataList = arcpy.ListTables('*.' + userName + '.*') + arcpy.ListFeatureClasses('*.' + userName + '.*') + arcpy.ListRasters('*.' + userName + '.*')
        for dataset in arcpy.ListDatasets('*.' + userName + '.*'):
            oDataList += arcpy.ListFeatureClasses(feature_dataset=dataset)
        LogFile.write("Tables owned by "+userName+":,"+str(oDataList)+",\n")
        arcpy.AddMessage("Rebuilding indexes for "+DB+"\n")
        LogFile.write("Rebuilding indexes for,"+DB+"\n")
        arcpy.RebuildIndexes_management(arcpy.env.workspace, "NO_SYSTEM", oDataList, "ALL")
        arcpy.AddMessage("Analyzing data for "+DB+"\n")
        LogFile.write("Analyzing data for,"+DB+"\n")
        arcpy.AnalyzeDatasets_management(arcpy.env.workspace, "NO_SYSTEM", oDataList, "ANALYZE_BASE", "ANALYZE_DELTA", "ANALYZE_ARCHIVE")
    except arcpy.ExecuteError:
        msgs = arcpy.GetMessages(2)
        arcpy.AddError(msgs)
        arcpy.AddMessage(msgs)
        LogFile.write(msgs)
        ErrorCount=ErrorCount+1
    except:
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
        msgs = arcpy.GetMessages(2) + "\n"
        arcpy.AddError(pymsg)
        arcpy.AddError(msgs)
        arcpy.AddMessage(pymsg)
        LogFile.write(pymsg)
        arcpy.AddMessage(msgs)
        LogFile.write(msgs)
        ErrorCount=ErrorCount+1
    finally:
        pass

Comments

  1. F!@#ing awesome! didn't know you were keeping up with this. You should've added a free image referencing lord of the rings with your statement near the beginning "...one script to manage them all". Me likey.

    ReplyDelete
    Replies
    1. I most certainly missed that opportunity! Glad you liked the post though. Thank you!

      Delete

Post a Comment

Popular posts from this blog

Updating GDB_Items XML Definitions

Animation with ArcGIS Pro

Add Field to a SQL Geodatabase using T-SQL