the main purpose of having backup function is whenever our database crashes,prevent from system failure..by keeping backups we can restore them at necessary time.here is the backup function which returns Boolean value.it checks for current date and time for purpose of naming the backup folder.then we can easily find which is the latest backup according to date and time.
Here i have given path to mysqldump in MySQL server which installed in my local disk C.if it is changing machine to machine we can configure it using app.config file.I have given that code segment also.you can implement this method to create backups and you should call this method in necessary events by passing MySQL credentials like username ,password, host name, database name.
public static bool Backup(string user, string password, string host, string database, string path)
{
try
{
DateTime backupTime = DateTime.Now;
int year = backupTime.Year;
int month = backupTime.Month;
int day = backupTime.Day;
int hour = backupTime.Hour;
int minute = backupTime.Minute;
int second = backupTime.Second;
int ms = backupTime.Millisecond;
String tmestr;
tmestr = path + "-" + year + "-" + month + "-" + day + "-" + hour + "-" + minute + ".sql";
StreamWriter file = new StreamWriter(tmestr);
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = @"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqldump";
string cmd = string.Format(@"-u{0} -p{1} -h{2} {3} ", user, password, host, database, tmestr /*"backup.sql"*/);
proc.Arguments = cmd;
proc.RedirectStandardInput = false;
proc.RedirectStandardOutput = true;
proc.UseShellExecute = false;
proc.CreateNoWindow = true;
Process p = Process.Start(proc);
string res;
res = p.StandardOutput.ReadToEnd();
file.WriteLine(res);
p.WaitForExit();
file.Close();
return true;
}
catch (IOException ex)
{
return false;
}
}
How to configure path using app.config is like below.inside app.config we should give all the credentials needed to make backup.and then we can access it inside our .cs class.
<connectionStrings>
<add name="cecb_host" connectionString="localhost"
providerName="MySql.Data.MySqlClient" />
<add name="cecb_password" connectionString="123"/>
<add name="cecb_user" connectionString="root"/>
<add name="cecb_database" connectionString="cecb"/>
<add name="cecb_path" connectionString="C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysqldump"/>
</connectionStrings>
accessing app.config connectionstrings through .cs class
string connectionString_host = ConfigurationManager.ConnectionStrings["cecb_host"].ConnectionString;
This is the restore method of database backups.here i have given the option of selecting backup file from local disk and open it.when it is opened it automatically execute through the MySQL server like manually script running.so it is very important and easy method to restoring backups.here what we do inside the implementation is run the script through MySQL server which is installed in local disk.that's why we are giving the path for MySQL.exe. here also we can configure path through app.config file for purpose of changing the path.
public static void restore(string user, string password, string host, string database, string path)
{
try{
StreamReader file = new StreamReader(path);
ProcessStartInfo proc = new ProcessStartInfo();
string cmdArgs = string.Format(@"-u{0} -p{1} -h{2} {3}", user, password, host, database);
proc.FileName = "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe";
proc.RedirectStandardInput = true;
proc.RedirectStandardOutput = false;
proc.Arguments = cmdArgs;
proc.UseShellExecute = false;
proc.CreateNoWindow = true;
Process p = Process.Start(proc);
string res = file.ReadToEnd();
file.Close();
p.StandardInput.WriteLine(res);
p.Close();
}
catch(Exception e)
{
MessageBox.Show("Restoring process failed !", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
Here i have given path to mysqldump in MySQL server which installed in my local disk C.if it is changing machine to machine we can configure it using app.config file.I have given that code segment also.you can implement this method to create backups and you should call this method in necessary events by passing MySQL credentials like username ,password, host name, database name.
public static bool Backup(string user, string password, string host, string database, string path)
{
try
{
DateTime backupTime = DateTime.Now;
int year = backupTime.Year;
int month = backupTime.Month;
int day = backupTime.Day;
int hour = backupTime.Hour;
int minute = backupTime.Minute;
int second = backupTime.Second;
int ms = backupTime.Millisecond;
String tmestr;
tmestr = path + "-" + year + "-" + month + "-" + day + "-" + hour + "-" + minute + ".sql";
StreamWriter file = new StreamWriter(tmestr);
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = @"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqldump";
string cmd = string.Format(@"-u{0} -p{1} -h{2} {3} ", user, password, host, database, tmestr /*"backup.sql"*/);
proc.Arguments = cmd;
proc.RedirectStandardInput = false;
proc.RedirectStandardOutput = true;
proc.UseShellExecute = false;
proc.CreateNoWindow = true;
Process p = Process.Start(proc);
string res;
res = p.StandardOutput.ReadToEnd();
file.WriteLine(res);
p.WaitForExit();
file.Close();
return true;
}
catch (IOException ex)
{
return false;
}
}
How to configure path using app.config is like below.inside app.config we should give all the credentials needed to make backup.and then we can access it inside our .cs class.
<connectionStrings>
<add name="cecb_host" connectionString="localhost"
providerName="MySql.Data.MySqlClient" />
<add name="cecb_password" connectionString="123"/>
<add name="cecb_user" connectionString="root"/>
<add name="cecb_database" connectionString="cecb"/>
<add name="cecb_path" connectionString="C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysqldump"/>
</connectionStrings>
accessing app.config connectionstrings through .cs class
string connectionString_host = ConfigurationManager.ConnectionStrings["cecb_host"].ConnectionString;
This is the restore method of database backups.here i have given the option of selecting backup file from local disk and open it.when it is opened it automatically execute through the MySQL server like manually script running.so it is very important and easy method to restoring backups.here what we do inside the implementation is run the script through MySQL server which is installed in local disk.that's why we are giving the path for MySQL.exe. here also we can configure path through app.config file for purpose of changing the path.
public static void restore(string user, string password, string host, string database, string path)
{
try{
StreamReader file = new StreamReader(path);
ProcessStartInfo proc = new ProcessStartInfo();
string cmdArgs = string.Format(@"-u{0} -p{1} -h{2} {3}", user, password, host, database);
proc.FileName = "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe";
proc.RedirectStandardInput = true;
proc.RedirectStandardOutput = false;
proc.Arguments = cmdArgs;
proc.UseShellExecute = false;
proc.CreateNoWindow = true;
Process p = Process.Start(proc);
string res = file.ReadToEnd();
file.Close();
p.StandardInput.WriteLine(res);
p.Close();
}
catch(Exception e)
{
MessageBox.Show("Restoring process failed !", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
No comments:
Post a Comment