Python to backup SDE database on Sqlserver Express?

2474
2
09-09-2015 11:02 AM
DavidDoop
New Contributor II

Is it possible to use Python to do a backup of SDE databases stored in Sqlserver Express?  I have been using a custom executable that uses Arcobjects IDataServerManagerAdmin.BackupGeodatabase() but, I would prefer to use Python. The executable makes our IT staff a bit nervous.

thanks

0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor

Hi David,

Yes, this is possible.  Below is an example using the PYODBC module:

import pyodbc

conn = pyodbc.connect('DRIVER={SQL Server};SERVER=<server name>;UID=sa;PWD=sa;Trusted_Connection=yes', autocommit=True)
backup_path = 'C:\\temp\\'
db = "VECTOR"

def backup_db(conn, db, backup_path):
  cur = conn.cursor()
  try:
    cur.execute('BACKUP DATABASE ? TO DISK=?', [db, backup_path + db + r'.bak'])
    while cur.nextset(): 
      pass
    cur.close()
  except:
    print 'Unable to backup: ' + db

backup_db(conn, db, backup_path)

conn.close()
JoshuaBixby
MVP Esteemed Contributor

If you haven't already, it might be worth checking out the Overview of copying geodatabases documentation, it covers a few different ways of getting data in bulk out of geodatabases.  A note of caution, one of the links is incorrect and redirects you to another link.  See the ArcGIS 10.2.x Help for Copying a geodatabase using the Copy tool.