Solved

script for check backup SQL SERVER

Posted on 2011-09-15
9
323 Views
Last Modified: 2012-05-12
Hello,

I search a script for verify and backup MSSQL which db are not dump.

Thanks

bibi
0
Comment
Question by:bibi92
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 17

Expert Comment

by:dbaSQL
ID: 36542473
I'm not sure if I follow exaclty what you're wanting, but take a look at this:

/* Backups;  are they being done? */
SELECT d.name, MAX(b.backup_finish_date) AS last_backup_finish_date
FROM master.sys.databases d
LEFT OUTER JOIN msdb.dbo.backupset b ON d.name = b.database_name AND b.type = 'D'
WHERE d.database_id NOT IN (2, 3)  -- Bonus points if you know what that means
GROUP BY d.name
ORDER BY d.name

/*  Backups; Where are they written to? */
SELECT TOP 100 physical_device_name, * FROM msdb.dbo.backupmediafamily ORDER BY media_set_id DESC
SELECT * FROM msdb..backupmediafamily where physical_device_name like '%msdb%'
0
 
LVL 10

Expert Comment

by:gaurav05
ID: 36542490
Hi,,

SET NOCOUNT ON
DECLARE @FileName VARCHAR(255)
DECLARE @File_Exists INT
DECLARE @DBname sysname

--get list of files to check
DECLARE FileNameCsr CURSOR
READ_ONLY
FOR  
   SELECT physical_device_name, sd.name
   FROM msdb..backupmediafamily bmf
   INNER JOIN msdb..backupset bms ON bmf.media_set_id = bms.media_set_id
   INNER JOIN master..sysdatabases sd ON bms.database_name = sd.name
   AND bms.backup_start_date = (SELECT MAX(backup_start_date) FROM [msdb]..[backupset] b2
                                   WHERE bms.database_name = b2.database_name AND b2.type = 'D')
   WHERE sd.name NOT IN ('Pubs','tempdb','Northwind', 'Adventureworks')

BEGIN TRY
   OPEN FileNameCsr

   FETCH NEXT FROM FileNameCsr INTO @FileName, @DBname
   WHILE (@@fetch_status <> -1)
   BEGIN
       IF (@@fetch_status <> -2)
       BEGIN
           EXEC Master.dbo.xp_fileexist @FileName, @File_Exists OUT
       
           --if the file is not found, print out a message
           IF @File_Exists = 0 --0 means file is not found, 1 means it is found
               PRINT 'File Not Found: ' + @FileName + ' -- for database: ' + @DBName
       END
   
   FETCH NEXT FROM FileNameCsr INTO @FileName, @DBName
   END
   
END TRY

BEGIN CATCH
    SELECT
        ERROR_NUMBER() AS ErrorNumber
        ,ERROR_SEVERITY() AS ErrorSeverity
        ,ERROR_STATE() AS ErrorState
        ,ERROR_PROCEDURE() AS ErrorProcedure
        ,ERROR_LINE() AS ErrorLine
        ,ERROR_MESSAGE() AS ErrorMessage;
END CATCH


CLOSE FileNameCsr
DEALLOCATE FileNameCsr
GO
0
 

Author Comment

by:bibi92
ID: 36542500
yes, but how can I start a backup if the last_backup_finish_date is > 24 hours.
Thanks

bibi
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:dbaSQL
ID: 36542561
here's another good one:
WITH Backups AS (
    SELECT
        server_name,
        database_name,
        MAX(backup_finish_date) AS LastBackupFinishDate
    FROM msdb.dbo.backupset
    WHERE type = 'D' AND is_damaged = 0 and server_name = @@SERVERNAME
    GROUP BY server_name, database_name
)  
SELECT
D.database_id,
D.name as database_name,
B.LastBackupFinishDate
FROM sys.databases D
LEFT JOIN Backups B on D.name = B.database_name42



let me look into the subsequent backup, based on last_backup_finish_date
0
 
LVL 10

Expert Comment

by:gaurav05
ID: 36542569
0
 

Author Comment

by:bibi92
ID: 36542599
yes, but how can I add a backup command in the script if the last_backup_finish_date is > 24 hours.
Thanks

bibi
0
 
LVL 17

Accepted Solution

by:
dbaSQL earned 500 total points
ID: 36542639
this is very, very rough....

declare @lastbackup datetime
select @lastbackup = MAX(backup_finish_date) from sys.databases db left outer join msdb..backupset b
  on db.name = b.database_name
where db.name = 'YourDBName'
select @lastbackup

IF @lastbackup < DATEADD(hour,-24,GETDATE())
BEGIN
            backup database YourDBName.....
END
0
 

Author Closing Comment

by:bibi92
ID: 36543223
Thanks bibi
0
 
LVL 17

Expert Comment

by:dbaSQL
ID: 36543324
My pleasure.
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question