Guest User

Untitled

a guest
Oct 14th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. $FtpTargetDirectory = "/Database/Backup"
  2. $HostName = "dtbunnsql.cloudapp.net"
  3. $UserName = "dtbunnsql"
  4. $Password = "Passw0rd"
  5. $BackupDirectory = "D:\tmp\bunnigbak"
  6.  
  7. Add-Type -Path "WinSCPnet.dll"
  8.  
  9.  
  10. $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
  11. Protocol = [WinSCP.Protocol]::Sftp
  12. HostName = $HostName
  13. UserName = $UserName
  14. Password = $Password
  15. SshHostKeyFingerprint = "ssh-rsa 1024 70:34:fd:93:18:e2:25:df:ee:82:1d:d2:a7:71:11:b5"
  16. }
  17.  
  18.  
  19. function GetLatestFile()
  20. {
  21. # $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
  22. # Protocol = [WinSCP.Protocol]::Sftp
  23. # HostName = $HostName
  24. # UserName = $UserName
  25. # Password = $Password
  26. # SshHostKeyFingerprint = "ssh-rsa 1024 70:34:fd:93:18:e2:25:df:ee:82:1d:d2:a7:71:11:b5"
  27. # }
  28.  
  29. $fileDate = 0
  30. $session = New-Object WinSCP.Session
  31. $targetDate = 20100101
  32. $filesToGet = New-Object System.Collections.ArrayList
  33.  
  34. try
  35. {
  36. # Connect
  37. $session.Open($sessionOptions)
  38.  
  39. # List directory
  40. $directory = $session.ListDirectory($FtpTargetDirectory)
  41.  
  42. # Get latest file
  43. foreach ($fileInfo in $directory.Files)
  44. {
  45. Write-Host "Writting out the name $fileInfo.Name"
  46.  
  47. $fName = $fileInfo.Name
  48.  
  49. if ($fName.length -gt 8)
  50. {
  51. $valfile = $fileInfo.Name.Substring($fileInfo.Name.length - 12, 8)
  52. $valOk = [int32]::TryParse($valfile , [ref] $fileDate)
  53. if ($valOk -eq $True)
  54. {
  55. if ($targetDate -lt $fileDate)
  56. {
  57. $targetDate = $fileDate
  58. }
  59. }
  60. }
  61. }
  62. }
  63. finally
  64. {
  65. # Disconnect, clean up
  66. $session.Dispose()
  67. }
  68.  
  69. Write-Host $targetDate
  70. return $targetDate
  71. }
  72.  
  73. function CheckIfBackupPatternExist($pattern)
  74. {
  75. $path = "$BackupDirectory\*$pattern*"
  76. return test-path $path -pathtype leaf
  77. }
  78.  
  79. function GetFile($targetFile) {
  80.  
  81. # $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
  82.  
  83. # Protocol = [WinSCP.Protocol]::Sftp
  84. # HostName = $HostName
  85. # UserName = $UserName
  86. # Password = $Password
  87. # SshHostKeyFingerprint = "ssh-rsa 1024 70:34:fd:93:18:e2:25:df:ee:82:1d:d2:a7:71:11:b5"
  88. # }
  89.  
  90. $sessionOptions.AddRawSettings("FSProtocol", "2")
  91. $session = New-Object WinSCP.Session
  92.  
  93. try
  94. {
  95. # Connect
  96. $session.Open($sessionOptions)
  97. #$session.GetFiles("/Database/Backup/" + $targetFile, "d:\tmp\bunnigbak\*")
  98.  
  99. $targetFile = "20181011"
  100.  
  101. $ftpPattern = "/Database/Backup/*$targetFile*.BAK";
  102. $ftpTargetDirectory = $BackupDirectory + "\*"
  103.  
  104. Write-Host("Ftp pattern to download: $ftpPattern Target folder: $ftpTargetDirectory")
  105. $session.GetFiles($ftpPattern, $ftpTargetDirectory)
  106.  
  107. # Your code
  108. }
  109. finally
  110. {
  111. $session.Dispose()
  112. }
  113. }
  114.  
  115. function Main()
  116. {
  117. $filePattern = GetLatestFile
  118. Write-Host "Latest backup obtained from ftp site [$filePattern]"
  119.  
  120. $backupFilesExist = CheckIfBackupPatternExist($filePattern)
  121.  
  122. if ($backupFilesExist -eq $false)
  123. {
  124. Write-Host "Downloading latest backup from server......please wait"
  125. GetFile $filePattern
  126. }
  127. else {
  128. Write-Host "Backup files already exists. No restore will happened."
  129. }
  130. }
  131.  
  132. ##### Execute Main function #######
  133. Main
Add Comment
Please, Sign In to add comment