如何通过pyodbc备份数据库

时间:2013-11-19 08:53:29

标签: python database backup pyodbc

使用pyodbc游标执行时,不能在事务中使用备份语句。似乎pyodbc在默认事务中执行查询。 我还尝试使用自动提交模式或在备份语句之前添加commit语句。这两个都不起作用。

#can't execute the backup statement in transaction
cur.execute("backup database database_name to disk = 'backup_path'")
#not working too
cur.execute("commit;backup database database_name to disk = 'backup_path'")

是否可以通过pyodbc执行备份语句?提前谢谢!

-----添加了附加信息--------------------------------------- --------------------------------

备份操作封装在一个函数中,例如:

def backupdb(con, name, save_path):
    # with autocommit mode, should be pyodbc.connect(con, autocommit=True)
    con = pyodbc.connect(con) 
    query = "backup database %s to disk = '%s'" % (name, save_path)
    cur = con.cursor()
    cur.execute(query)
    cur.commit()
    con.close()

如果通过以下代码调用该函数,

backupdb('DRIVER={SQL Server};SERVER=.\sqlexpress;DATABASE=master;Trusted_Connection=yes',
             'DatabaseName',
             'd:\\DatabaseName.bak')

那么例外将是:

File "C:/Documents and Settings/Administrator/Desktop/bakdb.py", line 14, in <module>'d:\\DatabaseName.bak')
File "C:/Documents and Settings/Administrator/Desktop/bakdb.py", line 7, in backupdb cur.execute(query)
ProgrammingError: ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot perform a backup or restore operation within a transaction. (3021) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]BACKUP DATABASE is terminating abnormally. (3013)')

打开关键字autocommit = True后,该函数将以静默方式运行,但备份文件夹中没有生成备份文件。

2 个答案:

答案 0 :(得分:9)

假设您使用的是SQL Server,请在建立连接时指定autocommit=True

>>> import pyodbc
>>> connection = pyodbc.connect(driver='{SQL Server Native Client 11.0}', 
                                server='InstanceName', database='master', 
                                trusted_connection='yes', autocommit=True)
>>> backup = "BACKUP DATABASE [AdventureWorks] TO DISK = N'AdventureWorks.bak'"
>>> cursor = connection.cursor().execute(backup)
>>> connection.close()

这是pyodbc 3.0.7Python 3.3.2一起使用。我相信对于旧版本的pyodbc,您需要使用Cursor.nextset()来创建备份文件。例如:

>>> import pyodbc
>>> connection = pyodbc.connect(driver='{SQL Server Native Client 11.0}', 
                                server='InstanceName', database='master', 
                                trusted_connection='yes', autocommit=True)
>>> backup = "E:\AdventureWorks.bak"
>>> sql = "BACKUP DATABASE [AdventureWorks] TO DISK = N'{0}'".format(backup)
>>> cursor = connection.cursor().execute(sql)
>>> while cursor.nextset():
>>>    pass
>>> connection.close()

值得注意的是,我不必使用Cursor.nextset()来使用当前版本的pyodbc和SQL Server 2008 R2创建备份文件。

答案 1 :(得分:0)

几个小时后,我找到了解决方案。必须在没有 MASTER 的情况下执行,必须终止其他会话,必须将 DB 设置为 OFFLINE,然后 RESTORE,然后再次设置为 ONLINE。

def backup_and_restore():
server = 'localhost,1433'
database = 'myDB'
username = 'SA'
password = 'password'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE=MASTER;UID='+username+';PWD='+ password)
cnxn.autocommit = True

def execute(cmd):
    cursor = cnxn.cursor()
    cursor.execute(cmd)
    while cursor.nextset():
        pass
    cursor.close()

execute("BACKUP DATABASE [myDB] TO DISK = N'/usr/src/app/myDB.bak'")
# do something .......
execute("ALTER DATABASE [myDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;")
execute("ALTER DATABASE [myDB] SET OFFLINE;")
execute("RESTORE DATABASE [myDB] FROM DISK = N'/usr/src/app/myDB.bak' WITH REPLACE")
execute("ALTER DATABASE [myDB] SET ONLINE;")
execute("ALTER DATABASE [myDB] SET MULTI_USER;")