question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SQL Server BACKUP DATABASE statement fails silently

See original GitHub issue

(Prompted by investigation into this Stack Overflow question.)

Python 3.6.4 64-bit, pyodbc 4.0.24, Windows 7

The following Python/pyodbc code fails silently. No error is thrown, but no backup file is produced either.

import pyodbc


def get_conn_str():
    conn_str = (
        r'DRIVER=ODBC Driver 17 for SQL Server;'
        r'SERVER=.\SQLEXPRESS;'
        r'DATABASE=myDb;'
        r'Trusted_Connection=yes;'
    )
    return conn_str


pyodbc.pooling = False

cnxn = pyodbc.connect(get_conn_str(), autocommit=True)
crsr = cnxn.cursor()

crsr.execute(
        r"BACKUP DATABASE [myDb] TO  DISK = N'C:\__tmp\mybackup.bak' WITH NOFORMAT, NOINIT,  NAME = N'mybackup.bak', SKIP, NOREWIND, NOUNLOAD,  STATS = 10"
    )

crsr.close()
cnxn.close()

ODBC log: pyodbc_no_va.txt

However, the following VBScript does create the .bak file.

Option Explicit
Dim conn
Set conn = CreateObject("ADODB.Connection")
Dim connStr
connStr = _
        "DRIVER=ODBC Driver 17 for SQL Server;" & _
        "SERVER=.\SQLEXPRESS;" & _
        "DATABASE=myDb;" & _
        "Trusted_Connection=yes;"
conn.Open connStr

conn.Execute("BACKUP DATABASE [myDb] TO  DISK = N'C:\__tmp\mybackup.bak' WITH NOFORMAT, NOINIT,  NAME = N'mybackup.bak', SKIP, NOREWIND, NOUNLOAD,  STATS = 10")

conn.Close

ODBC log: adodb_ok.txt

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
v-makouzcommented, Oct 10, 2018

After looking into it, I see that when requesting the DB backup the server sends multiple results containing messages about what percentage of the backup is complete, so if the client application doesn’t wait for all of them and closes the connection after the first one, the backup is aborted. The client application will not see the error sent by the server, because it already closed the connection and is no longer listening for it.

The easiest way to fix it is to add

while (crsr.nextset()):
    pass

right after the execute command, internally this will keep calling SQLMoreResults until the server is done with the backup

1reaction
v-makouzcommented, Oct 11, 2018

In TDS there is an indication that the result set is not final. As I understand in ODBC the way to check is to call SQLMoreResults or SQLFetchScroll which would return SQL_NO_DATA if there are no more results.

But for the progress reporting when backing up the DB, the messages come as DiagRecs, so they don’t have result sets per se, so one can’t SQLFetch them that’s why for this specific case I believe only SQLMoreResults is useful

Read more comments on GitHub >

github_iconTop Results From Across the Web

Non-component VSS backups fail on servers - SQL Server
This problem occurs because SQL Server SQLWriter currently doesn't handle AUTO-CLOSE databases correctly in non-component mode VSS backup ...
Read more >
mysqldump Silently Fails to Backup One Table
I'm using an application that uses MySQL Server 5.7.16.0 on a Windows Server 2012 R2 server. ... I use mysqldump to backup this...
Read more >
Backup of SQL instance fails with "There are no SQL writers ...
Open MS SQL Management Studio, right-click on any database and select Run query · Run the following query: select '#' + name +'#'...
Read more >
SQL-Server: The backup set holds a backup of a database ...
1) Use WITH REPLACE while using the RESTORE command (if using the GUI, it is found under Options -> Overwrite the existing database...
Read more >
Silent Data truncation in SQL Server 2019 - SQLShack
it will raise an SQL truncate error. Insert statement will fail in this case. We normally call it as silent truncation and occur...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found