Waiting for DB restore to finish using sqlalchemy on SQL Server 2008

六月ゝ 毕业季﹏ 提交于 2020-08-23 07:33:38

问题


I'm trying to automate my db restores during development, using TSQL on SQL Server 2008, using sqlalchemy with pyodbc as a transport.

The command I'm executing is:

"""CREATE DATABASE dbname

restore database dbname FROM DISK='C:\Backups\dbname.bak' WITH REPLACE,MOVE 'dbname_data' TO 'C:\Databases\dbname_data.mdf',MOVE 'dbname_log' TO 'C:\Databases\dbname_log.ldf'"""

Unfortunately, the in SQL Management Studio, after the code has run, I see that the DB remains in state "Restoring...".

If I restore through management studio, it works. If I use subprocess to call "sqlcmd", it works. pymssql has problems with authentication and doesnt even get that far.

What might be going wrong?


回答1:


The BACKUP and RESTORE statements run asynchronously so they don't terminate before moving on to the rest of the code.

Using a while statement as described at http://ryepup.unwashedmeme.com/blog/2010/08/26/making-sql-server-backups-using-python-and-pyodbc/ solved this for me:

# setup your DB connection, cursor, etc
cur.execute('BACKUP DATABASE ? TO DISK=?', 
            ['test', r'd:\temp\test.bak'])
while cur.nextset():
    pass



回答2:


Unable to reproduce the problem restoring directly from pyodbc (without sqlalchemy) doing the following:

connection = pyodbc.connect(connection_string) # ensure autocommit is set to `True` in connection string
cursor = connection.cursor()
affected = cursor.execute("""CREATE DATABASE test
RESTORE DATABASE test FROM DISK = 'D:\\test.bak' WITH REPLACE, MOVE 'test_data' TO 'D:\\test_data.mdf', MOVE 'test_log' to 'D:\\test_log.ldf' """)
while cursor.nextset():
    pass

Some questions that need clarification:

  • What is the code in use to do the restore using sqlalchemy?
  • What version of the SQL Server ODBC driver is in use?
  • Are there any messages in the SQL Server log related to the restore?

Thanks to geographika for the Cursor.nextset() example!




回答3:


Five things fixed my problem with identical symptoms.

  1. Found that my test.bak file contained the wrong mdf and ldf files:

    >>> cursor.execute(r"RESTORE FILELISTONLY FROM DISK = 'test.bak'").fetchall()    
    [(u'WRONGNAME', u'C:\\Program Files\\Microsoft SQL ...),
    (u'WRONGNAME_log', u'C:\\Program Files\\Microsoft SQL ...)]
    
  2. Created a new bak file and made sure to set the copy-only backup option

  3. Set the autocommit option for my connection.

    connection = pyodbc.connect(connection_string, autocommit=True)
    
  4. Used the connection.cursor only for a single RESTORE command and nothing else

  5. Corrected the test_data MOVE to test in my RESTORE command (courtesy of @beargle).

    affected = cursor.execute("""RESTORE DATABASE test FROM DISK = 'test.bak' WITH REPLACE, MOVE 'test' TO 'C:\\test.mdf', MOVE 'test_log' to 'C:\\test_log.ldf' """)
    



回答4:


For SQL Alchemy users, and thanks to geographika for the answer: I ended up using the “raw” DBAPI connection from the connection pool.

It is exactly as geographika's solution but with a few additional pieces:

import sqlalchemy as sa
driver = 'SQL+Server'
name = 'servername'
sql_engine_str = 'mssql+pyodbc://'\
                     + name\
                     + '/'\
                     + 'master'\
                     + '?driver='\
                     + driver
engine = sa.create_engine(sql_engine_str, connect_args={'autocommit': True})

connection = engine.raw_connection()
try:
  cursor = connection.cursor()
  sql_cmd = """
        RESTORE DATABASE [test]
        FROM DISK = N'...\\test.bak'
        WITH FILE = 1,
         MOVE N'test'
         TO N'...\\test_Primary.mdf',
         MOVE N'test_log'
         TO N'...\\test_log.ldf',
         RECOVERY,
         NOUNLOAD,
         STATS = 5,
         REPLACE
        """
  cursor.execute(sql_cmd)
  while cursor.nextset():
       pass
except Exception as e:
  logger.error(str(e), exc_info=True)


来源:https://stackoverflow.com/questions/4254581/waiting-for-db-restore-to-finish-using-sqlalchemy-on-sql-server-2008

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!