MYSQL Database Backup / Restore

View: 869    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: MySQL   Fields: Education - Schools

0 point/3 review File has been tested

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.

Introduction

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:

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

Code for restore is as follows:

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

<span style="color:blue">End</span> <span style="color:blue">Sub</span> 

MYSQL Database Backup / Restore

MYSQL Database Backup / Restore Posted on 26-03-2014  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. 5/10 869

Comment:

To comment you must be logged in members.

Files with category

 

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd