AnsweredAssumed Answered

SQL express back up with date

Question asked by CCweedcontrol on Jun 22, 2017
Latest reply on Jun 22, 2017 by bixb0012

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() 

Outcomes