Reputation: 2696
I have a procedure for backing up MySQL database.And also i have different MySQL servers. This procedure works on some of MySQL servers.But on some of servers it won't works proper and create a backup file with the size of 1kb.
public void DatabaseBackup(string ExeLocation, string DBName)
{
try
{
string tmestr = "";
tmestr = DBName + "-" + DateTime.Now.ToString("hh.mm.ss.ffffff") + ".sql";
tmestr = tmestr.Replace("/", "-");
tmestr = "c:/" + tmestr;
StreamWriter file = new StreamWriter(tmestr);
ProcessStartInfo proc = new ProcessStartInfo();
string cmd = string.Format(@"-u{0} -p{1} -h{2} {3}", "uid", "pass", "host", DBName);
proc.FileName = ExeLocation;
proc.RedirectStandardInput = false;
proc.RedirectStandardOutput = true;
proc.Arguments = cmd;
proc.UseShellExecute = false;
proc.CreateNoWindow = true;
Process p = Process.Start(proc);
string res;
res = p.StandardOutput.ReadToEnd();
file.WriteLine(res);
p.WaitForExit();
file.Close();
}
catch (IOException ex)
{
}
}
Can any one tell me what is the problem and how can i solve it.
Upvotes: 3
Views: 2510
Reputation: 2696
Finally i got the answer. We need SELECT and LOCK_TABLE privilege on MySQL user or database on which we want backup. After setting these privilege on database i am able to take full backup of that database.
Upvotes: 2
Reputation: 2612
Where is the Backup Statement?
Here is the best way to backup your database :
private void BackupDatabase()
{
string time = DateTime.Now.ToString("dd-MM-yyyy");
string savePath = AppDomain.CurrentDomain.BaseDirectory + @"Backups\"+time+"_"+saveFileDialogBackUp.FileName;
if (saveFileDialogBackUp.ShowDialog() == DialogResult.OK)
{
try {
using (Process mySqlDump = new Process())
{
mySqlDump.StartInfo.FileName = @"mysqldump.exe";
mySqlDump.StartInfo.UseShellExecute = false;
mySqlDump.StartInfo.Arguments = @"-u" + user + " -p" + pwd + " -h" + server + " " + database + " -r \"" + savePath + "\"";
mySqlDump.StartInfo.RedirectStandardInput = false;
mySqlDump.StartInfo.RedirectStandardOutput = false;
mySqlDump.StartInfo.CreateNoWindow = true;
mySqlDump.Start();
mySqlDump.WaitForExit();
mySqlDump.Close();
}
}
catch (IOException ex)
{
MessageBox.Show("Connot backup database! \n\n" + ex);
}
MessageBox.Show("Done! database backuped!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Good luck!
Upvotes: 0