[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

script for check backup SQL SERVER

Posted on 2011-09-15
9
Medium Priority
?
326 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
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 UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

656 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