Пример #1
0
        public void Backup()
        {
            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();
                mb.DropAndRecreateDatabase = DropAndRecreateDatabase;
                mb.DropAndRecreateTable = DropAndRecreateTable;
                mb.ConstructSQLInOneLineFromSameTable = ConstructSQLInOneLineFromSameTable;

                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");
            }
        }
Пример #2
0
        public void Restore()
        {
            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();

                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");
            }
        }