powershell to download file from sftp site
Here is a sample powershell script to download files from a sftp site using WinScp's dll.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory=$false)] | |
[string]$FtpTargetDirectory, | |
[Parameter(Mandatory=$false)] | |
[string]$HostName, | |
[Parameter(Mandatory=$false)] | |
[string]$UserName, | |
[Parameter(Mandatory=$false)] | |
[string]$Password, | |
[Parameter(Mandatory=$false)] | |
[string]$DevLocalBackupDirectory, | |
[Parameter(Mandatory=$false)] | |
[string]$Bakfile | |
) | |
# | |
# To test : | |
# .\getbackup.ps1 -FtpTargetDirectory "/Database/Backup" -Hostname "dtbunnsql.cloudapp.net" -UserName "dtbunnsql" -Password "Passw0rd" -DevLocalBackupDirectory "D:\tmp\bunnigbak" -Bakfile "Red.Website.Sitecore.Reporting_20181012" | |
# | |
# $FtpTargetDirectory = "/Database/Backup" | |
# $HostName = "dtbunnsql.cloudapp.net" | |
# $UserName = "dtbunnsql" | |
# $Password = "Passw0rd" | |
# $DevLocalBackupDirectory = "D:\tmp\bunnigbak" | |
Add-Type -Path "WinSCPnet.dll" | |
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{ | |
Protocol = [WinSCP.Protocol]::Sftp | |
HostName = $HostName | |
UserName = $UserName | |
Password = $Password | |
SshHostKeyFingerprint = "ssh-rsa 1024 70:34:fd:93:18:e2:25:df:ee:82:1d:d2:a7:71:11:b5" | |
} | |
function GetLatestFile() | |
{ | |
$fileDate = 0 | |
$session = New-Object WinSCP.Session | |
$targetDate = 20100101 | |
$filesToGet = New-Object System.Collections.ArrayList | |
try | |
{ | |
# Connect | |
$session.Open($sessionOptions) | |
# List directory | |
$directory = $session.ListDirectory($FtpTargetDirectory) | |
# Get latest file | |
foreach ($fileInfo in $directory.Files) | |
{ | |
Write-Host "Writting out the name $fileInfo.Name" | |
$fName = $fileInfo.Name | |
if ($fName.length -gt 8) | |
{ | |
$valfile = $fileInfo.Name.Substring($fileInfo.Name.length - 12, 8) | |
$valOk = [int32]::TryParse($valfile , [ref] $fileDate) | |
if ($valOk -eq $True) | |
{ | |
if ($targetDate -lt $fileDate) | |
{ | |
$targetDate = $fileDate | |
} | |
} | |
} | |
} | |
} | |
finally | |
{ | |
# Disconnect, clean up | |
$session.Dispose() | |
} | |
Write-Host $targetDate | |
return $targetDate | |
} | |
function CheckIfBackupPatternExist($pattern) | |
{ | |
$path = "$DevLocalBackupDirectory\*$pattern*" | |
return test-path $path -pathtype leaf | |
} | |
function GetFile($targetFile) { | |
$sessionOptions.AddRawSettings("FSProtocol", "2") | |
$session = New-Object WinSCP.Session | |
try | |
{ | |
# Connect | |
$session.Open($sessionOptions) | |
$ftpPattern = "$FtpTargetDirectory/*$targetFile*.BAK"; | |
$ftpTargetDirectory = $DevLocalBackupDirectory + "\*" | |
Write-Host("Ftp pattern to download: $ftpPattern Target folder: $ftpTargetDirectory") | |
$session.GetFiles($ftpPattern, $ftpTargetDirectory) | |
} | |
finally | |
{ | |
$session.Dispose() | |
} | |
} | |
function Main() | |
{ | |
# Get latest backup from stp server | |
if ((test-path $DevLocalBackupDirectory) -eq $False) | |
{ | |
Write-Host "Download path does not exist. Creating $DevLocalBackupDirectory" | |
New-Item -ItemType directory -Path $DevLocalBackupDirectory | |
} | |
if ($Bakfile -eq "") | |
{ | |
$filePattern = GetLatestFile | |
} | |
else { | |
# default to this backup | |
$filePattern = $Bakfile | |
} | |
Write-Host "Latest backup obtained from ftp site [$filePattern]" | |
$backupFilesExist = CheckIfBackupPatternExist($filePattern) | |
if ($backupFilesExist -eq $false) | |
{ | |
Write-Host "Downloading latest backup from server......please wait" | |
GetFile $filePattern | |
} | |
else { | |
Write-Host "Backup files already exists. No restore will happened." | |
} | |
} | |
##### Execute Main function ####### | |
Main |
Comments