0
votes

I have a simple Perl/DBI program to backup SQL Server databases. The program runs cleanly, produces no errors, supposedly creates database backups, but the backup files do not exist!

When I execute the following command via DBI,

backup database dz to disk='C:\SqlBackups\dz.perl' with init

No backup file is created. Perl and SQL report "Processed x pages for database 'y',
but the backup file does not appear when I execute DOS> dir

When I execute a similiar command via SqlCmd,

backup database dz to disk='C:\SqlBackups\dz.sqlcmd' with init

A backup file is created.

I'm running ActiveState Perl version This is perl 5, version 16, subversion 1 (v5.16.1) built for MSWin32-x64-multi-thread on Microsoft Windows 7 Professional Build 6.1 (Build 7601; Service Pack 1)

Here's the program

#!perl -w
#Test making database backups via Perl DBI 


#preparations
    use strict;  #require variable declaration 
    use DBI;      #database interface
    my $dbHandle = DBI->connect("dbi:ODBC:Driver={SQL Server};Server=DavidZokaites")  or die;
    $dbHandle->{TraceLevel} = "15|ALL";    #get maximum debugging information 


#loop over all databases that need a dump 
    foreach my $dbName (  qw < dz master model msdb my_test > ) 
    {
        #DBI method that runs but creates no dumps  
        my $query = "backup database $dbName to disk='C:\\SqlBackups\\$dbName.perl' with init";
        print $query, "\n"; 
        warn "\n$dbName\n";
        $dbHandle->do ($query)  or die; 

        #SQLCMD that works 
        $query = "backup database $dbName to disk='C:\\SqlBackups\\$dbName.sqlcmd' with init";
        print $query, "\n"; 
        my $sysCmd = "SqlCmd -S DavidZokaites -Q \"$query\" >> TestDbi.sqlcmd.out";
        system ($sysCmd) == 0
            or die "Unable to execute $sysCmd \n"; 
    } 

And here's a bit of the DBI stderr tracing

    DBI::db=HASH(0x297ba10) trace level set to 0x7f0fff00/15 (DBI @ 0x0/0) in DBI 1.622-ithread (pid 7712)

master
    !! info: '' CLEARED by call to do method
    -> do for DBD::ODBC::db (DBI::db=HASH(0x297bae8)~0x297ba10 'backup database master to disk='C:\SqlBackups\master.perl' with init') thr#1d97e8
    >> STORE         DISPATCH (DBI::db=HASH(0x297ba10) rc1/2 @3 g2 ima41c pid#7712) at C:/Perl64/site/lib/DBD/ODBC.pm line 424 via  at C:\David\DoBackup\TestDbi.pl line 20
1    -> STORE for DBD::ODBC::db (DBI::db=HASH(0x297ba10)~INNER 'Statement' 'backup database master to disk='C:\SqlBackups\master.perl' with init') thr#1d97e8
    !!DBD::ODBC unsupported attribute passed (Statement)
    STORE DBI::db=HASH(0x297ba10) 'Statement' => 'backup database master to disk='C:\SqlBackups\master.perl' with init'
1    <- STORE= ( 1 ) [1 items] at C:/Perl64/site/lib/DBD/ODBC.pm line 424 via  at C:\David\DoBackup\TestDbi.pl line 20
    SQLExecDirect backup database master to disk='C:\SqlBackups\master.perl' with init
    Processing non utf8 sql in unicode mode
    SQLExecDirect = 1
    !!dbd_error2(err_rc=1, what=Execute immediate success with info, handles=(26bd70,2699f00,29cd080)
    !SQLError(26bd70,2699f00,29cd080) = (01000, 4035, [Microsoft][ODBC SQL Server Driver][SQL Server]Processed 1376 pages for database 'master', file 'master' on file 1.)
    !! info: '' '[Microsoft][ODBC SQL Server Driver][SQL Server]Processed 1376 pages for database 'master', file 'master' on file 1. (SQL-01000)' (err#0)
    <- do= ( -1 ) [1 items] at C:\David\DoBackup\TestDbi.pl line 20
1

1 Answers

0
votes

You need to read Why does my backup/restore/some_other_procedure in MS SQL Server not complete?. Any command/procedure which can output print messages when your using the "do" method does not complete. Use prepare/execute and odbc_more_results.