简体   繁体   English

VB.Net中的mysql数据库的数据库备份

[英]Database backup of a mysql database in VB.Net

如何在VB.Net中创建mysql数据库的数据库备份?

You can use MySqlBackup.NET , which is an alternative to mysqldump. 您可以使用MySqlBackup.NET ,它是mysqldump的替代品。

Official Website & Documentation > https://github.com/MySqlBackupNET/MySqlBackup.Net 官方网站和文档> https://github.com/MySqlBackupNET/MySqlBackup.Net

Examples: 例子:

Backup a MySql Database 备份MySql数据库

Dim conn As MySqlConnection = New MySqlConnection(constr)
Dim cmd As MySqlCommand = New MySqlCommand
cmd.Connection = conn
conn.Open
Dim mb As MySqlBackup = New MySqlBackup(cmd)
mb.ExportToFile("C:\backup.sql")
conn.Close

Restore a MySql Database 恢复MySql数据库

Dim conn As MySqlConnection = New MySqlConnection(constr)
Dim cmd As MySqlCommand = New MySqlCommand
cmd.Connection = conn
conn.Open
Dim mb As MySqlBackup = New MySqlBackup(cmd)
mb.ImportFromFile("C:\backup.sql")
conn.Close

I am one of the author of this project. 我是这个项目的作者之一。

你可以调用mysqldump,但你可能需要在Mysql服务器上运行你的VB.NET。

I found the easiest way was to use the mysqldump.exe which is a standalone application. 我发现最简单的方法是使用mysqldump.exe这是一个独立的应用程序。

mysqldump --host=[HOSTNAME] --user=[USER] --password=[PASSWORD] -R [DATABASE NAME] > [PATH TO BACKUP FILE]

We had issues with backups not saving db functions but the -R switch sorted it so id recommend using it if you use stored procedures or functions in your DB. 我们遇到的问题是备份没有保存数据库功能,但-R开关对它进行了排序,因此如果您在数据库中使用存储过程或函数,则建议使用它。

to restore the created file use the mysql command instead. 要恢复创建的文件,请使用mysql命令。

mysql --host=[HOSTNAME] --user=[USER] --password=[PASSWORD] [DATABASE NAME] < [PATH TO BACKUP FILE]

Use this code. 使用此代码。 It works for me. 这个对我有用。

I had such a problem and then found this article 我有这样的问题然后找到了这篇文章

" http://www.experts-exchange.com/Programming/Languages/.NET/Q_27155602.html " http://www.experts-exchange.com/Programming/Languages/.NET/Q_27155602.html

Example was in C#. 示例是在C#中。 I manually converted it into vb.net and add converting into 'utf8'. 我手动将其转换为vb.net并将转换添加到'utf8'。

Imports System.Text
Public Class Form1
    Dim OutputStream As System.IO.StreamWriter
    Sub OnDataReceived1(ByVal Sender As Object, ByVal e As    System.Diagnostics.DataReceivedEventArgs)
        If e.Data IsNot Nothing Then
            Dim text As String = e.Data
            Dim bytes As Byte() = Encoding.Default.GetBytes(text)
            text = Encoding.UTF8.GetString(bytes)
            OutputStream.WriteLine(text)
           End If
    End Sub

   Sub CreateBackup()
        Dim mysqldumpPath As String = "d:\mysqldump.exe"
        Dim host As String = "localhost"
        Dim user As String = "root"
        Dim pswd As String = "Yourpwd"
        Dim dbnm As String = "BaseName"
        Dim cmd As String = String.Format("-h{0} -u{1} -p{2} {3}", host, user, pswd, dbnm)
        Dim filePath As String = "d:\backup\fieName.sql"
        OutputStream = New System.IO.StreamWriter(filePath, False, System.Text.Encoding.UTF8)

        Dim startInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo()
        startInfo.FileName = mysqldumpPath
        startInfo.Arguments = cmd

        startInfo.RedirectStandardError = True
        startInfo.RedirectStandardInput = False
        startInfo.RedirectStandardOutput = True 
        startInfo.UseShellExecute = False
        startInfo.CreateNoWindow = True
        startInfo.ErrorDialog = False

        Dim proc As System.Diagnostics.Process = New    System.Diagnostics.Process()
        proc.StartInfo = startInfo
        AddHandler proc.OutputDataReceived, AddressOf OnDataReceived1
        proc.Start()
        proc.BeginOutputReadLine()
        proc.WaitForExit()

       OutputStream.Flush()
       OutputStream.Close()
        proc.Close()
    End Sub

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As      System.EventArgs) Handles MyBase.Load

        CreateBackup()

     End Sub
   End Class

This is what I use to backup data on mysql. 这就是我用来备份mysql数据的方法。 I make a copy of mysqldump.exe and mysql.exe and store it on my LIB_PATH then the following code will backup your data. 我制作了mysqldump.exe和mysql.exe的副本并将其存储在我的LIB_PATH上,然后以下代码将备份您的数据。 You can specify your mysqldump.exe directory and assign it to LIB_PATH, provide your login details under the Arguments then specify your output directory, mine is set to BACKUP_DIR and I use the preformatted Now() as my filename. 您可以指定mysqldump.exe目录并将其分配给LIB_PATH,在Arguments下提供您的登录详细信息,然后指定您的输出目录,我的设置为BACKUP_DIR,并使用预先格式化的Now()作为我的文件名。 The code is pretty straight forward. 代码很简单。 Goodluck 祝好运

    Using myProcess As New Process()
        Dim newfiledb As String = BACKUPDIR_PATH & Format(Now(), "MMM_dd_yyyy@h~mm_tt").ToString & "_local.sql"
        Try
            myProcess.StartInfo.FileName = "mysqldump.exe"
            myProcess.StartInfo.WorkingDirectory = LIB_PATH
            myProcess.StartInfo.Arguments = "--host=localhost --user=username --password=yourpassword yourdatabase -r " & newfiledb
            myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
            myProcess.Start()
            myProcess.WaitForExit()
            MsgBox("Backup Created ... " & vbNewLine & newfiledb)
        Catch ex As Exception
            MsgBox(ex.Message, vbCritical + vbOKOnly, ex.Message)
        Finally
            myProcess.Close()
        End Try
    End Using

您可以读取每个表的数据并将其写入新数据库。

I would write a stored proc, since MySQL 5 has support for them, that handles all the data "heavy" work. 我会编写一个存储过程,因为MySQL 5支持它们,它处理所有数据“繁重”的工作。 Then just create scheduled task that calls the procedure every "night". 然后创建每隔“夜晚”调用该过程的计划任务。 For this latter component, I highly recommend Powershell....its awesome. 对于后一个组件,我强烈推荐Powershell ....它很棒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM