Differences
This shows you the differences between the selected revisions of the page.
2012-01-10 | 2012-01-13 | ||
toc (martin) | example (martin) | ||
Line 31: | Line 31: | ||
The command must not require user input. | The command must not require user input. | ||
+ | ===== Example ===== | ||
+ | <code csharp> | ||
+ | using System; | ||
+ | using WinSCP; | ||
+ | |||
+ | class Example | ||
+ | { | ||
+ | public static int Main() | ||
+ | { | ||
+ | try | ||
+ | { | ||
+ | // Setup session options | ||
+ | SessionOptions sessionOptions = new SessionOptions(); | ||
+ | sessionOptions.Protocol = 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"; | ||
+ | |||
+ | using (Session session = new Session()) | ||
+ | { | ||
+ | // Connect | ||
+ | session.Open(sessionOptions); | ||
+ | |||
+ | // Execute mysqldump on the server to dump all MySQL databases and compress the results | ||
+ | const string dbUsername = "USERNAME"; | ||
+ | const string dbPassword = "PASSWORD"; | ||
+ | const string tempFilePath = "/tmp/all_databases.gz"; | ||
+ | |||
+ | string dumpCommand = | ||
+ | string.Format("mysqldump --opt -u {0} --password={1} --all-databases | gzip > {2}", | ||
+ | dbUsername, dbPassword, tempFilePath); | ||
+ | session.ExecuteCommand(dumpCommand); | ||
+ | |||
+ | // Download the database dump | ||
+ | session.GetFiles(tempFilePath, "D:\\dbbackup\\").Check(); | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | catch (Exception e) | ||
+ | { | ||
+ | Console.WriteLine("Error: {0}", e); | ||
+ | return 1; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </code> |