如何在PowerShell中通过FTP上传数据库备份文件

时间:2018-05-07 09:38:04

标签: powershell

我正忙着编写一个power-shell脚本,用于将数据库备份文件上传到其他位置。请参阅下面的代码:

$Dir="\\server\COM\"
#ftp server
$ftp = "ftp://ftp.xyz.com/"
$user = "user1"
$pass = "pass1"
$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  

#Lets upload latest backup file
$latest = Get-ChildItem -Path $dir -Filter *.bak | Sort-Object LastWriteTime -Descending |Select-Object -First 1
Write-Output "The latest db backup file is $latest. Let's start uploading"

"Uploading $latest..."
$uri = New-Object System.Uri($ftp+$latest.Name) 
$webclient.UploadFile($uri, $latest.FullName) 

我没有看到我的脚本有什么问题,但由于某些原因,脚本无法运行,我无法弄清楚问题是什么。我收到以下错误:

Exception calling "UploadFile" with "2" argument(s): "The remote server returned an error: (534) 534 Policy requires SSL.

我仍然在学习power-shell而不是那么擅长编写脚本。有人可以帮忙吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

使用此脚本:

   #$Dir=""
     $Dir = ""
     $CertificateFingerprint = ""

 $newFileName = Get-ChildItem -Path $dir -Filter *.bak | Sort-Object LastWriteTime -Descending |Select-Object -First 1

 $Day = Get-Date
 $UploadDay= $Day.DayOfWeek  


 If($UploadDay -eq "Tuesday" ){ 

      Write-Output "The latest db backup file is $newFileName. Let's start uploading"


      Add-FTPItem -Path "" -LocalPath "" -Username "" -Password "" -FTPHost "" 
      Write-Output "Upload completed!"

    }else {
        "Upload failed!"    
    }


 #$DiffBackupDir="\\"
 $DiffBackupDir = ""

 $DiffBackup = Get-ChildItem -Path $DiffBackupDir -Filter *.bak | Sort-Object LastWriteTime -Descending |Select-Object -First 1

 Write-Output "The latest db backup file is $DiffBackup. Let's start uploading"

 #Upload of a differential backup to FTP Folder
 Add-FTPItem -Path "" -LocalPath "" -Username "" -Password "" -FTPHost ""