Click here to Skip to main content
11,709,536 members (43,095 online)
Click here to Skip to main content

Tagged as

MYSQL Database Backup / Restore

, 13 Feb 2014 CPOL 13.5K 7
Rate this:
Please Sign up or sign in to vote.
Using this, you can easily create database backup and also you can restore MYSQL database.

Introduction

Go to this link and download 'mysql-5.1.73-win32'.

Install it in your system as a server machine and set the password.

Download MySql Connector Net 5.1.7 from here and install it.

Design the form in .NET. Add 'Save File Dialog' control and set name sfd. Add 'Open File Dialog' control and set name opd.

Add 'MySql.Data' version 5.1.7.0 in Reference.

*Import on Form

Imports System.ComponentModel
Imports System.Text
Imports MySql.Data.MySqlClient

Imports System.IO 

Using the Code

Code for backup is as given below:

Private Sub btnBackup_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnBackup.Click
Dim file As String
sfd.Filter = "SQL Dump File (*.sql)|*.sql|All files (*.*)|*.*"
sfd.FileName = "Database Backup " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".sql"
If sfd.ShowDialog = DialogResult.OK Then
file = sfd.FileName
Dim myProcess As New Process()
myProcess.StartInfo.FileName = "cmd.exe"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.WorkingDirectory = "C:\Program Files\MySQL\MySQL Server 5.1\bin\"
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.Start()
Dim myStreamWriter As StreamWriter = myProcess.StandardInput
Dim mystreamreader As StreamReader = myProcess.StandardOutput
myStreamWriter.WriteLine("mysqldump -u root --password=yourpassword _
	-h 192.168.1.201 ""databasename"" > """ + file + """ ")
myStreamWriter.Close()
myProcess.WaitForExit()
myProcess.Close()
MsgBox("Backup Created Successfully!", MsgBoxStyle.Information, "Backup")
End If
End Sub

Code for restore is as follows:

Private Sub btnRestore_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnRestore.Click
Dim file As String
opd.Filter = "SQL Dump File (*.sql)|*.sql|All files (*.*)|*.*"
If opd.ShowDialog = DialogResult.OK Then
file = opd.FileName
Dim myProcess As New Process()
myProcess.StartInfo.FileName = "cmd.exe"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.WorkingDirectory = "C:\Program Files\MySQL\MySQL Server 5.1\bin\"
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.Start()
Dim myStreamWriter As StreamWriter = myProcess.StandardInput
Dim mystreamreader As StreamReader = myProcess.StandardOutput
myStreamWriter.WriteLine("mysql -u root --password=yourpassword _
	-h 192.168.1.201 ""databasename"" < """ + file + """ ")
myStreamWriter.Close()
myProcess.WaitForExit()
myProcess.Close()
MsgBox("Database Restoration Successfully!", MsgBoxStyle.Information, "Restore")
End If

End Sub 

History

  • 13th February, 2014: Initial version

Go to this link and join my page.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sumit Kumar Pati
Software Developer (Senior) Shankar Infotech
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionEmpty Backup Pin
Member 1160119121-Aug-15 8:00
memberMember 1160119121-Aug-15 8:00 
QuestionBackup Empty Pin
Member 1069256727-Oct-14 6:10
memberMember 1069256727-Oct-14 6:10 
QuestionEmpty file created when I backed up my database. Pin
Leetian0426-Jun-14 17:22
memberLeetian0426-Jun-14 17:22 
QuestionRe: Empty file created when I backed up my database. Pin
Member 1108012914-Sep-14 8:23
memberMember 1108012914-Sep-14 8:23 
QuestionMySQL Backup Pin
asdrog17-Feb-14 3:53
memberasdrog17-Feb-14 3:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 13 Feb 2014
Article Copyright 2014 by Sumit Kumar Pati
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid