Artikel kali ini hanya posting ulang dari jawaban saya yang ada di artikel Membuat paket instalasi vb + mysql dengan NSIS, karena masih ada beberapa pengunjung yang bertanya tentang masalah ini.
Untuk proses backup kita cukup menggunakan tool bawaan MySQL yaitu mysqldump dan untuk restorenya menggunakan mysql.
Adapun perintah dasar untuk melakukan backup dan restore adalah sebagai berikut :
parameter –routines ditambahkan jika database mempunyai fungsi/trigger
Sekarang tinggal kita siapkan kode berikut :
12345678910111213141516171819202122232425
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const SYNCHRONIZE As Long = &H100000
Private Const INFINITE As Long = &HFFFF
Private Sub execCommand(ByVal cmd As String)
Dim result As Long
Dim lPid As Long
Dim lHnd As Long
Dim lRet As Long
cmd = "cmd /c " & cmd
result = Shell(cmd, vbHide)
lPid = result
If lPid <> 0 Then
lHnd = OpenProcess(SYNCHRONIZE, 0, lPid)
If lHnd <> 0 Then
lRet = WaitForSingleObject(lHnd, INFINITE)
CloseHandle (lHnd)
End If
End If
End Sub
kemudian tinggal panggil di masing-masing proses (backup dan restore)
1234567891011121314151617181920212223
Dim cmd As String
Private Sub cmdBackup_Click()
Screen.MousePointer = vbHourglass
DoEvents
cmd = Chr(34) & "C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqldump" & Chr(34) & " -uroot -prahasia --routines --comments nama_database > c:\nama_database.sql"
Call execCommand(cmd)
Screen.MousePointer = vbDefault
MsgBox "done"
End Sub
Private Sub cmdRestore_Click()
Screen.MousePointer = vbHourglass
DoEvents
cmd = Chr(34) & "C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql" & Chr(34) & " -uroot -prahasia --comments nama_database < c:\nama_database.sql"
Call execCommand(cmd)
Screen.MousePointer = vbDefault
MsgBox "done"
End Sub
kode diatas dengan asumsi direktori bin MySQL ada di folder “C:\Program Files\MySQL\MySQL Server 5.1\bin”.