C# mysql 备份还原表

protected void btnCommit_Click(object sender, EventArgs e)
    {
        if (GridViewDBBackupList.SelectedIndex == -1)
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(\"没有选择要恢复的备份!\");", true);
        }
        else
        {
            DataTable dt = common.getDataTable("select * from prodoc_jc_dbbackuprestore where id=" + GridViewDBBackupList.SelectedDataKey["id"].ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                string filePath = @"" + System.Configuration.ConfigurationManager.AppSettings["dbbackuppath"].ToString();
                string fileName = dt.Rows[0]["fileName"].ToString();
                if (File.Exists(filePath + "\\" + fileName))//d:\dbbackup\db.sql"))
                {
                    string str = common.CallExe(@"mysql -uroot -p" + System.Configuration.ConfigurationManager.AppSettings["mysqlrootpwd"].ToString() + " " + System.Configuration.ConfigurationManager.AppSettings["mysqldbname"].ToString() + "<" + filePath + "\\" + fileName);//d:\dbbackup\db.sql");
                    if (str.Equals("OK"))
                    {
                        ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(\"还原成功!\");closeBackupList();", true);// common.popUpMessage(this.Page, );
                        string sql = string.Empty;
                        sql = "insert into prodoc_jc_dbbackuprestore(type,filePath,fileName,autoORmanu";
                        if (Session["userRealName"] != null)
                            sql += ",createPerson";
                        sql += ")";
                        sql += " values(1,'" + filePath + "','" + fileName + "',1";
                        if (Session["userRealName"] != null)
                            sql += ",'" + Session["userRealName"].ToString() + "'";
                        sql += ")";
                        common.executeNoQuery(sql);

                        bindData();
                        bindDataForBackupList();
                    }
                }
                else
                    //common.popUpMessage(this.Page, "没有找到备份文件!");
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(\"没有找到备份文件!\");", true);
            }
        }
    }
#region backup button
    protected void btnBackup_Click(object sender, EventArgs e)
    {
        if (Session.Count == 0)
        {
            common.execJavaScript(this.Page, "window.top.location.href='../login.aspx';");
            return;
        }
        string filePath = string.Empty;
        filePath = @"" + System.Configuration.ConfigurationManager.AppSettings["dbbackuppath"].ToString();
        if (!Directory.Exists(filePath))
            common.CallExe(@"mkdir " + filePath);
        string fileName = "dbbackup" + System.DateTime.Today.ToString("yyyyMMdd") + System.DateTime.Now.ToLongTimeString().Replace(":", "") + ".sql";
        string str = common.CallExe(@"mysqldump -uroot -p" + System.Configuration.ConfigurationManager.AppSettings["mysqlrootpwd"].ToString() + " " + System.Configuration.ConfigurationManager.AppSettings["mysqldbname"].ToString() + " --ignore-table=" + System.Configuration.ConfigurationManager.AppSettings["mysqldbname"].ToString() + ".prodoc_jc_dbbackuprestore>" + filePath + "\\" + fileName);
        if (str.Equals("OK"))
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(\"备份成功!\");closeBackupList();", true);
            string sql = string.Empty;
            sql = "insert into prodoc_jc_dbbackuprestore(filePath,fileName,autoORmanu";
            if (Session["userRealName"] != null)
                sql += ",createPerson";
            sql += ")";
            sql += " values('" + filePath + "','" + fileName + "',1";
            if (Session["userRealName"] != null)
                sql += ",'" + Session["userRealName"] + "'";
            sql += ")";
            common.executeNoQuery(sql);
            bindData();
        }
    }
    #endregion

    #region 功能:执行dos命令;参数:一个,要执行的dos命令;返回值:成功,OK;失败,错误信息
    public string CallExe(string argm)
    {
        Process p = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "cmd.exe"; //设定需要执行的命令
        startInfo.Arguments = "/C " + argm; //  设定参数,其中的“/C”表示执行完命令后马上退出
        startInfo.UseShellExecute = false; //不使用系统外壳程序启动
        startInfo.RedirectStandardInput = false; //不重定向输入
        startInfo.RedirectStandardOutput = true; //重定向输出
        startInfo.CreateNoWindow = true; //不创建窗口
        p.StartInfo = startInfo;
        try
        {
            if (p.Start()) //开始进程
            {
                //if (milliseconds == 0)
                p.WaitForExit(); //这里无限等待进程结束
                //else
                //p.WaitForExit(milliseconds); //这里等待进程结束,等待时间为指定的毫秒                
            }
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
        finally
        {
            if (p != null)
                p.Close();
        }
        return "OK";
    }
    #endregion
mysqldump -uxxx -pxxx --ignore-table=database.table1 --ignore-table=database.table2 > backup.sql
 mysqldump -uxxx -pxxx table1 table2 … > backup.sql
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值