Differences

This shows you the differences between the selected revisions of the page.

2012-04-03 2012-04-03
vbnet example (martin) powershell example (martin)
Line 134: Line 134:
End Class End Class
 +</code>
 +
 +==== [[powershell]] PowerShell Example ====
 +<code powershell>
 +try
 +{
 +    # Load WinSCP .NET assembly
 +    [Reflection.Assembly]::LoadFrom("WinSCP.dll") | Out-Null
 +
 +    # Setup session options
 +    $sessionOptions = New-Object WinSCP.SessionOptions
 +    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
 +    $sessionOptions.HostName = "example.com"
 +    $sessionOptions.UserName = "user"
 +    $sessionOptions.Password = "mypassword"
 +    $sessionOptions.SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
 +
 +    $session = New-Object WinSCP.Session
 +
 +    try
 +    {
 +        # Connect
 +        $session.Open($sessionOptions)
 +
 +        # Execute mysqldump on the server to dump all MySQL databases and compress the results
 +        $dbUsername = "USERNAME"
 +        $dbPassword = "PASSWORD"
 +        $tempFilePath = "/tmp/all_databases.gz"
 +
 +        $dumpCommand =
 +            ("mysqldump --opt -u {0} --password={1} --all-databases | gzip > {2}" -f `
 +                $dbUsername, $dbPassword, $tempFilePath)
 +        $session.ExecuteCommand($dumpCommand)
 +
 +        # Download the database dump
 +        $session.GetFiles($tempFilePath, "D:\dbbackup\").Check()
 +
 +    finally
 +    {
 +        # Disconnect, clean up
 +        $session.Dispose()
 +    }
 +
 +    exit 0
 +}
 +catch [Exception]
 +{
 +    Write-Host $_.Exception.Message
 +    exit 1
 +}
</code> </code>

Last modified: by martin