Solved

script for check backup SQL SERVER

Posted on 2011-09-15
9
319 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

910 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