Solved

script for check backup SQL SERVER

Posted on 2011-09-15
9
318 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
  • 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Select2 jquery help 9 45
Getting certain data from a string 1 24
Mssql SQL query 14 27
SQL Date Retrival 7 28
Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now