PowerShell sync script delete remote files and start over again
Hello, I am facing a strange problem I couldn't find anywhere (maybe I am doing a wrong research).
I have a script that synchronize a local drive over internet with FTP TLS. But if I get an error while synchronize (the error is "failure reading network stream" and I am facing that too), the process will start over again, deleting the already transferred files instead of just continue from where it stopped. The strange thing is that if it upload, for example, 60Gb of files, it will delete until they are like 40Gb and then start the process. Any help?
Sorry if I'm not 100% clear, you can always ask me more info.
This is the code:
I have a script that synchronize a local drive over internet with FTP TLS. But if I get an error while synchronize (the error is "failure reading network stream" and I am facing that too), the process will start over again, deleting the already transferred files instead of just continue from where it stopped. The strange thing is that if it upload, for example, 60Gb of files, it will delete until they are like 40Gb and then start the process. Any help?
Sorry if I'm not 100% clear, you can always ask me more info.
This is the code:
# Load WinSCP .NET assembly Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll" # Session.FileTransferred event handler function FileTransferred { param($e) if ($e.Error -eq $Null) { Write-Host "Upload of $($e.FileName) succeeded" } else { Write-Host "Upload of $($e.FileName) failed: $($e.Error)" } if ($e.Chmod -ne $Null) { if ($e.Chmod.Error -eq $Null) { Write-Host "Permissions of $($e.Chmod.FileName) set to $($e.Chmod.FilePermissions)" } else { Write-Host "Setting permissions of $($e.Chmod.FileName) failed: $($e.Chmod.Error)" } } else { Write-Host "Permissions of $($e.Destination) kept with their defaults" } if ($e.Touch -ne $Null) { if ($e.Touch.Error -eq $Null) { Write-Host "Timestamp of $($e.Touch.FileName) set to $($e.Touch.LastWriteTime)" } else { Write-Host "Setting timestamp of $($e.Touch.FileName) failed: $($e.Touch.Error)" } } else { # This should never happen during "local to remote" synchronization Write-Host "Timestamp of $($e.Destination) kept with its default (current time)" } } # Main script try { $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Ftp HostName = "DOMAINHERE" PortNumber = PORT UserName = "USERNAME" Password = "PASSWORD" FtpSecure = [WinSCP.FtpSecure]::Explicit TlsHostCertificateFingerprint = "THE LONG CERTIFICATE FINGERPRINT" } $transferOptions = New-Object WinSCP.TransferOptions $transferOptions.FileMask = "*.*| System Volume Information/" $session = New-Object WinSCP.Session try { # Will continuously report progress of synchronization $session.add_FileTransferred( { FileTransferred($_) } ) # Connect $sessionLogPath="C:\ftp\session.log" $session.SessionLogPath = $sessionLogPath $session.Open($sessionOptions) # Synchronize files $synchronizationResult = $session.SynchronizeDirectories( [WinSCP.SynchronizationMode]::Remote, "V:\", "/backup/g", $False, $false, [WinSCP.SynchronizationCriteria]::Time, $transferOptions) # Throw on any error $synchronizationResult.Check() } finally { # Disconnect, clean up $session.Dispose() } exit 0 } catch { Write-Host "Error: $($_.Exception.Message)" exit 1 }