Copy enterprise geodatabase to file geodatabase

1280
1
08-03-2022 01:33 PM
DirtDogRoj
New Contributor III

Two other users and I developed a simple script to properly copy an enterprise geodatabase to a file geodatabase.  The enterprise geodatabase in question is on SQL Server and contains stand-alone feature classes, feature datasets, stand-alone tables, relationships, and attachments.  Of note, it does not contain data types like annotations, representations, dimensions, or rasters.  The script was written in a Python Notebook in ArcGIS Pro version 2.9.3.

# DateTime is necessary to create the name of our backup folder based on the
# current date
import datetime
# Os is necessary to properly concatenate file paths to ArcGIS resources
import os
# ArcPy is necessary to examine, create, and copy ArcGIS resources
import arcpy
# The name of your geodatabase connection.  This could be a full path and should
# end in .sde.  If no path is specified, ArcGIS Pro will assume the connection file
# is in the same directory as this script.
sdeConnection = "username@database.sde"
# The base path where the backup will reside
backupDestination = r"Drive:\Parent\Child"
# Will contain a string like 20220803.gdb for August 3, 2022
backupGdbName = datetime.datetime.today().strftime("%Y%m%d.gdb")
# Create an empty file geodatabase
arcpy.CreateFileGDB_management(backupDestination, backupGdbName)
# Contains the full path to the geodatabase just created
backupGdbPath = os.path.join(backupDestination, backupGdbName)
# Get into the geodatabase connection file and list all datasets and stand-alone
# tables found.  Only the first level is considered and only the first level is
# needed, therefore, the for loop ends in a break
for root, datasets, tables in arcpy.da.Walk(sdeConnection, followlinks=True):
    for dataset in datasets:
        sourceDatasetPath = os.path.join(root, dataset)
        # Get the base name of the dataset (the last string after the last period)
        backupDatasetPath = os.path.join(backupGdbPath, dataset.split(".")[-1])
        # Copy the dataset, plus everything in it, including feature classes,
        # relationships, attachments.  Feature datasets can't contain other
        # datasets, so, again, no need to da.Walk another level in.
        arcpy.Copy_management(sourceDatasetPath, backupDatasetPath)
    for table in tables:
        # Ignore attachment tables, taken care of in the previous Copy_management
        if not table.endswith("__ATTACH"):
            sourceTableName = os.path.join(root, table)
            itemDesc = arcpy.Describe(sourceTableName)
            if itemDesc.dataType in ("Table", "FeatureClass"):
                # Get the base name of the table (the last string after 
                # the last period)
                backupTableName = os.path.join(backupGdbPath, table.split(".")[-1])
                # Copy the stand-alone table or feature class to backup
                arcpy.Copy_management(sourceTableName, backupTableName)
    break

 

If this code helps you, please leave a kudos

1 Reply
Peter_Green
New Contributor III

This is a handy bit of script to have. Just what I was looking for. Thank you.

0 Kudos