Mysqldump does not work when launched from ASP.NET web application as a new process
It works correctly when run from the "run" window.
cmd.exe /K mysqldump --add-drop-database --add-drop-table --user=root --password=thepassword --databases theDatabase > C:\Backup\theBackup.sql
However, the same command, when I try to execute from my web application by calling an external process, fails.
Here's the code:
ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", @"/K mysqldump --add-drop-database --add-drop-table --user=root --password=thepassword --databases theDatabase > C:\Backup\theBackup" + ".sql");
Process p = Process.Start(processInfo);
This is what I get ...
alt text http://img40.imageshack.us/img40/8121/mysqldumperror.gif
Interestingly, the file - theBackup.sql - is created but empty.
This is not a problem with the environment PATH variable; the path to the bin directory in MySql that contains mysqldump is added to the environment PATH variable. To test this, if I open a command prompt, navigate to the path given in the screenshot above and enter the mysqldump command manually, it will recognize the command ... as shown below ...
alt text http://img40.imageshack.us/img40/6879/mysqldumppathproper.gif
The problem is with mysqldump as the following code snippet works
ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", "/K ping stackoverflow.com");
What's wrong here?
a source to share
I am assuming that the $ PATH variable will not be defined to include the path to MySQL in the environment ASP.NET is running in (either for that user or for ASP.NET clears the environment).
Two attempts:
- (might work) Make sure the mysql directory is added to the global path and not just to the PATH for a specific user. (note: this will require at least a reboot of IIS, perhaps a reboot will take effect).
- Use the full path to mysqldump (you will need to do something to quote or avoid spaces in the path).
a source to share
Example for mysqldump
Private sw As IO.StreamWriter
Public Sub ExecuteBackup()
Try
Dim mysqlDump As New Process
mysqlDump.StartInfo.FileName = "PahtToMysqlDump\mysqldump.exe"
mysqlDump.StartInfo.Arguments = "--user=myuser --password=mypassword --routines --all-databases"
mysqlDump.StartInfo.RedirectStandardOutput = True
mysqlDump.StartInfo.CreateNoWindow = True
mysqlDump.StartInfo.UseShellExecute = False
Console.WriteLine("Arguments:")
Console.WriteLine("mysqldump.exe " & mysqlDump.StartInfo.Arguments)
sw = New IO.StreamWriter("backup.sql")
AddHandler mysqlDump.OutputDataReceived, AddressOf mysqlDumpNewData
mysqlDump.Start()
mysqlDump.BeginOutputReadLine()
mysqlDump.WaitForExit()
sw.Close()
Console.WriteLine("Backup completed")
Catch ex As Exception
Console.WriteLine("BackupEngine.ExecuteBackup.", ex)
End Try
End Sub
Private Sub mysqlDumpNewData(sender As Object, e As DataReceivedEventArgs)
Dim line = e.Data
If line IsNot Nothing Then
sw.WriteLine(line)
If line.StartsWith("USE") Then
Dim ln = line.Split("`")(1)
Console.WriteLine("Database : " & ln.ToUpper)
End If
If line.StartsWith("CREATE TABLE") Then
Dim ln = line.Split("`")(1)
Console.WriteLine(" Table : " & ln.ToUpper)
End If
End If
End Sub
a source to share