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

Summary of 9 excellent code comparison tools recommended under Linux

When we write code, we often need to know the dif...

CentOS method to modify the default ssh port number example

The default ssh port number of Linux servers is g...

Tutorial on using Webpack in JavaScript

Table of contents 0. What is Webpack 1. Use of We...

A brief discussion on the corresponding versions of node node-sass sass-loader

Table of contents The node version does not corre...

About front-end JavaScript ES6 details

Table of contents 1. Introduction 1.1 Babel Trans...

Docker pull image and tag operation pull | tag

I re-read the source code of the Fabric project a...

What are the new features of Apache Spark 2.4, which will be released in 2018?

This article is from the Apache Spark Meetup held...

Mysql | Detailed explanation of fuzzy query using wildcards (like,%,_)

Wildcard categories: %Percent wildcard: indicates...

Border-radius IE8 compatible processing method

According to canisue (http://caniuse.com/#search=...

HTML table tag tutorial (19): row tag

The attributes of the <TR> tag are used to ...

How to modify the "Browse" button of the html form to upload files

Copy code The code is as follows: <!DOCTYPE HT...

CSS3 realizes text relief effect, engraving effect, flame text

To achieve this effect, you must first know a pro...

Shell script settings to prevent brute force ssh

The shell script sets access control, and the IP ...