SQL express back up with date

603
5
Jump to solution
06-22-2017 10:29 AM
CCWeedcontrol
Occasional Contributor III

I would like to back up a SQL express database but with the date of when the backup was created added to the end (Owls06222017.bak). Is this possible if so how?


I tried the following but did not work.

db, backup_path + db + time.strftime("%m%d%y")+ r'.bak'

import pyodbc, datetime, fnmatch 

conn = pyodbc.connect('DRIVER={SQL Server};SERVER=GIS7\SQLEXPRESS;Trusted_Connection=yes', autocommit=True)  
backup_path = 'C:\\temp\\'  
db = "Owls"  
  
def backup_db(conn, db, backup_path):  
  cur = conn.cursor()  
  try:  
    cur.execute('BACKUP DATABASE ? TO DISK=?', [db, backup_path + db + time.strftime("%m%d%y")+ r'.bak'])  
    while cur.nextset():   
      pass  
    cur.close()  
  except:  
    print 'Unable to backup: ' + db  
  
backup_db(conn, db, backup_path)  
  
conn.close()  
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

For debugging purposes, how about you remove the try:except block so you can see the actual error message.  Or, have your except clause print the error message and stack trace as well.

View solution in original post

5 Replies
JoshuaBixby
MVP Esteemed Contributor

How did it not work, exactly?  What were the errors or results you didn't expect?

0 Kudos
CCWeedcontrol
Occasional Contributor III

it returned

cant backup: Owls
Done

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

For debugging purposes, how about you remove the try:except block so you can see the actual error message.  Or, have your except clause print the error message and stack trace as well.

CCWeedcontrol
Occasional Contributor III

After adding the trace back it came back as not having arcpy module. Once i added the module it ran fine.

Would a better alternative be arcpy.ExportXMLWorkspaceDocument_management or be the same?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I don't administer geodatabases enough to have an informed opinion on your follow-up question.  That said, I think the caution note in Export XML Workspace Document—Help | ArcGIS Desktop strongly implies the answer isn't to use that tool for managing data itself:

The output XML workspace document can be very large when you copy both the data and the schema. Generally, this alternative is not recommended for copying geodatabase data. Alternatives for making a geodatabase copy include the use of the Copy tool or the Clip tool to extract subsets of the data. In addition, tools in the Package toolset provides tools to consolidate, package, and share layers, map documents, address locators, and geoprocessing results.

Since this question has already been answered, maybe ask another question in the https://community.esri.com/groups/geodatabase?sr=search&searchId=af6bec87-2501-47bf-91d5-63c244bc724...‌ or https://community.esri.com/community/gis/managing-data?sr=search&searchId=c73670d4-2ccf-4af2-9825-d2...‌ places.