Quantcast
Viewing latest article 3
Browse Latest Browse All 40

Checking For A Database Backup with PowerShell

Todays PowerShell Box of Tricks Post is about backups

I highly recommend you go and read bookmark and come straight back Stuart Moore’s series on Backups with Powershell The link takes you to the category showing all of the posts in the series

Whilst I have many other methods to inform me if backups fail, sometimes someone walks up to the desk ignores the headphones in my ears (They are bright yellow FFS), eyes fixed on the screen, face deep in concentration and asks me a question. It was for times like these that the functions from this series were written.

“When was this database last backed up?”

Auditors, managers, bosses, developers all can come and ask this question. I just turn to my PowerShell prompt type

Image may be NSFW.
Clik here to view.
image

and show him the screen. Job done (Sometimes)

As I mentioned previously there are 154 properties on the database object. Three of them are

LastBackupDate
LastDifferentialBackupDate
LastLogBackupDate

We simply call these and if the date reported is ’01 January 0001 00:00:00′ print NEVER

The Code is here

#############################################################################################
#
# NAME: Show-LastServerBackup.ps1
# AUTHOR: Rob Sewell http://newsqldbawiththebeard.wordpress.com
# DATE:06/08/2013
#
# COMMENTS: Load function for Showing Last Backup of each database on a server
# ————————————————————————

Function Show-LastDatabaseBackup ($SQLServer, $sqldatabase) {
    $server = new-object "Microsoft.SqlServer.Management.Smo.Server" $SQLServer
    $db = $server.Databases[$sqldatabase]

    Write-Output "Last Full Backup"
    $LastFull = $db.lastbackupdate
    if ($lastfull -eq '01 January 0001 00:00:00')
    {$LastFull = 'NEVER'}
    Write-Output $LastFull
    Write-Output "Last Diff Backup"
    $LastDiff = $db.LastDifferentialBackupDate  
    if ($lastdiff -eq '01 January 0001 00:00:00')
    {$Lastdiff = 'NEVER'}
    Write-Output $Lastdiff
    Write-Output "Last Log Backup"                                                  $lastLog = $db.LastLogBackupDate 
    if ($lastlog -eq '01 January 0001 00:00:00')
    {$Lastlog = 'NEVER'}
    Write-Output $lastlog
}

 


Viewing latest article 3
Browse Latest Browse All 40

Trending Articles