C# implements MySQL command line backup and recovery

C# implements MySQL command line backup and recovery

There are many tools available for backing up MySQL databases. In the past two days, I wrote a small tool that uses C# to call MYSQL's mysqldump command to complete the backup and recovery of MySQL databases.

Let's first talk about how to use the mysqldump command to back up the MySQL database.

mysqldump -hhostname -uusername -ppassword databasename > backupfile.sql

Compress and back up the MySQL database directly

mysqldump -hhostname -uusername -ppassword databasename | gzip > backupfile.sql.gz

Back up a table(s) in the MySQL database

mysqldump -hhostname -uusername -ppassword databasename specific_table1 specific_table2 > backupfile.sql

Back up multiple MySQL databases simultaneously

mysqldump -hhostname -uusername -ppassword –databases databasename1 databasename2 databasename3 > multibackupfile.sql

Back up only the database structure

mysqldump –no-data –databases databasename1 databasename2 databasename3 > structurebackupfile.sql

Back up all databases on the server

mysqldump –all-databases > allbackupfile.sql

Command to restore MySQL database

mysql -hhostname -uusername -ppassword databasename < backupfile.sql

Restoring a compressed MySQL database

gunzip < backupfile.sql.gz | mysql -uusername -ppassword databasename

Transfer the database to the new server

mysqldump -uusername -ppassword databasename | mysql –host=*.*.*.* -C databasename

Using C# to operate MYSQL backup and recovery is mainly achieved by using C# to execute external programs

Below is some C# source code

/// <summary>
/// Back up the database to a specific directory/// </summary>
/// <param name="binfolderpath">Bin folder directory, used to get the mysqldump.exe file</param>
/// <param name="server">Server</param>
/// <param name="user">User name</param>
/// <param name="pass">Password</param>
/// <param name="db">Database name to be backed up</param>
/// <param name="backupfile">What file to back up to</param>
/// <returns></returns>
public static bool BackupDB(string binfolderpath, string server, string character, string user, string pass, string db, string backupfile)
{
string command = string.Format("mysqldump.exe --quick --host=\"{0}\" --default-character-set=\"{1}\" --lock-tables --verbose --force --port=3306 --user=\"{2}\" --password=\"{3}\" \"{4}\" -r \"{5}\"",server, character.Trim().ToLower(), user, pass, db, backupfile);
  StartCmd(binfolderpath + @"\", command);
  if (File.Exists(backupfile))
  {
    return true;
  }
  else
  {
    return false;
  }
}
/// <summary>
/// Restore the specified database to the specified file/// </summary>
/// <param name="binfolderpath">Bin folder directory, used to get the mysqldump.exe file</param>
/// <param name="server">Server</param>
/// <param name="user">User name</param>
/// <param name="pass">Password</param>
/// <param name="db">Database name to be backed up</param>
/// <param name="backupfile">SQL file to be restored</param>
/// <returns></returns>
public static bool RestoreDB(string binfolderpath, string character, string server, string user, string pass, string db, string restorefile)
{
string command = string.Format("mysql.exe --host=\"{0}\" --default-character-set=\"{1}\" --port=3306 --user=\"{2}\" --password=\"{3}\" \"{4}\"<\"{5}\"", server, character.Trim().ToLower(), user, pass, db, restorefile);
   StartCmd(binfolderpath + @"\", command);
   return true;
}

The database backup and recovery function can be implemented with just a few lines of code. The main principle of implementing MySQL command line backup and recovery in C# is to use C# to execute external programs. There are other implementation methods. You can learn more about them.

You may also be interested in:
  • How to connect to MySQL database in C#
  • C# connect to MySQL detailed tutorial
  • Two simple code examples for connecting C# to MySQL
  • How to connect to mysql in C# [Based on vs2010]
  • How to efficiently read and write massive amounts of data in MySQL using C#
  • Detailed explanation of efficient reading and writing of large amounts of data in MySQL using C#
  • C# Batch insert of large data in several databases (SqlServer, Oracle, SQLite and MySql)
  • How to call MySQL stored procedure in C#
  • C# connect to mysql database complete example
  • C# implements MysqlHelper instance to operate MySql data layer class
  • Two ways to add data to MySQL

<<:  Detailed explanation of several ways of communication between Linux user state and kernel state

>>:  How to use module fs file system in Nodejs

Recommend

Detailed installation and configuration of Subversion (SVN) under Ubuntu

If you are a software developer, you must be fami...

10 excellent Web UI libraries/frameworks

1. IT Mill Toolkit IT Mill Toolkit is an open sou...

Understanding the CSS transform-origin property

Preface I recently made a fireworks animation, wh...

CSS stacking and z-index example code

Cascading and Cascading Levels HTML elements are ...

Classification of web page color properties

Classification of color properties Any color can ...

Implementation of Docker private library

Installing and deploying a private Docker Registr...

SQL Practice Exercise: Online Mall Database Product Category Data Operation

Online shopping mall database-product category da...

mysql command line script execution example

This article uses an example to illustrate the ex...

How to implement simple data monitoring with JS

Table of contents Overview first step Step 2 Why ...

Vue implements video upload function

This article example shares the specific code of ...

Detailed graphic tutorial on installing and uninstalling Tomcat8 on Linux

[ Linux installation of Tomcat8 ] Uninstall Tomca...

Detailed explanation of MySQL custom functions and stored procedures

Preface This article mainly introduces the releva...

Windows 10 installation vmware14 tutorial diagram

Software Download Download software link: https:/...