Example #1
0
        public void Backup()
        {
            #region Check Required Connection Parameters

            bool okConnect = true;
            string errorMsg = "";
            if (textBox_Server.Text.Length == 0)
            { errorMsg += "Database server not define.\r\n"; okConnect = false; }
            if (textBox_Username.Text.Length == 0)
            { errorMsg += "Username must not empty.\r\n"; okConnect = false; }
            if (textBox_Password.Text.Length == 0)
            { errorMsg += "Password is not entered.\r\n"; okConnect = false; }
            if (textBox_Database.Text.Length == 0)
            { errorMsg += "No database is defined.\r\n"; okConnect = false; }
            if (!okConnect)
            {
                ToolTip tt = new ToolTip();
                tt.ToolTipTitle = "Some entry is blank.";
                tt.Show(errorMsg, this, textBox_Server.Location.X + textBox_Server.Width, textBox_Server.Location.Y + textBox_Server.Height, 7000);
                ToolTip tt2 = new ToolTip();
                tt2.ToolTipTitle = "Cannot Backup";
                tt2.Show("Some parameters is missing.", this, button_Backup.Location, 2000);
                return;
            }
            #endregion

            string backupFile = "";
            string filter = "";
            try
            {
                #region Get the Backup Filename and Path

                if (FileExtension != "") filter = "*." + FileExtension + "|*." + FileExtension;

                if (textBox_filename.Text.Trim().Length == 0)
                {
                    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                    saveFileDialog1.Title = "Save Database Backup File";
                    saveFileDialog1.Filter = filter;
                    if (Directory.Exists(textBox_folder.Text.Trim()))
                        saveFileDialog1.InitialDirectory = textBox_folder.Text.Trim();
                    if (DialogResult.OK != saveFileDialog1.ShowDialog())
                        return;
                    backupFile = saveFileDialog1.FileName;
                    if (AddDateToFilename)
                    {
                        if (FileExtension != "")
                            backupFile = backupFile.Replace("." + FileExtension, DateTime.Now.ToString(" yyyy-MM-dd-HH-mm-ss") + "." + FileExtension);
                        else
                            backupFile += DateTime.Now.ToString(" yyyy-MM-dd-HH-mm-ss");
                    }
                }
                else
                {
                    backupFile = textBox_filename.Text.Trim();
                    if (checkBox_AddDate.Checked) backupFile += DateTime.Now.ToString(" yyyy-MM-dd-HH-mm-ss");
                    if (FileExtension.Length != 0) backupFile = backupFile + "." + FileExtension;
                    string folderpath = "";
                    if (Directory.Exists(textBox_folder.Text.Trim()))
                        folderpath = textBox_folder.Text.Trim();
                    else
                    {
                        FolderBrowserDialog fbd = new FolderBrowserDialog();
                        fbd.Description = "Choose where to save the backup file.\r\n" + backupFile;
                        if (DialogResult.OK != fbd.ShowDialog())
                            return;
                        folderpath = fbd.SelectedPath;
                    }
                    backupFile = (folderpath + "\\").Replace("\\\\", "\\") + backupFile;
                }
                #endregion

                // Start Backup Process

                MySqlBackupRestore mb = new MySqlBackupRestore(SeverStr, Username, Pass, Database, Port, otherDetails);
               // MySqlBackupRestore mb = new MySqlBackupRestore(textBox_Server.Text, textBox_Username.Text, textBox_Password.Text, textBox_Database.Text, textBox_Port.Text, textBox_otherDetails.Text);
                mb.DropAndRecreateDatabase = DropAndRecreateDatabase;
                mb.DropAndRecreateTable = DropAndRecreateTable;
                mb.Construct_SQL_In_One_Line_From_Same_Table = Construct_SQL_In_One_Line_From_Same_Table;

                //if (EnableEncryption)
                //{
                //    mb.EncryptBackupFile = EnableEncryption;
                //    mb.EncryptionKey = textBox_encryptKey.Text;
                //}

                mb.Backup(backupFile);

                // End of Backup Process

                MessageBox.Show("Backup Successfully.\n\nYour backup file is created at:\r\n" + backupFile, "Backup", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex) // Log any error that occur during the backup process
            {
                string errorMessage = "Backup fail.\r\n\r\n" + ex.ToString();
                MessageBox.Show(errorMessage, "Error");
            }
        }
Example #2
0
        public void Restore()
        {
            #region Check Required Connection Parameters

            bool okConnect = true;
            string errorMsg = "";
            if (textBox_Server.Text.Length == 0)
            { errorMsg += "Database server not define.\r\n"; okConnect = false; }
            if (textBox_Username.Text.Length == 0)
            { errorMsg += "Username must not empty.\r\n"; okConnect = false; }
            if (textBox_Password.Text.Length == 0)
            { errorMsg += "Password is not entered.\r\n"; okConnect = false; }
            if (!okConnect)
            {
                ToolTip tt = new ToolTip();
                tt.ToolTipTitle = "Some entry is blank.";
                tt.Show(errorMsg, this, textBox_Server.Location.X + textBox_Server.Width, textBox_Server.Location.Y + textBox_Server.Height, 7000);
                ToolTip tt2 = new ToolTip();
                tt2.ToolTipTitle = "Cannot Restore";
                tt2.Show("Some parameters is missing.", this, button_Restore.Location, 2000);
                return;
            }
            #endregion
            // Locate backup file -----------------------------

            string filename = "";
            OpenFileDialog f2 = new OpenFileDialog();
            f2.Title = "Open Database Backup File";
            f2.Filter = "*." + FileExtension + "|*." + FileExtension + "|All Files|*.*";
            if (Directory.Exists(textBox_folder.Text.Trim()))
                f2.InitialDirectory = textBox_folder.Text.Trim();
            if (DialogResult.OK != f2.ShowDialog())
                return;

            filename = f2.FileName;

            // End of Locate backup file ----------------------

            try
            {
                MySqlBackupRestore mb = new MySqlBackupRestore(SeverStr, Username, Pass, Database, Port, otherDetails);
                //MySqlBackupRestore mb = new MySqlBackupRestore(textBox_Server.Text, textBox_Username.Text, textBox_Password.Text, "", textBox_Port.Text, textBox_otherDetails.Text);

                //if (EnableEncryption)
                //{
                //    mb.Restore(filename, true, textBox_encryptKey.Text);
                //}
                //else
                //{
                    mb.Restore(filename);
                //}

                MessageBox.Show("Restore successfull.", "Restore");
            }
            catch (Exception ex) // Log any error that occur during the backup process
            {
                string errorMessage = "Restore fail.\r\n\r\n" + ex.ToString();
                MessageBox.Show(errorMessage, "Error");
            }
        }