Advertisement
Guest User

Untitled

a guest
Oct 14th, 2017
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Script for backuping SQL databases to remote host
  2. ## Requirements: Task scheduler account must be sysadmin in SQL Server and have proper ACL priveleges on folders;
  3. ## Account under which run's SQL Server must have priveleges on local backup folder
  4. ## Author: Denis Goncharov 8/16/2017
  5. ## v.0.3
  6.  
  7. $BackupDate = get-date -format yyyyMMdd_HHmmss
  8. $RemoteBackupDirectory = "\\tvebus10\Backups\RUTVEM0APP05\"
  9. $LocalBackupDirectory = "C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\"
  10. $CurrentBackupDirectory = $LocalBackupDirectory + $BackupDate
  11. $date_limit = (Get-Date).AddDays(-14);
  12. $hostname = (get-childitem -path env:computername).value
  13.  
  14. if (!(Test-Path $CurrentBackupDirectory)) {New-Item -ItemType Directory $CurrentBackupDirectory}
  15. if (!(Test-Path $RemoteBackupDirectory)) {New-Item -ItemType Directory $RemoteBackupDirectory}
  16.  
  17. $exceptions = "master|model|msdb|Report|tempdb"
  18.  
  19. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
  20. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
  21. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
  22. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null
  23.  
  24. $server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") 'localhost'
  25. $server.ConnectionContext.StatementTimeout = 0
  26. $DatabaseList = $server.Databases
  27.  
  28. foreach ($Database in $DatabaseList) {
  29.     if ($Database.Name -notmatch $exceptions) {
  30.         $dbName = $Database.Name
  31.         $localSqlBackupPath = $CurrentBackupDirectory + "\" + $dbName + "_" + $BackupDate + ".bak"
  32.         $backup = New-Object ("Microsoft.SqlServer.Management.Smo.Backup")
  33.         $backup.Action = "Database"
  34.         $backup.BackupSetDescription = "Full backup of " + $dbName
  35.         $backup.BackupSetName = $dbName + " backup"
  36.         $backup.Database = $dbName
  37.         $backup.MediaDescription = "Disk"
  38.         $backup.Devices.AddDevice("$localSqlBackupPath", "File")
  39.         $backup.SqlBackup($server)
  40.     }
  41. }
  42. Copy-Item -Recurse $CurrentBackupDirectory $RemoteBackupDirectory
  43.  
  44. function compare_size ($FirstDirectory,$SecondDirectory){
  45.    if(!(New-Object -COM 'Scripting.FileSystemObject').GetFolder($FirstDirectory).Size -eq (New-Object -COM 'Scripting.FileSystemObject').GetFolder($SecondDirectory).Size){
  46.         return $true
  47.     }
  48. return $false
  49. }
  50.  
  51. function Send-Email () {
  52.     $emailSmtpServer = "corimc04.corp.jabil.org"
  53.     $emailSmtpServerPort = "25"
  54.     $emailSmtpUser = "Backup_SQL_"+$hostname+"@jabil.com"
  55.     $emailSmtpPass = ""
  56.     $recipient = @("anastasia_ivanova@jabil.com","denis_goncharov@jabil.com","aleksandr_dobrov@jabil.com","mikhail_kiselev@Jabil.com")
  57.     $encoding = [System.Text.Encoding]::UTF8
  58.     $msgText = "Backup error on "+$hostname
  59.     Send-mailmessage -from $emailSmtpUser -to $recipient -subject "Error backuping $hostname" -body $msgText -smtpServer $emailSmtpServer -Encoding $encoding -Attachments $diff_file_path
  60.  
  61. }
  62.  
  63. function get_backups($Root_path) {
  64.     $array_backups = Get-ChildItem -Path $Root_path -Recurse -Force -Directory | Where-Object { $_.PSIsContainer -and $_.CreationTime -lt $date_limit } |
  65.         ForEach-Object {[object] @{Name = $_.Name; Path = $_.FullName; Size = ((New-Object -COM 'Scripting.FileSystemObject').GetFolder($_.FullName).Size) }}
  66.     return $array_backups
  67. }
  68. $tmp_var = $RemoteBackupDirectory+$BackupDate
  69.  
  70. if((compare_size $CurrentBackupDirectory $tmp_var)){
  71.     Send-Email
  72. }
  73.  
  74. $local_backups = get_backups $LocalBackupDirectory
  75. $remote_backups = get_backups $RemoteBackupDirectory
  76.  
  77. $local_backups | ForEach-Object ($_) {  Remove-Item -Force -Recurse $_.Path } | Where-Object { $remote_backups -eq $_.Size }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement