3

How do you create a database backup of a mysql database in VB.Net?

7 Answers 7

9

You can use MySqlBackup.NET, which is an alternative to mysqldump.

Official Website & Documentation > https://github.com/MySqlBackupNET/MySqlBackup.Net

Examples:

Backup a MySql Database

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

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I keep getting this error: An unhandled exception of type 'System.IO.FileLoadException' occurred in System.Windows.Forms.dll Additional information: Could not load file or assembly 'MySqlBackup, Version=2.0.9.2, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)
@MarkIsmail this might be caused by different version of target .NET Framework between your project and the referenced DLL.
@mjb What Imports do you use? because it says: Type 'MySqlBackup' is not defined.
@kiLLua >> MySql.Data.MySqlClient. for more information, you can visit: github.com/MySqlBackupNET/MySqlBackup.Net
2

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"

Example was in C#. I manually converted it into vb.net and add converting into '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

Comments

1

you could invoke mysqldump, but you may need to be running your VB.NET on the Mysql server.

Comments

1

I found the easiest way was to use the mysqldump.exe which is a standalone application.

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.

to restore the created file use the mysql command instead.

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

Comments

0

You can read each table's data and write it to a new database.

Comments

0

I would write a stored proc, since MySQL 5 has support for them, that handles all the data "heavy" work. Then just create scheduled task that calls the procedure every "night". For this latter component, I highly recommend Powershell....its awesome.

Comments

0

This is what I use to backup data on 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. 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. 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.