You are on page 1of 11

Backup monitoring - PowerShell

Ver: 1

White Paper on

Backup monitoring - PowerShell
Submitted By:

Nikhil Vyas

1/26/2014

©2011 Accenture. All Rights Reserved.

1

Modified: 12/19/2014 10:44 PM

....................................................................................................................................................................................................... 3 Revision History ....................................Backup monitoring ................................................ All Rights Reserved.............................3 Introduction ......................................................................................... 5 Conclusion ....................................................................................................................................................................................3 Related Documents... 2 Modified: 12/19/2014 10:44 PM ................................................................................................................................................................................... 11 ©2011 Accenture................................................................................................................................................... 11 References .............................................................................................. 4 Procedure .................PowerShell Ver: 1 Table of Contents Control...........

All Rights Reserved. Title Description ©2011 Accenture.Backup monitoring . Location 3 Modified: 12/19/2014 10:44 PM .PowerShell Ver: 1 Control Revision History Date Version 1/26/2014 1.0 Description Initial Draft Author Nikhil Vyas Related Documents No.

All Rights Reserved. We need to make sure that our databases are being backed up. Windows PowerShell is Microsoft's task automation and configuration management framework.wikipedia. enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WSManagement and CIM enabling management of remote Linux systems and network devices. consisting of a command-line shell and associated scripting language built on . 4 Modified: 12/19/2014 10:44 PM . The last thing we would like to happen is our database going down and on that server is our client’s customer list or say banking transactions.PowerShell Ver: 1 Introduction One of the most evitable tasks for running a business is making sure we have the backup of the data in place. PowerShell provides full access to COM and WMI. we cannot recover the database and the business goes under. You can read more about it here – http://en.NET Framework.Backup monitoring . Without a backup. All this is to be done in a fairly quick time. What’s PowerShell? Before getting into the procedure let me reiterate what is powershell for you.org/wiki/Windows_PowerShell ©2011 Accenture. We also need to be able to check and confirm that every database on every server is being backed up.

If we have the SQL 2008 or later installed.The below command are independent of the version on Powershell Add-pssnapin SqlServerProviderSnapin100  In this case I will query a localized installation of SQL Server. Use DEFAULT if it is not a named instance (example . It doesn not matter which version of Powershell we are using.Server SQLSERVER and instance name as NORTHWIND would be would be SQLSERVER\NORTHWIND)  Now.PowerShell Ver: 1 Procedure There are many ways through which one can back up the database but in this case I would like to bring to notice the effectiveness of powershell in doing so. we can display the list of database on the SQL Instance and get the information about their last backup date using the below command – dir SQLSERVER:\SQL\SQLSERVER\NORTHWIND\Databases | Select Name. we should be good to go. Let us understand how PowerShell and also the SQL snap-ins help us to make the task much easier. We can change out “localhost\DEFAULT” for the SQL server we are using. All Rights Reserved.Backup monitoring . LastBackupDate | Out-GridView (Server is SQL SERVER and DB Instance is NORTHWIND) o The result will fetch you something like this – ©2011 Accenture. We can only do this on a machine where SQL snap-ins is installed. 5 Modified: 12/19/2014 10:44 PM . Open the SQL snap-ins using the below command on powershell.

Backup monitoring . MSDB databases will not be displayed with the above commands since they are the system database. LastBackupDate | Out-GridView Name AdventureWorks2008R2 Report Server SQLInfo BankDB master model msdb LastBackupDate 12/27/2013 11:53 12/26/2013 1:53 11/26/2013 10:23 11/5/2013 1:53 12/7/2013 0:00 12/8/2013 0:00 12/8/2013 1:05  As we see that we are getting the information about last backup taken but thing to note is that the information provided was the last full backup and no information provided for differential backup. MODEL. if we run the above commands and then backup and again run the command. All Rights Reserved. System Databases are hidden and hence we would need to use the “Force” parameter as – o dir -force SQLSERVER:\SQL\SQLSERVER\NORTHWIND\Databases | Select Name. 6 Modified: 12/19/2014 10:44 PM . Also. The solution is to run the refresh command. we would not see the change in results. ©2011 Accenture. This is because the data is cached every time we are running the command.PowerShell Ver: 1 Name AdventureWorks2008R2 LastBackupDate 12/27/2013 11:53 Report Server 12/26/2013 1:53 SQLInfo 11/26/2013 10:23 BankDB 11/5/2013 1:53  Please not that the MASTER.

