C # implement MySQL command line backup and recovery,
There are many tools available for MySQL database backup. In the past two days, I wrote a tool that uses C # To Call MYSQL's mysqldump command to complete MySQL database backup and recovery.
How to back up a MySQL database using the mysqldump command
mysqldump -hhostname -uusername -ppassword databasename > backupfile.sql
Directly compress and back up the MySQL database
mysqldump -hhostname -uusername -ppassword databasename | gzip > backupfile.sql.gz
Back up a MySQL database table
mysqldump -hhostname -uusername -ppassword databasename specific_table1 specific_table2 > backupfile.sql
Back up multiple MySQL databases at the same time
mysqldump -hhostname -uusername -ppassword –databases databasename1 databasename2 databasename3 > multibackupfile.sql
Back up database structures only
mysqldump –no-data –databases databasename1 databasename2 databasename3 > structurebackupfile.sql
Back up all databases on the server
mysqldump –all-databases > allbackupfile.sql
Command for restoring MySQL database
mysql -hhostname -uusername -ppassword databasename < backupfile.sql
Restore a compressed MySQL database
gunzip < backupfile.sql.gz | mysql -uusername -ppassword databasename
Transfer database to new server
mysqldump -uusername -ppassword databasename | mysql –host=*.*.*.* -C databasename
Use C # To perform MYSQL backup and recovery, mainly using C # To execute external programs.
Below is part of C # source code
/// <Summary> /// back up the database to a specific directory /// </summary> /// <param name = "binfolderpath"> binfolder, used in the mysqldump.exe file </param> // /<param name = "server"> server </param> // <param name = "user"> user name </param> // <param name = "pass"> password </param> /// <param name = "db"> name of the database to be backed up </param> /// <param name = "backupfile"> backup to what file </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"> binfolder, used in the mysqldump.exe file </param>/ // <param name = "server"> server </param> // <param name = "user"> user name </param> // <param name = "pass "> password </param> /// <param name =" db "> name of the database to be backed up </param> /// <param name =" backupfile "> restore </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 ;}
A few lines of code can be used to implement the database backup and recovery function. C # The main principle of implementing MySQL command line backup and recovery is to use C # To execute external programs. Other implementation methods are also available for you to have a better understanding.