简体   繁体   中英

Using Powershell to get SQL Backup Data

I've been looking at ways to automatically get information about SQL backups, and the Powershell script in this Technet article would do nicely, except for an issue with the data it returns.

When run, it returns a "last backup" date for test databases I know have not been backed up via SQL. It would appear that it's taking the times of the system backup; it coincides with beVSS start times in the OS event log (the server is backed up by Backup Exec 2010, not using the SQL agent).

I would like it to gather the last time that the SQL backup was run, not a system backup. Is this possible to filter out somehow?

The DB Server is Windows Server 2008/SQL server 2008.

Edit :

To clarify, I'm looking for solutions in PowerShell, not SQL, and to understand why PowerShell returns what it does.

I often use a query like this when looking for the time and filename of the most recent backup. This particular query will return either the most recent FULL Backup or Differential Backup but obviously you can change that if you like.

SELECT 
    bf.physical_device_name ,
    name ,
    bs.backup_start_date ,
    bs.media_set_id
    FROM sys.databases d
    OUTER APPLY (
        SELECT TOP 1
            backup_start_date ,
            media_set_id
        FROM msdb.dbo.backupset
        WHERE type != 'L'
        AND database_name = d.name
        ORDER BY backup_start_date DESC
    ) bs
INNER JOIN msdb.dbo.backupmediafamily bf ON bf.media_set_id = bs.media_set_id

I've tended to use queries against the backupset table in msdb instead of the SMO LastBackupDate Database property as mentioned in the article you referenced.

In my environment we're using CommVault Galaxy for backups and I can filter out backups by CommvVault vs. regular SQL backups by looking at the the user_name column of the backupset table. SQL Agent and CommVault run under difference user accounts.

http://msdn.microsoft.com/en-us/library/ms186299.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM