Solved

script for check backup SQL SERVER

Posted on 2011-09-15
9
320 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DTS Connection Failed 7 70
Access denied running PowerPivot -SQL Server 2014 on Windows Server 2012 10 30
SqlServer no dupes 25 34
Increment column based of a FK 8 21
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…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

773 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