Advertisement
bdill

ps_copy_sql_log_files.ps1

Apr 14th, 2020 (edited)
1,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #-------------------------------------------------------------------------------
  2. # Desc: Copy last 24 hours of SQL log files to a disaster recovery (DR) server.  Run this on the DR server.
  3. #       Prunes files older than 24 hours on DR server to prevent bloat.
  4. # License: ShoutOutWare - give me a shout out on Twitter @bdill if this script helped you. :)
  5. # Script home: https://pastebin.com/QSf2WG1F
  6. # Other useful files: https://pastebin.com/u/bdill (scripts, data files, etc.)
  7. # Auth: Brian Dill
  8. # Created: 2020-04-14
  9. # Updated: 2020-12-03 added some comments for clarity
  10. #-------------------------------------------------------------------------------
  11.  
  12. cls
  13. $script:error.clear()
  14. #-------------------------------------------------------------------------------
  15. # CONFIG PARAMS
  16. #-------------------------------------------------------------------------------
  17. $CurrentDate  = Get-Date -Format yyyy-MM-dd_hhmm
  18. $ScriptName   = "ps_copy_sql_log_files.ps1"
  19. $LocalPath  = "W:\LogShip\"    # Base path on the DR server where the log files will be copied.  Create a sub folder for each DB.
  20. $LogFile      = [System.IO.Path]::Combine($LocalPath, "ps_copy_sql_log_files.log")  # Logfile of *this script* not SQL log files
  21. $SessionLogContainer = ""  #Holding tank for all session LogLine data
  22. #-------------------------------------------------------------------------------
  23. function LogLine
  24. { param ( $s )
  25.     $d2 = Get-Date -format "yyyy-MM-dd HH:mm:ss";
  26.     $s = "$d2 - $s";
  27.     Write-Host "$s";
  28.     Echo "$s" | Out-File $LogFile -append;
  29.     $script:SessionLogContainer += $s + "`n"
  30. }
  31. #-------------------------------------------------------------------------------
  32. function CopyLatestLogFiles {
  33.     param([string]$DBName, [string]$SourceFolder)
  34.  
  35.     $HoursBack = 24
  36.     $Destination = [System.IO.Path]::Combine($LocalPath, $DBName, "log")
  37.     LogLine("Copying $SourceFolder")
  38.  
  39.     $i = 0
  40.     Get-ChildItem $SourceFolder |
  41.         Where-Object {$_.LastWriteTime -gt (get-date).AddHours(-1*($HoursBack))} |
  42.         ForEach {
  43.             $ModifiedDestination = $($_.FullName).Replace("$SourceFolder","$Destination")
  44.             If ((Test-Path $ModifiedDestination) -eq $False) {  # if the file has not already been copied
  45.                 Copy-Item $_.FullName $ModifiedDestination
  46.                 $i = $i + 1
  47.             }
  48.         }
  49.  
  50.     LogLine("Copied $i files from $SourceFolder")
  51.     DelFilesOlderThan $HoursBack $Destination
  52.  
  53. }
  54. #-------------------------------------------------------------------------------
  55. function DelFilesOlderThan {
  56.     Param( [int]$HoursBack, [string]$Path )
  57.     LogLine("Deleting files older than $HoursBack hours from $Path")
  58.     $DatetoDelete = (Get-Date).AddHours(-1*($HoursBack))
  59.     Get-ChildItem -File $Path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item
  60. }
  61. #-------------------------------------------------------------------------------
  62.  
  63. #-------------------------------------------------------------------------------
  64.  
  65. LogLine "--------------------------------------------------------------------------------"
  66. LogLine "(Starting) $ScriptName"
  67. CopyLatestLogFiles "DB1" "\\server1\D$\Backups\DB1\LOG"
  68. CopyLatestLogFiles "DB2" "\\server1\D$\Backups\DB1\LOG"
  69. CopyLatestLogFiles "DB3" "\\server1\D$\Backups\DB3\LOG"
  70. LogLine "(complete) $ScriptName"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement