Introduction
I am currently working in an environment where database backup files are generated on production servers and it gets replicated across to another data centre.
The replication is done via a scheduled task in Windows Task Scheduler. The scheduled task merely runs the robocopy command with the appropriate parameters.
Detecting if the Files Have Stopped Replicating
One of the things I've learnt all these years is that if you forget to check if something is working, it ends up being that that's when it stops working.
I have created a PowerShell script which will read a text file containing a list of folders it needs to check to determine if the latest backup files have come in or not. It will send an e-mail to the appropriate administrators.
You can modify the PowerShell script to meet your needs, but I will use it as a way to demonstrate PowerShell programming.
DISCLAIMER:
It is NOT implied that it will work in your environment and that it will suit your particular purpose. You should review and modify it to suit your own needs.
Script Listing
## Start of Script
############################## ############################## ############################## ###############
write-output $outputmessage;
send-mailmessage -To $emailToAddress -Subject "ALERT: Backup files failed to come in" -From "SERVERA Backup Server<donotreply@myorg.com.au>" -Body "Please see attached report." -SmtpServer "smtp" -Attachment $report
## NOTE: The above line highlighted in blue for clarity is one PowerShell cmdlet statement with parameters. They need to be on one line
## in the script and
## cannot extend over multiple lines without explicitly using the grave accent (or backtick) i.e. ` . Some other character elements i.e. | or { }
## naturally allow multiple lines
}
## You may wish to add an else clause and send an e-mail where no errors are found. That's entirely up to you, depending on the
## frequency of your checks.
## End of Script
I am currently working in an environment where database backup files are generated on production servers and it gets replicated across to another data centre.
The replication is done via a scheduled task in Windows Task Scheduler. The scheduled task merely runs the robocopy command with the appropriate parameters.
Detecting if the Files Have Stopped Replicating
One of the things I've learnt all these years is that if you forget to check if something is working, it ends up being that that's when it stops working.
I have created a PowerShell script which will read a text file containing a list of folders it needs to check to determine if the latest backup files have come in or not. It will send an e-mail to the appropriate administrators.
You can modify the PowerShell script to meet your needs, but I will use it as a way to demonstrate PowerShell programming.
DISCLAIMER:
It is NOT implied that it will work in your environment and that it will suit your particular purpose. You should review and modify it to suit your own needs.
Script Listing
## Start of Script
##############################
##
##
## This script checks the backup folders as listed in listofbackupdirs.txt
##
## and test to see if the last backup file to have come in for that folder is within the specified number of hours as specified in the file
##
##
##
##
##
##
## Clears the screen
clear-host
## Initializes variables
$ScriptsFolder="F:\PowerShell\ Scripts\CheckBackupFilesFlow"
$listofbackupdirs=" listofbackupdirs.txt"
$haserrored=$false
$emailToAddress="Admin Nistrator1<ANistrator1@myorg.com.au>"," Admin Nistrator2<ANistrator2@myorg.com.au>"
$mydate=get-date -uformat %Y%m%d%H%M
$report="$ScriptsFolder\ ErrorLog-" + $mydate+".log";
## Get current date and time
$a=get-date
write-output "Checking Backup Folders ....."
write-output "Checking Backup Folders ....." > $report ;
write-output $a >> $report ;
set-location -Path $ScriptsFolder
## reads the list of backup directories and the specified number of hours
Import-Csv $listofbackupdirs | foreach-object -process {
[int64]$offsethours=$_. OffsetHours;
$backuppath=$_.BackupPath;
write-output $backuppath;
## Finds the last file that have come in in that backup folder by sorting the files by creation time. It will also sort by file names if there are
## different sets of file names in the same folder - so be careful here, as the last file to come in may not be what you are expecting.
$b=get-childitem $backuppath | where-object {$_.Mode -ne "d----"} |
sort-object -property $_.CreationTime -Descending |
select-object -Index 0
## different sets of file names in the same folder - so be careful here, as the last file to come in may not be what you are expecting.
$b=get-childitem $backuppath | where-object {$_.Mode -ne "d----"} |
sort-object -property $_.CreationTime -Descending |
select-object -Index 0
## Calculates the offset timeframe between the current time and the timestamp of the last file that is found by the sort-object cmdlet in the directory
$timedelta = New-Timespan -Start $b.CreationTime -End $a
## Checks to see if the calculated offset (timedelta) is greater than what is specified in the file
If ($timedelta.Ticks -ge $offsethours * 36000000000 ) {
$haserrored=$true;
$outputmessage = "Backups are not coming in for " + $backuppath
write-output $outputmessage;
write-output $outputmessage >> $report;
write-output "Last file that came in was $b ";
write-output "Last file that came in was $b " >> $report ;
write-output "Last file that came in was $b " >> $report ;
write-output $b;
write-output $b >> $report ;
write-output "";
}
}
if ($haserrored -eq $true) {
send-mailmessage -To $emailToAddress -Subject "ALERT: Backup files failed to come in" -From "SERVERA Backup Server<donotreply@myorg.com.au>" -Body "Please see attached report." -SmtpServer "smtp" -Attachment $report
## NOTE: The above line highlighted in blue for clarity is one PowerShell cmdlet statement with parameters. They need to be on one line
## in the script and
## cannot extend over multiple lines without explicitly using the grave accent (or backtick) i.e. ` . Some other character elements i.e. | or { }
## naturally allow multiple lines
}
## You may wish to add an else clause and send an e-mail where no errors are found. That's entirely up to you, depending on the
## frequency of your checks.
## End of Script
Contents of ListOfBackupDirs.txt
The below, including the header row, needs to be saved in a file called ListOfBackupDirs.txt and placed in the same location as the script. Put your own folder locations in, and a time in hours. The folders to be checked must be on separate lines, and you can have as many as you want (as much as PowerShell will allow).
The below, including the header row, needs to be saved in a file called ListOfBackupDirs.txt and placed in the same location as the script. Put your own folder locations in, and a time in hours. The folders to be checked must be on separate lines, and you can have as many as you want (as much as PowerShell will allow).
BackupPath,OffsetHours
F:\SQLBackups\SQLServer1,2
F:\SQLBackups\SQLServer2,2
F:\SQLBackups\SQLServer3,4
Comments
Post a Comment