How To Backup MySql Database Through Code


How To Backup MySql Database Through Code

On our first two tutorials about Modern Project design we manage to discuss on how to design login screen and connect our project to a MySql database, now I will show you on how to Backup MySql Database through code.  To backup MySql database we need to add a external application on our project which is called MySqlDump.

The mysqldump client is a backup program originally written by Igor Romanenko. It can be used to dump a database or a collection of databases for backup or transfer to another SQL server
MySQL

We can use MySqlDump as it is by using a command prompt and pass several parameters like –host (server name or IP), –port (mysql Port), –user (database user name), –password (database password) , database name and we also need to specify from where we are going to save the backup by passing a filename.

mysqldump --host=[server name or IP] --port=[port no] --user=[user name] --password=[password] [database name]  > [filname]

backup-mysql-database

We need to add mysqldump exe file inside our Resources folder that we added on our project.  And inside our code we need to create a new function call DoBackup. DoBackup function will contain all the codes that will create the batch file and executing it.

On this code we are creating a batch file using StreamWriter.  The batch file will execute mysqldump and pass the parameters needed to make a backup.

StreamWriter _sw = File.CreateText(_batfile);
_sw.WriteLine(@"""" + Application.StartupPath + @"\Resources\mysqldump"" --host=" + Settings.Default.Server + " --port=" + Settings.Default.Port + " --user=" + Settings.Default.Username + " --password=" + Settings.Default.Password + " " + Settings.Default.Database + " --routines > " + @"""" + _sqlfile + @"""");
_sw.Close();
_sw.Dispose();

Once we have created the batch file we now need to execute it using a Process.

Process _bu = new Process();
_bu.StartInfo.FileName = _batfile;
_bu.StartInfo.CreateNoWindow = true;
_bu.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
_bu.StartInfo.RedirectStandardError = true;
_bu.StartInfo.UseShellExecute = false;
_bu.Start();

while (!_bu.HasExited)
{
//Application.DoEvents();
}

_bu.Dispose();
_bu = null;

if (!File.Exists(_sqlfile))
{
_return = false;
}
else
{
File.Copy(_sqlfile, filename);
}

Once we have the DoBackup function we can just call it and pass a filename parameter. We can use SaveFileDialog to specify the save directory and filename.

SaveFileDialog _dia = new SaveFileDialog();
_dia.Title = "Backup Database";
_dia.Filter = "SQL files (*.sql)|*.sql";
_dia.FileName = _filename;
_dia.DefaultExt = "*.sql";
_dia.InitialDirectory = string.Empty;
if (_dia.ShowDialog() == DialogResult.OK && !String.IsNullOrEmpty(_dia.FileName.Trim()))
{
_filename = _dia.FileName;
}
else
{
_close = true;
}

Update (12-October-2015)

You can now download the file that I created on this tutorial here.

Liked it? Take a second to support denricdenise on Patreon!

You may also like...