sde的备份与还原本可以通过ArcGIS的导出和导入XML工作空间文档来实现。但是由于数据质量和ArcGIS稳定性问题,在SDE数据库导出到XML过程中,会发生ArcGIS的崩溃,所以特写了以下两个脚本。
1.SDE的备份。通过将SDE数据库导出为GDB。
代码如下:
# -*- coding: utf-8 -*- import arcpy from arcpy import env try: arcpy.CreateFileGDB_management("D:/","SDEPrepare.gdb","CURRENT") if isinstance("D:/SDEPrepare.gdb<GDB>Create Succeed",unicode): print ("D:/SDEPrepare.gdb<GDB>Create Succeed").encode(‘gb2312‘) else: print ("D:/SDEPrepare.gdb<GDB>Create Succeed").decode(‘utf-8‘).encode(‘gb2312‘) except Exception as inst: if isinstance("D:/SDEPrepare.gdb<GDB>Create Failed",unicode): print ("D:/SDEPrepare.gdb<GDB>Create Failed").encode(‘gb2312‘) else: print ("D:/SDEPrepare.gdb<GDB>Create Failed").decode(‘utf-8‘).encode(‘gb2312‘) env.workspace="数据库连接/Connection to tjch3650m31.sde" fcs=arcpy.ListFeatureClasses("*") for fc in fcs: try: arcpy.FeatureClassToGeodatabase_conversion(fc,"D:/SDEPrepare.gdb") if isinstance(fc+"<FeatureClass>Import Succeed",unicode): print (fc+"<FeatureClass>Import Succeed").encode(‘gb2312‘) else: print (fc+"<FeatureClass>Import Succeed").decode(‘utf-8‘).encode(‘gb2312‘) except Exception as inst: if isinstance(fc+"<FeatureClass>Import Failed",unicode): print (fc+"<FeatureClass>Import Failed").encode(‘gb2312‘) else: print (fc+"<FeatureClass>Import Failed").decode(‘utf-8‘).encode(‘gb2312‘) print "游离的要素类导入成功" datasetList= arcpy.ListDatasets("*", "All") for dataset in datasetList: try: arcpy.CreateFeatureDataset_management("D:/SDEPrepare.gdb",dataset.split(‘.‘)[1]) if isinstance(dataset+"<Dataset>Create Succeed",unicode): print (dataset+"<Dataset>Create Succeed").encode(‘gb2312‘) else: print (dataset+"<Dataset>Create Succeed").decode(‘utf-8‘).encode(‘gb2312‘) env.workspace=u"数据库连接/Connection to wl05031.sde"+"//"+dataset dsfcs=arcpy.ListFeatureClasses("*") for dsfc in dsfcs: try: arcpy.FeatureClassToGeodatabase_conversion(dsfc,"D:/SDEPrepare.gdb"+"/"+dataset.split(‘.‘)[1]) if isinstance(dsfc+"<FeatureClass>Import Succeed",unicode): print (dsfc+"<FeatureClass>Import Succeed").encode(‘gb2312‘) else: print (dsfc+"<FeatureClass>Import Succeed").decode(‘utf-8‘).encode(‘gb2312‘) except Exception as inst: if isinstance(dsfc+"<FeatureClass>Import Failed",unicode): print (dsfc+"<FeatureClass>Import Failed").encode(‘gb2312‘) else: print (dsfc+"<FeatureClass>Import Failed").decode(‘utf-8‘).encode(‘gb2312‘) if isinstance(dataset+"<Dataset>Import Succeed",unicode): print (dataset+"<Dataset>Import Succeed").encode(‘gb2312‘) else: print (dataset+"<Dataset>Import Succeed").decode(‘utf-8‘).encode(‘gb2312‘) except Exception as inst: if isinstance(dataset+"<Dataset>Create Failed",unicode): print (dataset+"<Dataset>Create Failed").encode(‘gb2312‘) else: print (dataset+"<Dataset>Create Failed").decode(‘utf-8‘).encode(‘gb2312‘) print "Complete SDE DB Prepared in D:/DBPrepare.gdb. Please Notice the Message"
图片如下:
2.SDE还原。通过将GDB导入到SDE。
代码如下:
# -*- coding: utf-8 -*- import os.path import arcpy from arcpy import env inputGDB = "D:/SDEPrepare.gdb" if(os.path.exists(inputGDB)!=True): print inputGDB+" Not Found" exit sdepath=u"数据库连接/Connection to wl05031.sde" env.workspace="D:/SDEPrepare.gdb" fcs=arcpy.ListFeatureClasses("*") for fc in fcs: try: #arcpy.FeatureClassToGeodatabase_conversion(fc,sdepath) if isinstance(fc+"<FeatureClass>Import Succeed",unicode): print (fc+"<FeatureClass>Import Succeed").encode(‘gb2312‘) else: print (fc+"<FeatureClass>Import Succeed").decode(‘utf-8‘).encode(‘gb2312‘) except Exception as inst: if isinstance(fc+"<FeatureClass>Import Failed",unicode): print (fc+"<FeatureClass>Import Failed").encode(‘gb2312‘) else: print (fc+"<FeatureClass>Import Failed").decode(‘utf-8‘).encode(‘gb2312‘) print "游离的要素类导入成功" datasetList= arcpy.ListDatasets("*", "All") for dataset in datasetList: try: arcpy.CreateFeatureDataset_management(sdepath,dataset) if isinstance(dataset+"<Dataset>Create Succeed",unicode): print (dataset+"<Dataset>Create Succeed").encode(‘gb2312‘) else: print (dataset+"<Dataset>Create Succeed").decode(‘utf-8‘).encode(‘gb2312‘) env.workspace=u"D:/SDEPrepare.gdb"+"//"+dataset dsfcs=arcpy.ListFeatureClasses("*") for dsfc in dsfcs: try: arcpy.FeatureClassToGeodatabase_conversion(dsfc,sdepath+"/"+dataset) if isinstance(dsfc+"<FeatureClass>Import Succeed",unicode): print (dsfc+"<FeatureClass>Import Succeed").encode(‘gb2312‘) else: print (dsfc+"<FeatureClass>Import Succeed").decode(‘utf-8‘).encode(‘gb2312‘) except Exception as inst: if isinstance(dsfc+"<FeatureClass>Import Failed",unicode): print (dsfc+"<FeatureClass>Import Failed").encode(‘gb2312‘) else: print (dsfc+"<FeatureClass>Import Failed").decode(‘utf-8‘).encode(‘gb2312‘) if isinstance(dataset+"<Dataset>Import Succeed",unicode): print (dataset+"<Dataset>Import Succeed").encode(‘gb2312‘) else: print (dataset+"<Dataset>Import Succeed").decode(‘utf-8‘).encode(‘gb2312‘) except Exception as inst: if isinstance(dataset+"<Dataset>Create Failed",unicode): print (dataset+"<Dataset>Create Failed").encode(‘gb2312‘) else: print (dataset+"<Dataset>Create Failed").decode(‘utf-8‘).encode(‘gb2312‘) print "Complete SDE DB recovered in "+sdepath+" .Please Notice the Message"
图片如下: