PowerShell sync script delete remote files and start over again

Advertisement

kingtech
Joined:
Posts:
2

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:
# 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
}

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,034
Location:
Prague, Czechia

Re: PowerShell sync script delete remote files and start over again

Please attach a full session log file showing the problem (using the latest version of WinSCP). And give us names of the files that where deleted and retransferred after the network failure.

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

kingtech
Joined:
Posts:
2

Thanks for the reply, I will attach part of the session log because it's actual size is 120 Mb. I don't have a specific filename because it seems to randomly crash while transferring (maybe I am wrong but I'm new to the FTP scenario). If this hint can help I have built an Ubuntu FTP Server with VSFTPD.
Description: I deleted the mid part so the file will have the right maximum size

Reply with quote

Advertisement

You can post new topics in this forum