以前一直做android客户端的项目,根本没有开发asp.net mvc的开发,现阶段做了一个模块,参数设置,以及数据库的备份与还原。其需求如下:

参数设置    本项参数设置为对自动数据备份进行设置,管理员可以对系统自动备份文件所存放的位置、自动备份的周期、自动备份的时间进行设定,并且可以开启或关闭自动备份功能。

数据备份    在本模块中,管理员可以通过录入系统备份文件的目标位置,对系统数据库进行备份,保证系统数据的安全性。每次备份都会记录在系统中,包含备份时间、备份位置及备份操作者。

数据还原    管理员通过选择曾经进行备份的数据库文件,在进行校对后,系统会进行数据库还原工作,本项功能用以数据灾难恢复,还原信息也会记录至系统中。 今天主要给大家认识一下,如何通过SQL语句实现MYSql数据库的备份与还原。 其实代码很简单,下来我们就直接看如何实现吧。 首先你在项目中需要导入两个Dll包,Mysql.Data.dll和Mysqlbackup.dll. 下来来看代码

1.databackup(备份)

void Backup()
{
string constr = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\MyDumpFile.sql";
MySqlBackup mb = new MySqlBackup(constr);
mb.ExportInfo.FileName = file;
mb.Export();
}

2.dataRestore(还原)

void Restore()
{
string constr = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\MyDumpFile.sql";
MySqlBackup mb = new MySqlBackup(constr);
mb.ImportInfo.FileName = file;
mb.Import();
}

3. Export and Compress as Zip File(出口和压缩,压缩文件)

void Backup()
{
string constr = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\MyDumpFile.sql";
MySqlBackup mb = new MySqlBackup(constr);
mb.ExportInfo.FileName = file;
mb.ExportInfo.ZipOutputFile = true;
mb.Export();
}

4. Select tables to backup(选择要备份表)

void BackupTables()
{
string constr = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\MyDumpFile.sql";
MySqlBackup mb = new MySqlBackup(constr);
mb.ExportInfo.FileName = file;
mb.ExportInfo.TablesToBeExported = new string[] { "member", "activity", "season" };
mb.Export();
}

5.Custom columns and rows backup conditions(自定义列和行备份条件)

void BackupTablesCustomSQL()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("member", "SELECT * FROM `member` WHERE `membertype` = 1;");
dic.Add("payment", "SELECT `id`,`total` FROM `payment`;");
dic.Add("activity", "SELECT * FROM `activity` a INNER JOIN `season` s ON a.`seasonid` = s.`id` WHERE s.`name` = 'Spring';");
string constr = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\MyDumpFile.sql";
MySqlBackup mb = new MySqlBackup(constr);
mb.ExportInfo.FileName = file;
mb.ExportInfo.TableCustomSql = dic;
mb.Export();
}

6.Backup with Encryption(备份和加密)

void BackupEncrypt()
{
string constr = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\MyDumpFile.sql";
MySqlBackup mb = new MySqlBackup(constr);
mb.ExportInfo.FileName = file;
mb.ExportInfo.EnableEncryption = true;
mb.ExportInfo.EncryptionKey = "my secret password";
mb.Export();
}

7.Resotre with Decryption(还原与解密)

void RestoreDecrypt()
{
string constr = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\MyDumpFile.sql";
MySqlBackup mb = new MySqlBackup(constr);
mb.ImportInfo.FileName = file;
mb.ImportInfo.EnableEncryption = true;
mb.ImportInfo.EncryptionKey = "my secret password";
mb.Import();
}

8.Encrypt & Decrypt a SQL Dump File(加密和解密SQL转储文件)

void EncryptDecryptDumpFile()
{
MySqlBackup mb = new MySqlBackup();
string file1 = "C:\\MyDumpFileOld.sql";
string file2 = "C:\\MyDumpFileNew.sql";
string file3 = "C:\\MyDumpFileBackToOri.sql";
string password = "my secret password";
mb.EncryptSqlDumpFile(file1, file2, password);
mb.DecryptSqlDumpFile(file2, file3, password);
}

9.Backup Table Structures without rows and reset auto-increment to 1 Collapse | Copy Code (自动增量备份表结构没有行和重置为1 |复制代码崩溃)

void BackupNoRows()
{
string constr = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\MyDumpFile.sql";
MySqlBackup mb = new MySqlBackup(constr);
mb.ExportInfo.FileName = file;
mb.ExportInfo.ExportRows = false;
mb.ExportInfo.ResetAutoIncrement = true;
mb.Export();
}

