SQL server Full backup job is skipping databases

2019-09-05 23:01发布

问题:

I have a monthly job running on my SQL server.

  • It backups all the online databases to a location "\Backupserver\e$\MonthEndBackups\".

  • Job runs the USP below like this: exec [usp_dba_BackupDatabases] 3 (http://screencast.com/t/l7IS5TZK)

  • It runs on the last day of the month.

Our databases were scattered around multiple servers and this job runs on those servers and this used to work.

We consolidated all the database on the same server (same SQL instance) and this job does not seem to backup all the databases. I am not sure why? My job has notification(email) on failure and also writes to a log file.

http://screencast.com/t/8ioTZdqEMg9x http://screencast.com/t/VI3d4GLBTGoX

But nothing fails, so nothing is on the logs and no email notifications show up.

I know it did not work as I don't see the full backups in the folder.

here is the schedule setup:

http://screencast.com/t/waeGwLSa

What could be going wrong? I don't see any pattern in the databases that are not backing up. There are larger databases that are getting backed up.

Can anyone think of why this could be happening? is there any way to trouble shoot this?

USE [DBA]
GO

/****** Object:  StoredProcedure [dbo].[usp_dba_BackupDatabases]    Script Date: 10/01/2013 11:10:26 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[usp_dba_BackupDatabases] 
    @pBackupType        SMALLINT
    ,@pDatabaseName     sysname = NULL

AS



SET NOCOUNT ON


DECLARE @vDatabase sysname, @sql VARCHAR(MAX), @vBackupFileFullPath VARCHAR(MAX)

DECLARE c CURSOR FOR 
SELECT  name FROM sys.sysdatabases
WHERE name NOT IN('tempdb', 'model')
    AND DATABASEPROPERTYEX (name,'STATUS') IN( 'ONLINE')
    AND (name  = @pDatabaseName OR @pDatabaseName IS NULL)

OPEN c
FETCH NEXT FROM c INTO @vDatabase
WHILE @@FETCH_STATUS = 0 
BEGIN

SELECT CONVERT(VARCHAR,GETDATE(),121)
    PRINT '|-->' + CONVERT(VARCHAR,GETDATE(),121) +' Backup Start for database ' + @vDatabase 

    IF @pBackupType = 1
        BEGIN
            SET @vBackupFileFullPath = '\\Backupserver\d$\' + @@SERVERNAME +'\' + @vDatabase +'_DB_'
                +REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(16),GETDATE(),112),'-',''),' ',''),':','') +'.bak'
            SET @sql  = 'BACKUP DATABASE ' + @vDatabase + ' TO  DISK = N' + CHAR(39) + @vBackupFileFullPath +  char(39) 
            + ' WITH FORMAT, INIT,  NAME = N' + CHAR(39) + @vDatabase + ' -Full Database Backup' + CHAR(39) + ', SKIP, NOREWIND, NOUNLOAD,  STATS = 5, CHECKSUM'
        END


    IF @pBackupType = 2
        BEGIN
            SET @vBackupFileFullPath = '\\Backupserver\d$\' + @@SERVERNAME + '\Differential\' + @vDatabase +'_Diff_'
                 +REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(16),GETDATE(),121),'-',''),' ',''),':','') +'.bak'        
            SET @sql  = 'BACKUP DATABASE ' + @vDatabase + ' TO  DISK = N' + CHAR(39) +  @vBackupFileFullPath + char(39)
                 + ' WITH DIFFERENTIAL , FORMAT, INIT, NAME = N' + CHAR(39) + @vDatabase 
                                + ' -Differential Database Backup' + CHAR(39) + ', SKIP, NOREWIND, NOUNLOAD,  STATS = 5, CHECKSUM'              
        END

    IF @pBackupType = 3
        BEGIN
            SET @vBackupFileFullPath = '\\Backupserver\e$\MonthEndBackups\' + @@SERVERNAME +'_'+ @vDatabase +'_db.bak'
            SET @sql  = 'BACKUP DATABASE ' + @vDatabase + ' TO  DISK = N' + CHAR(39) + @vBackupFileFullPath  + char(39) 
                + ' WITH COPY_ONLY, FORMAT, INIT,  NAME = N' + CHAR(39) + @vDatabase + ' -Full Month End Database Backup' + CHAR(39) + ', SKIP, NOREWIND, NOUNLOAD,  STATS = 5, CHECKSUM'           
        END

    PRINT ' |'+ @sql  
    EXEC (@sql )


    --VERIFY BACKUP

    IF @pBackupType = 1
        SET @sql  = 'RESTORE VERIFYONLY FROM DISK = N' + CHAR(39) + @vBackupFileFullPath +  char(39) 
                    + ' WITH  FILE = 1,  NOUNLOAD,  NOREWIND'

    IF @pBackupType = 2
        SET @sql  = 'RESTORE VERIFYONLY FROM DISK = N' + CHAR(39) + @vBackupFileFullPath + char(39)
                 + ' WITH  FILE = 1,  NOUNLOAD,  NOREWIND'

    IF @pBackupType = 3
        SET @sql  = 'RESTORE VERIFYONLY FROM DISK = N' + CHAR(39) + @vBackupFileFullPath + char(39) 
                    + ' WITH  FILE = 1,  NOUNLOAD,  NOREWIND'

    --PRINT '   |'+ @sql  
    --EXEC (@sql )



    PRINT '|-->' + CONVERT(VARCHAR,GETDATE(),121) +' Backup Complete for database ' + @vDatabase 
    PRINT '' 

    FETCH NEXT FROM c INTO @vDatabase
END

CLOSE c
DEALLOCATE c



GO

回答1:

Try with the following options (fixing the options that are likely breaking your current loop and also break sp_MSforeachdb, as I've documented here and here, and corrected here):

DECLARE c CURSOR LOCAL FAST_FORWARD FOR
-----------------^^^^^^^^^^^^^^^^^^

...

WHILE @@FETCH_STATUS <> -1
---------------------^^^^^

Also, please, please, please stop declaring VARCHAR without length.