Imports System.Configuration
Public Class frmMySqlBackupBD

    Dim intBancoAtual As Integer = 0
    Dim vMyFont As New Font("Segoe UI", 20, FontStyle.Bold Or FontStyle.Italic)

#Region "Propriedades"

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

        Try

            Me.txtCaminhoBackup.Text = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\New Service System\" & vstrNomeSistema, "sistema.backUp.ultimoCaminhoUtilizado", "")

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub btProcurarCaminhoBackup_Click(sender As System.Object, e As System.EventArgs) Handles btProcurarCaminhoBackup.Click

        Try

            If Me.FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.txtCaminhoBackup.Text = Me.FolderBrowserDialog1.SelectedPath
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub btBackUp_Click(sender As System.Object, e As System.EventArgs) Handles btBackUp.Click

        Try

            lBackupBD()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub btFechar_Click(sender As System.Object, e As System.EventArgs) Handles btFechar.Click

        Try

            lClose()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub frmMySqlBackupBD_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize

        Try


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub frmMySqlBackupBD_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

        Try

            Select Case intBancoAtual
                Case 0 '// Backup pendente
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(0), 40, 130)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(0), 40, 180)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(0), 40, 230)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(0), 40, 280)
                Case 1 '// Backup do banco do LinkCenter concluído
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(1), 40, 130)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(0), 40, 180)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(0), 40, 230)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(0), 40, 280)
                Case 2 '// Backup do banco do NSS concluído
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(2), 40, 130)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(1), 40, 180)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(0), 40, 230)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(0), 40, 280)
                Case 3 '// Backup do banco do Faturamento concluído
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(2), 40, 130)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(2), 40, 180)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(1), 40, 230)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(0), 40, 280)
                Case 4 '// Backup do banco do Faturamento Online concluído
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(2), 40, 130)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(2), 40, 180)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(2), 40, 230)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(1), 40, 280)
                Case 5 '// Todos os backups concluídos
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(2), 40, 130)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(2), 40, 180)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(2), 40, 230)
                    e.Graphics.DrawImage(Me.ImageList1.Images.Item(2), 40, 280)
            End Select

            e.Graphics.DrawString("LinkCenter", vMyFont, Brushes.Black, 80, 125)
            e.Graphics.DrawString("NSS", vMyFont, Brushes.Black, 80, 175)
            e.Graphics.DrawString("Faturamento", vMyFont, Brushes.Black, 80, 225)
            e.Graphics.DrawString("Faturamento Online", vMyFont, Brushes.Black, 80, 275)

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try


    End Sub

    Private Sub frmMySqlBackupBD_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

        Try
            If Not vfrmOld Is Nothing Then
                vfrmOld.Enabled = True
            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

#End Region

#Region "Métodos"

    Private Sub lClose()

        Try
            Me.Close()

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub lBackupBD()

        Dim comando As String

        Try

            If Me.txtCaminhoBackup.Text = String.Empty Then
                MessageBox.Show("Por favor, selecione o local do backup!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.btProcurarCaminhoBackup.PerformClick()
                Exit Sub
            End If

            If Not IO.Directory.Exists(Me.txtCaminhoBackup.Text) Then
                MessageBox.Show("O caminho selecionado para backup não existe!" & vbCrLf & vbCrLf & "Por favor, selecione um caminho válido.", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.btProcurarCaminhoBackup.PerformClick()
                Exit Sub
            End If

            If Not IO.File.Exists(My.Application.Info.DirectoryPath & "\mysqldump.exe") Then
                MessageBox.Show("O arquivo ""mysqldump.exe"" não foi encontrado na pasta do sistema!" & vbCrLf & vbCrLf & "Sem esse arquivo é impossível realizar o back-up do banco de dados.", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If

            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\New Service System\" & vstrNomeSistema, "sistema.backUp.ultimoCaminhoUtilizado", Me.txtCaminhoBackup.Text)

            comando = String.Empty

            intBancoAtual = 1 : Me.Refresh()

            comando = "mysqldump.exe" &
                " -h " & vMySqlHostName &
                " -P " & vMySqlPort &
                " -u " & vMySqlUserName &
                " -p" & vMySqlPassWord &
                " -R --skip-lock-tables " & ConfigurationManager.AppSettings("table_lc") &
                " --result-file=""" & Me.txtCaminhoBackup.Text & "\lc_" & UCase(vstrNomeEmpresa) & "_" & Format(Now, "yyyyMMdd HHmmss") & ".sql" & """"

            Shell(comando, AppWinStyle.Hide, True)

            comando = String.Empty

            intBancoAtual = 2 : Me.Refresh()

            comando = "mysqldump.exe" &
                " -h " & vMySqlHostName &
                " -P " & vMySqlPort &
                " -u " & vMySqlUserName &
                " -p" & vMySqlPassWord &
                " -R --skip-lock-tables " & ConfigurationManager.AppSettings("table_nss") &
                " --result-file=""" & Me.txtCaminhoBackup.Text & "\nss_" & UCase(vstrNomeEmpresa) & "_" & Format(Now, "yyyyMMdd HHmmss") & ".sql" & """"

            Shell(comando, AppWinStyle.Hide, True)

            comando = String.Empty

            intBancoAtual = 3 : Me.Refresh()

            comando = "mysqldump.exe" &
                " -h " & vMySqlHostName &
                " -P " & vMySqlPort &
                " -u " & vMySqlUserName &
                " -p" & vMySqlPassWord &
                " -R --skip-lock-tables " & ConfigurationManager.AppSettings("table_fat") &
                " --result-file=""" & Me.txtCaminhoBackup.Text & "\fatf_" & UCase(vstrNomeEmpresa) & "_" & Format(Now, "yyyyMMdd HHmmss") & ".sql" & """"

            Shell(comando, AppWinStyle.Hide, True)

            comando = String.Empty

            intBancoAtual = 4 : Me.Refresh()

            comando = "mysqldump.exe" &
                " -h " & ConfigurationManager.AppSettings("server_fatonline") &
                " -P " & vMySqlPort &
                " -u " & ConfigurationManager.AppSettings("u_fatonline") &
                " -p" & ConfigurationManager.AppSettings("p_fatonline") &
                " -R --skip-lock-tables " & ConfigurationManager.AppSettings("table_fatonline") &
                " --result-file=""" & Me.txtCaminhoBackup.Text & "\fatf_online_" & UCase(vstrNomeEmpresa) & "_" & Format(Now, "yyyyMMdd HHmmss") & ".sql" & """"

            Shell(comando, AppWinStyle.Hide, True)

            comando = String.Empty

            intBancoAtual = 5 : Me.Refresh()

            MessageBox.Show("Backup concluído com sucesso!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

#End Region

End Class