10.Full List of Settings for Export (Backup) (的完整列表设置出口)

void BackupNoRows()
{
string constr = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\MyDumpFile.sql";
MySqlBackup mb = new MySqlBackup(constr);
mb.ExportInfo.FileName = file;
mb.ExportInfo.AddCreateDatabase = true;
mb.ExportInfo.AsynchronousMode = true;
mb.ExportInfo.AutoCloseConnection = true;
mb.ExportInfo.CalculateTotalRowsFromDatabase = true;
mb.ExportInfo.EnableEncryption = true;
mb.ExportInfo.EncryptionKey = "my secret password";
mb.ExportInfo.ExportEvents = true;
mb.ExportInfo.ExportFunctions = true;
mb.ExportInfo.ExportRows = true;
mb.ExportInfo.ExportStoredProcedures = true;
mb.ExportInfo.ExportTableStructure = true;
mb.ExportInfo.ExportTriggers = true;
mb.ExportInfo.ExportViews = true;
mb.ExportInfo.MaxSqlLength = ;
mb.ExportInfo.RecordDumpTime = true;
mb.ExportInfo.ResetAutoIncrement = true;
mb.ExportInfo.TableCustomSql = //Shown in example 4
mb.ExportInfo.TablesToBeExported = //Shown in example 3
mb.ExportInfo.ExportRows = false;
mb.ExportInfo.ResetAutoIncrement = true;
mb.ExportInfo.ZipOutputFile = false;
mb.Export();
}

11.Full List of Settings for Import (设置导入的完整列表)

void Restore()
{
string constr = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\MyDumpFile.sql";
MySqlBackup mb = new MySqlBackup(constr);
mb.ImportInfo.FileName = file;
mb.ImportInfo.AsynchronousMode = true;
mb.ImportInfo.AutoCloseConnection = true;
mb.ImportInfo.EnableEncryption = true;
mb.ImportInfo.EncryptionKey = "my secret password";
mb.ImportInfo.IgnoreSqlError = true;
mb.ImportInfo.SetTargetDatabase("MyNewDatabase", ImportInformations.CharSet.latin1);
mb.Import();
}

12.Export BLOB data and save as files on local drive (出口BLOB数据并保存为本地驱动器上的文件)

private void BackupEncrypt()
{
string folder = "C:\\exportedfiles";
string table = "upload";
string column_Blob = "blobdata";
string column_FileName = "filename";
string column_FileSize = "filesize";
string con = "server=localhost;user=root;pwd=qwerty;database=member;";
MySqlBackup mb = new MySqlBackup(con);
mb.ExportBlobAsFile(folder, table, column_Blob, column_FileName, column_FileSize);
}

13.Get all tables' name from database (从数据库获取所有表的名字)

private string[] GetTableNames()
{
string con = "server=localhost;user=root;pwd=qwerty;database=test;";
MySqlBackup mb = new MySqlBackup(con);
return mb.DatabaseInfo.TableNames;
}

14.Get Create Table sql statement for specific table (为特定的表创建表的sql语句)

private string GetCreateTable(string tableName)
{
string con = "server=localhost;user=root;pwd=qwerty;database=test;";
MySqlBackup mb = new MySqlBackup(con);
return mb.DatabaseInfo.Tables[tableName].CreateTableSql;
}

下面是我项目中写的备份与还原方法

//备份newtest database 里面所有的表


public void DataBackup()
{
try
{
//var time = DateTime.Now;
string file = "D:\\mybackup.sql";//备份的路径
string conn = "server=172.18.0.3;database=newtest;uid=root;pwd=123;";
MySqlBackup mb = new MySqlBackup(conn);
mb.ExportInfo.FileName = file;
mb.ExportInfo.EnableEncryption = true;//加密
mb.ExportInfo.EncryptionKey = "1234";//加密密钥
mb.ExportInfo.ZipOutputFile = true;//压缩
mb.Export();

}
catch (Exception e)
{
Console.WriteLine("备份失败,请检查");
}


}

 

//还原newtest database 里面所有的表(这里不要解压,解压是自动的)

 public void DataRestore()
{
try
{
string file = "D:\\mybackup.zip";//找到备份的路径所在的zip文件,进行还原恢复。
string conn = "server=172.18.0.3;user=root;pwd=123;database=newtest;";
MySqlBackup mb = new MySqlBackup(conn);
mb.ImportInfo.FileName = file;
mb.ImportInfo.EnableEncryption = true;//解密
mb.ImportInfo.EncryptionKey = "";//解密密钥
mb.Import();
}
catch (Exception e)
{
Console.WriteLine("恢复失败,请检查");
}
}

