Showing posts with label asp.net mysql backup. Show all posts
Showing posts with label asp.net mysql backup. Show all posts

Tuesday, August 24, 2010

Backup mysql database using batch file and asp.net

Create a batch file with the following code

set pathchoice=%1%


"C:\wamp\MySQL\bin\mysqldump" -u root dbname -p password > %pathchoice%\filename.sql

and then you need to create a aspx file to run this bat file:here is the code for backup file

protected void ExecuteBatchFile(string batchFileName, string[] argumentsToBatchFile)
{
string argumentsString = string.Empty;
try
{
//Add up all arguments as string with space separator between the arguments
if (argumentsToBatchFile != null)
{
for (int count = 0; count < argumentsToBatchFile.Length; count++)
{
argumentsString += " ";
argumentsString += argumentsToBatchFile[count];
//argumentsString += "\"";
}
}

//Create process start information
System.Diagnostics.ProcessStartInfo DBProcessStartInfo = new System.Diagnostics.ProcessStartInfo(batchFileName, argumentsString);

//Redirect the output to standard window
DBProcessStartInfo.RedirectStandardOutput = true;

//The output display window need not be falshed onto the front.
DBProcessStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
DBProcessStartInfo.UseShellExecute = false;

//Create the process and run it
System.Diagnostics.Process dbProcess;
dbProcess = System.Diagnostics.Process.Start(DBProcessStartInfo);

//Catch the output text from the console so that if error happens, the output text can be logged.
System.IO.StreamReader standardOutput = dbProcess.StandardOutput;

/* Wait as long as the DB Backup or Restore or Repair is going on.
Ping once in every 2 seconds to check whether process is completed. */
while (!dbProcess.HasExited)
dbProcess.WaitForExit(2000);

if (dbProcess.HasExited)
{
string consoleOutputText = standardOutput.ReadToEnd();
//TODO - log consoleOutputText to the log file.
}
}
// Catch the SQL exception and throw the customized exception made out of that
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
// ExceptionManager.Publish(ex);
// throw SQLExceptionClassHelper.GetCustomMsSqlException(ex.Number);
}
// Catch all general exceptions

}
protected void Button1_Click(object sender, EventArgs e)
{
string[] names = new string[1];
names[0] = TextBox1.Text;


ExecuteBatchFile("E:\\b.bat", names);
}