$Northwind) { If ($SQLInstance. $_.ToString("yyyy/MM/dd HH:mm:ss")}}.width=25}.LastDifferentialBackupDate) {"FULL"} ©2011 Accenture.LastBackupDate.Backup monitoring .Expression={IF ($_.LastBackupDate eq "01/01/0001 00:00:00") {"NA"} ELSE {$_. LastBackupDate.PowerShell Ver: 1 dir -force SQLSERVER:\SQL\SQLSERVER\NORTHWIND\Databases | whereobject {$_. 7 Modified: 12/19/2014 10:44 PM .LastDifferentialBackupDate. @{Label="Last Differential". @{Label="Last Full Backup Date". LastDifferentialBackupDate | Out-GridView Name AdventureWorks2008R2 Report Server SQLInfo BankDB master model msdb LastBackupDate 12/27/2013 11:53 12/26/2013 1:53 11/26/2013 10:23 11/5/2013 1:53 12/7/2013 0:00 12/8/2013 0:00 12/8/2013 1:05 LastDifferentialbackup 12/27/2013 23:00 12/26/2013 22:00 11/26/2013 10:30 11/5/2013 11:00 12/7/2013 0:30 12/8/2013 9:30 12/8/2013 11:45  Now I would like to take one example of creating a function that would display required data every time we are calling the function.Expression={IF ($_. All Rights Reserved. @{Label="Most Recent Backup Type".Contains("`\")) { $location = "SQLSERVER:\SQL\$SQLServer\Databases" } Else { $location = "SQLSERVER:\SQL\$SQLServer\Northwind\Databases" } $DisplayResults = @{Label="DB Name".LastDifferentialBackupDate -eq "01/01/0001 00:00:00") {"NA"} ELSE {$_.LastBackupDate -gt $_.width=25}.Expression={IF ($_.LastBackupDate -eq "01/01/0001 00:00:00") {"NA"} ELSEIF ($_.Name}.ToString("yyyy/MM/dd HH:mm:ss")}}.width=30}.Refresh()} | Select Name. Function Get-DatabaseInfo ($SQLServer.Name.Expression={$_.

 Get-DatabaseInfo Northwind ©2011 Accenture.LastDifferentialBackupDate).Name -eq $Northwind.$_. @{Label="Days Since Last Backup".Days}}. $_.PowerShell Ver: 1 ELSE {"DIFF"}}.Backup monitoring .LastBackupDate).Expression={IF ($_.width=25}. we simply call this function along with the SQL instance name as shown here.LastBackupDate -eq "01/01/0001 00:00:00") {"Never Backed Up!"} ELSEIF ($_. $_.LastBackupDate) {((GetDate) .Refresh()} | format-table $DisplayResults } }  Now to return good. relevant information.$_.Refresh()} | format-table $DisplayResults } else { dir -force $location | where-object {$_.LastDifferentialBackupDate -gt $_. All Rights Reserved. 8 Modified: 12/19/2014 10:44 PM .Days} ELSE {((Get-Date) .width=25} if ($Northwind) { dir -force $location | where-object {$_.Name -ne "tempdb".

o SELECT * FROM dbo.Backup monitoring . The Invoke-Sqlcmd cmdlet lets you run your sqlcmd script files in a Windows PowerShell environment. All Rights Reserved. the following command will fetch us the results of all the backup information in table and from there we can send the information in a mail.PowerShell Ver: 1   Now when we have the information related to the entire database and their backup information. We will use the Invoke-sqlcmd . Say.  This can be achieved easily by fetching the data into a table in anyone of your database. we have the table called DBBackup.DBBackup  The below command will actually help us send the email to desired recipient. we would need the same information to you in the mail so that we do not have to run the command every time we need the information. 9 Modified: 12/19/2014 10:44 PM . ©2011 Accenture.

case LastAbsoluteBackupDate when '1900-01-01 00:00:00' then 'Never Backed Up' else convert(varchar. IncrementalBackup | out-string. ©2011 Accenture. All Rights Reserved. LastBackup.com" -Subject "Alarm – Database not backedup" –From"me@me.Backup monitoring . 120) end as LastBackup . DBBackup where IncrementalBackup > 0" | convertTo-Html Head$style -Property ServerName. DatabaseName.PowerShell Ver: 1 $FailedBackups = invoke-sqlcmd @params -Query "Select ServerName.com" -SmtpServer "mysmtpserver" -Body $FailedBackups –BodyAsHtml  We can get the most out of it by scheduling it to the desired timings using the SQL Server Agent which will help us avoid any issues prior to any business impact. 10 Modified: 12/19/2014 10:44 PM . We can then provide the SMTP information – Send-MailMessage -To "abc@xyz. case LastAbsoluteBackupDate when '1900-01-01 00:00:00' then 'Never Backed Up' else convert(varchar(20). LastAbsoluteBackupDate.DatabaseName .IncrementalBackup) end as IncrementalBackup from dbo.

Also. since multiple databases are involved here due to the indexing and maintenance strategies – the data retrieval will be far slower than doing it through the Windows Powershell. References 1.MSDN ©2011 Accenture.Backup monitoring . Wikipedia 2. Microsoft . All Rights Reserved. 11 Modified: 12/19/2014 10:44 PM .PowerShell Ver: 1 Conclusion Although getting information on backup can be accomplished by querying the database creating a function and calling it at the required time( We can even deliver the same to mailbox) however the biggest difficulty in getting that don’t through SQL is that It will not provide information of the database which are not backed up.