本文原创,请尊重劳动成果。也真诚希望和大家相互讨论,相互学习,相互提高。

如何用SQL语句实现Mysql数据库的备份与还原的更多相关文章

  1. 使用SQL语句修改Mysql数据库字符集的方法

    使用SQL语句修改Mysql数据库字符集的方法   修改库: alter database [$database] character set [$character_set] collate [$c ...

  2. mysql数据库的备份和还原的总结

    mysql数据库的备份和还原的总结 (来自一运维同事的总结) 1. 备份方式: 热备:数据库在线进行备份,不影响读和写的在线备份方式! 温备:数据库在线进行备份,对表备份时先锁定写操作,仅可以执行读操 ...

  3. MySQL数据库的备份、还原、迁移

    一.单库备份与还原 1.远程连接MySQL数据库 D:\mysql-5.7.14-winx64\bin>mysql -h192.168.2.201 -uroot -pcnbi2018 参数说明: ...

  4. Linux centosVMware mysql用户管理、常用sql语句、mysql数据库备份恢复

    一.mysql用户管理 grant all on *.* to 'user1'@‘127.0.0.1’ identified by 'mimA123'; 创建user1用户 使用user1登录 /us ...

  5. [知了堂学习笔记]_Java代码实现MySQL数据库的备份与还原

    通常在MySQL数据库的备份和恢复的时候,多是采用在cmd中执行mysql命令来实现. 例如: mysqldump -h127.0.0.1 -uroot -ppass test > d:/tes ...

  6. mysql数据库的备份和还原

    mysql数据库的备份命令:mysqldump -u root  -p 要备份的现有数据库名  >  备份后的sql文件名.sql,例如:  mysqldump -u root -p  heal ...

  7. SQL Serever学习12——数据库的备份和还原

    公司的服务器奔溃了,事先没相应的保护措施,使得公司遭到了较大损失,为了以后不再出现类似事件,在系统中引入备份机制,使得数据库被破坏后损失降到最低. 数据的导出和导入 数据转换服务 数据转换服务DTS( ...

  8. Java代码实现MySQL数据库的备份与还原

    通常在MySQL数据库的备份和恢复的时候,多是采用在cmd中执行mysql命令来实现. 例如: mysqldump -h127.0.0.1 -uroot -ppass test > d:/tes ...

  9. MySQL用户管理、常用sql语句、MySQL数据库备份恢复

    1.MySQL用户管理 给远程登陆用户授权:grant all on *.* to 'user1'@'127.0.0.1' identified by '123456' (这里的127.0.0.1是指 ...

随机推荐

  1. 锋利的JQuery 学习笔记

    第一章                认识JQuery ·页面加载事件(可以写多个ready())$(document).ready(function(){alert(“hello world”);} ...

  2. 打不开tomcat

    org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 59; schema_reference.4: 无法读取方案文档 'http: ...

  3. 移动端div移动

    <!doctype html> <html lang="en"> <head> <meta http-equiv="Conten ...

  4. Nginx + uwsgi

    1. 安装uwsgi依赖yum groupinstall "Development Tools"yum install pythonyum install python-devel ...

  5. docker 源码分析 六(基于1.8.2版本),Docker run启动过程

    上一篇大致了解了docker 容器的创建过程,其实主要还是从文件系统的视角分析了创建一个容器时需要得建立 RootFS,建立volumes等步骤:本章来分析一下建立好一个容器后,将这个容器运行起来的过 ...

  6. 多线程随笔知识点总结-NSThread4.1

    线程的状态 状态说明 a.新建 实例化线程对象 b.就绪 向线程对象发送start消息,线程对象被加入可调度线程池等待CPU调度;detach方法和performSelectorInBackGroun ...

  7. iOS UITextField限制输入数字

    有时候项目中要求文本框中只能输入数字,如:价格.公里数.费用等等,一般的文本框不限制输入的格式,这时候只能强制限制输入框的输入格式了,代码如下: #import "ViewControlle ...

  8. 【LeetCode】Roman to Integer & Integer to Roman

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  9. JS原生效果瀑布流布局的实现(一)

    JS原生效果 实现: HTML页面布局: <!DOCTYPE html> <html> <head> <meta charset="utf-8&qu ...

  10. runtime 初入

    一.runtime简介 RunTime简称运行时.OC就是运行时机制,也就是在运行时候的一些机制,其中最主要的是消息机制. 对于C语言,函数的调用在编译的时候会决定调用哪个函数. 对于OC的函数,属于 ...