I have used Visual Studio 2005 - meaning .NET 2.0. And it's a commandline program - single file - program.cs.
I will now take you through the code - lets start with Main:
static void Main(string[] args)
{
AMS.Profile.Xml oXml = new AMS.Profile.Xml(args[0]);
bool fMySQL = oXml.GetValue("MySQL", "Backup", false);
if (fMySQL)
{
RunMySQL(oXml);
}
bool fMSSQL = oXml.GetValue("MSSQL", "Backup", false);
if (fMSSQL)
{
RunMSSQL(oXml);
}
}
We are using the AMS.Profile component to read the conf.xml file and first check if we should run the backup at all.
Lets dive into RunMySQL - cause the MSSQL looks pretty much the same actually.
public static void RunMySQL(AMS.Profile.Xml oXml)
{
string sServer = oXml.GetValue("MySQL", "ServerName", "localhost");
string sUserId = oXml.GetValue("MySQL", "Userid", "root");
string sPwd = oXml.GetValue("MySQL", "Password", "");
string sOutPath = GetOutPath(oXml,"MySQL");
EnsureDirectory(new System.IO.DirectoryInfo(sOutPath));
//Retrieve all databases to backup
//Retrieve all databases...
string sConn = "Database=mysql;Data Source=" + sServer + ";User id=" + sUserId + ";Password=" + sPwd;
MySql.Data.MySqlClient.MySqlConnection oConn = new MySql.Data.MySqlClient.MySqlConnection(sConn);
try
{
oConn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
Console.WriteLine(ex.Message);
return;
}
string sExcluded = oXml.GetValue("MySQL", "ExcludedDatabases", "");
...
...
...
We start by retrieving all configuration settings, we then create a connection to the MySQL server bases on those settings. Now lets list all available databases - and put them into an array:
MySql.Data.MySqlClient.MySqlCommand oCommand = oConn.CreateCommand();
oCommand.CommandText = "show databases";
MySql.Data.MySqlClient.MySqlDataReader oReader = oCommand.ExecuteReader();
List<string> DatabaseNamesToBackup = new List<string>();
while (oReader.Read())
{
string sName = oReader[0].ToString();
bool fFoundInExcludeList = false;
foreach (string s in sExcluded.Split(','))
{
if (s.Trim().ToLower() == sName.ToLower().Trim())
fFoundInExcludeList = true;
}
if (fFoundInExcludeList == false && sName.ToLower().Trim() != "information_schema")
DatabaseNamesToBackup.Add(sName.ToLower().Trim());
}
oReader.Close();
So now we have all databases to be backup in our DatabaseNamesToBackup array.
Now lets do it
//Now run our command line file...
foreach (string sOne in DatabaseNamesToBackup)
{
string sOut = sOutPath + sOne + ".txt";
string sArgs = String.Format("{0} {1} {2} {3} {4}", sServer, sUserId, sPwd, sOne, sOut );
//Start exe...
System.Diagnostics.ProcessStartInfo oInfo = new System.Diagnostics.ProcessStartInfo("backmysql.cmd",sArgs);
oInfo.UseShellExecute = false;
oInfo.ErrorDialog = false;
oInfo.CreateNoWindow = true;
try
{
Process p = System.Diagnostics.Process.Start(oInfo);
//
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
For each database name in our DatabaseNamesToBackup array we basically create a command line (calling backmysql.cmd) with correct parameters.
And that's it. Not many lines of code - not even hard to understand. But oh so useful tool!