Monday 24 April 2017

arcgis 10.1 - Backup several SDE database connections


This question stems from my previous question on how to list databases based on SDE. Now I want to copy them and have it written as a function so I can list out each database connection below it so it's easy to manage and update. Here's the code that lists the databases. I added the CopyFeature_Management, which may be different that what I want.


import arcpy

import os

arcpy.env.workspace = r"C:\Users\me\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\Survey.sde"
def listFcsInGDB():
''' set your arcpy.env.workspace to a gdb before calling '''
for fds in arcpy.ListDatasets('','feature') + ['']:
for fc in arcpy.ListFeatureClasses('','',fds):
yield os.path.join(arcpy.env.workspace, fds, fc)

#Set workspace environment to geodatabase

#arcpy.env.workspace = r"C:\Users\me\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\Survey.sde"

#Get list of feature classes in geodatabase
#FCs = arcpy.ListFeatureClasses()
FCs = listFcsInGDB()

#Loop through feature classes in list
for FC in FCs:

#Print the feature class name

print FC
print os.path.basename(FC)[0]
print os.path.splitext(FC)
output = "C:/Users/me/Desktop/"
arcpy.CopyFeatures_management(FC, output + os.path.basename(FC).split('.')[-1] + ".shp")

#os.path.basename(MXDPath).split('.')[0] + ".pdf" .split('.')[0] + ".shp"

Any advice how to get the workspace and output as different variables listed below the function? I want them outputted to different folders based off of their database name. It works right now, but only for this one database connection.





UPDATE 2


So now I have this working where it creates a GDB based on the SDE connection name. It will copy the contents and put them in the GDB. The only problem I get is it always leaves one FC out of the file geodatabase. if there are 19 features in survey it only copies 18 to the file GDB


import arcpy
import os

#Set workspace environment to the GDB locations.
arcpy.env.workspace = "C:/Users/me/AppData/Roaming/ESRI/Desktop10.1/ArcCatalog/"

#get every SDE connection we have. We can narrow it down by replacing the wildcard "*" with something else.
workspaces = arcpy.ListWorkspaces("*", "SDE")


#workaround to get feature classes within and outside feature datasets
def listFcsInGDB():
''' set your arcpy.env.workspace to a gdb before calling '''
for fds in arcpy.ListDatasets('','feature') + ['']:
for fc in arcpy.ListFeatureClasses('','',fds):
yield os.path.join(arcpy.env.workspace, fds, fc)


for workspace in workspaces:

arcpy.env.overwriteOutput = True
#list the current workspace we are looping through
arcpy.AddMessage(workspace)
#set the current environment to the SDE or GDB instance we want to loop through
arcpy.env.workspace = workspace
FCs = listFcsInGDB()
#Loop through feature classes in SDE or GDB
for GDB in FCs:
output = arcpy.CreateFileGDB_management("C:/Users/me/Desktop/", os.path.basename(GDB)[:14] + time.strftime('_%y_%m_%d_%H%M'))


for FC in FCs:
arcpy.env.overwriteOutput = True
##output = arcpy.CreateFileGDB_management("C:/Users/me/Desktop/", os.path.basename(FC)[:14])
#output = "C:/Users/me/Desktop/" + os.path.basename(FC)[:14]+"/"
#os.makedirs(output)

arcpy.FeatureClassToGeodatabase_conversion(FC, output)
print str(FC) + " Moved to " + str(output)

Any ideas why its moving them all but one from each? One of the GDBs has only one fc in it. It creates the GDB, but the database is actually empty.





UPDATE 3


import arcpy
import os
timestr = time.strftime("%Y_%m_%d_%H:%M")

#Set workspace environment to the GDB locations.
arcpy.env.workspace = "C:/Users/me/AppData/Roaming/ESRI/Desktop10.1/ArcCatalog/"

#get every SDE connection we have. We can narrow it down by replacing the wildcard "*" with something else.

workspaces = arcpy.ListWorkspaces("*", "SDE")

#workaround to get feature classes within and outside feature datasets
def listFcsInGDB():
''' set your arcpy.env.workspace to a gdb before calling '''
for fds in arcpy.ListDatasets('','feature') + ['']:
for fc in arcpy.ListFeatureClasses('','',fds):
yield os.path.join(arcpy.env.workspace, fds, fc)



for workspace in workspaces:
#arcpy.env.overwriteOutput = True
#list the current workspace we are looping through
arcpy.AddMessage(workspace)
#set the current environment to the SDE or GDB instance we want to loop through
arcpy.env.workspace = workspace
FCs = listFcsInGDB()
#Loop through feature classes in SDE or GDB

output = arcpy.CreateFileGDB_management("C:/Users/me/Desktop/", os.path.basename(workspace)[:14] + time.strftime('_%y_%m_%d_%H%M'))


for FC in FCs:
arcpy.env.overwriteOutput = True
##output = arcpy.CreateFileGDB_management("C:/Users/me/Desktop/", os.path.basename(FC)[:14])
#output = "C:/Users/me/Desktop/" + os.path.basename(FC)[:14]+"/"
#os.makedirs(output)

arcpy.FeatureClassToGeodatabase_conversion(FC, output)
print str(FC) + " Backed up to: " + str(output)


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...