private void RunBackUp()
        {
            if (_isrunning)
            {
                return;
            }
            if (_succeeded)
            {
                _succeeded = false;
            }

            if (!Directory.Exists(Path.GetDirectoryName(lblPath.Text)))
            {
                MsgBoxEx.Shout("Please specify a valid database backup output directory.", "Database Backup"); return;
            }

            _isrunning        = true; _cancelled = false; InitializeEventGrid();
            btnBrowse.Enabled = false; btnBackup.Enabled = false;
            pctLoad.Show(); pctLoad.BringToFront(); btnCancel.BringToFront();

            AddEvent(BackupEvent.Information, "Database backup operations started.");

            string _tempdir = Application.StartupPath + "\\dbtemp\\"; int _trycounter = 0;

            if (!Directory.Exists(_tempdir))
            {
                try { Directory.CreateDirectory(_tempdir); }
                catch (Exception ex)
                {
                    SCMS.LogError(this.GetType().Name, ex);
                    AddEvent(BackupEvent.Error, "Failed to create temporary output directory.");
                    btnBrowse.Enabled          = _isfilebrowsingenabled; btnBackup.Enabled = !_isautoclose;
                    pctLoad.Hide(); _isrunning = false; btnCancel.SendToBack(); return;
                }
            }

            MySqlDumpParameterCollection _parameters = new MySqlDumpParameterCollection();

            _parameters.Add(MySqlDumpParameters.CompleteInsert);
            _parameters.Add(MySqlDumpParameters.HexBlob);
            _parameters.Add(MySqlDumpParameters.Routines);
            _parameters.Add("--skip-extended-insert");
            _parameters.Add(MySqlDumpParameters.Triggers);
            _parameters.Add(MySqlDumpParameters.Quick);

            string _filename = Application.StartupPath + "\\dbtemp\\" + Path.GetFileNameWithoutExtension(lblPath.Text) + ".sql";

            Func <string, string, MySqlDumpParameterCollection, MySqlResult> _dumpdelegate = new Func <string, string, MySqlDumpParameterCollection, MySqlResult>(MySql.Dump);
            IAsyncResult _dumpresult = _dumpdelegate.BeginInvoke(SCMS.ServerConnection.ToString(), _filename, _parameters, null, _dumpdelegate);

            AddEvent(BackupEvent.Information, "Running database backup operations.");

            while (!_dumpresult.IsCompleted &&
                   !_cancelled)
            {
                Thread.Sleep(1); Application.DoEvents();
            }

            if (_cancelled)
            {
                if (!_dumpresult.IsCompleted)
                {
                    try { _dumpresult = null; }
                    catch { }
                }

                AddEvent(BackupEvent.Warning, "Cancelling database backup...");

                Materia.RefreshAndManageCurrentProcess(); _trycounter = 0;
                while (File.Exists(_filename) &&
                       _trycounter < 30)
                {
                    try { File.Delete(_filename); }
                    catch { }

                    _trycounter += 1; Thread.Sleep(100); Application.DoEvents();
                }

                AddEvent(BackupEvent.Information, "Database backup operations has been cancelled.");
                btnBrowse.Enabled = _isfilebrowsingenabled; btnBackup.Enabled = !_isautoclose;
                pctLoad.Hide(); btnCancel.SendToBack(); _isrunning = false;

                _trycounter = 0;

                while (_trycounter < 10)
                {
                    Thread.Sleep(100); Application.DoEvents();
                    _trycounter += 1;
                }
            }
            else
            {
                MySqlResult _dumpexecresult = _dumpdelegate.EndInvoke(_dumpresult);
                if (_dumpexecresult.Succeeded)
                {
                    AddEvent(BackupEvent.Information, "Database backup has been created.");
                    AddEvent(BackupEvent.Information, "Archiving backup file...");

                    Func <string, ArchivingToolEnum, FileInfo> _archiverdelegate = new Func <string, ArchivingToolEnum, FileInfo>(Archiver.CompressInsert);
                    IAsyncResult _archiverresult = _archiverdelegate.BeginInvoke(_filename, ArchivingToolEnum.SevenZip, null, _archiverdelegate);

                    while (!_archiverresult.IsCompleted &&
                           !_cancelled)
                    {
                        Thread.Sleep(1); Application.DoEvents();
                    }

                    if (_cancelled)
                    {
                        AddEvent(BackupEvent.Warning, "Cancelling database backup...");

                        Materia.RefreshAndManageCurrentProcess(); _trycounter = 0;
                        while (File.Exists(_filename) &&
                               _trycounter < 30)
                        {
                            try { File.Delete(_filename); }
                            catch { }

                            _trycounter += 1; Thread.Sleep(100); Application.DoEvents();
                        }

                        _trycounter = 0;
                        _filename   = Application.StartupPath + "\\dbtemp\\" + Path.GetFileNameWithoutExtension(lblPath.Text) + ".7z";

                        while (File.Exists(_filename) &&
                               _trycounter < 30)
                        {
                            try { File.Delete(_filename); }
                            catch { }

                            _trycounter += 1; Thread.Sleep(20); Application.DoEvents();
                        }

                        AddEvent(BackupEvent.Information, "Database backup operations has been cancelled.");
                        btnBrowse.Enabled = _isfilebrowsingenabled; btnBackup.Enabled = !_isautoclose;
                        pctLoad.Hide(); btnCancel.SendToBack(); _isrunning = false;

                        _trycounter = 0;

                        while (_trycounter < 10)
                        {
                            Thread.Sleep(100); Application.DoEvents();
                            _trycounter += 1;
                        }
                    }
                    else
                    {
                        FileInfo _archivefile = _archiverdelegate.EndInvoke(_archiverresult);

                        if (_archivefile != null)
                        {
                            AddEvent(BackupEvent.Information, "Database backup file has been archived.");
                            AddEvent(BackupEvent.Information, "Finalizing database backup operations...");

                            bool _copied = false;

                            try
                            {
                                File.Copy(_archivefile.FullName, lblPath.Text, true);
                                _copied = true;

                                Materia.RefreshAndManageCurrentProcess(); _trycounter = 0;
                                while (File.Exists(_filename) &&
                                       _trycounter < 30)
                                {
                                    try { File.Delete(_filename); }
                                    catch { }

                                    _trycounter += 1; Thread.Sleep(100); Application.DoEvents();
                                }

                                _trycounter = 0;
                                _filename   = Application.StartupPath + "\\dbtemp\\" + Path.GetFileNameWithoutExtension(lblPath.Text) + ".7z";

                                while (File.Exists(_filename) &&
                                       _trycounter < 30)
                                {
                                    try { File.Delete(_filename); }
                                    catch { }

                                    _trycounter += 1; Thread.Sleep(100); Application.DoEvents();
                                }
                            }
                            catch (Exception ex)
                            {
                                SCMS.LogError(this.GetType().Name, ex);
                                AddEvent(BackupEvent.Error, "Failed to transfer archive file into output destinaton.");
                                AddEvent(BackupEvent.Warning, "Cancelling database backup...");

                                Materia.RefreshAndManageCurrentProcess(); _trycounter = 0;
                                while (File.Exists(_filename) &&
                                       _trycounter < 30)
                                {
                                    try { File.Delete(_filename); }
                                    catch { }

                                    _trycounter += 1; Thread.Sleep(100); Application.DoEvents();
                                }

                                _trycounter = 0;
                                _filename   = Application.StartupPath + "\\dbtemp\\" + Path.GetFileNameWithoutExtension(lblPath.Text) + ".7z";

                                while (File.Exists(_filename) &&
                                       _trycounter < 30)
                                {
                                    try { File.Delete(_filename); }
                                    catch { }

                                    _trycounter += 1; Thread.Sleep(100); Application.DoEvents();
                                }

                                AddEvent(BackupEvent.Information, "Database backup operations has been cancelled.");
                                btnBrowse.Enabled = _isfilebrowsingenabled; btnBackup.Enabled = !_isautoclose;
                                pctLoad.Hide(); btnCancel.SendToBack(); _isrunning = false;

                                _trycounter = 0;

                                while (_trycounter < 10)
                                {
                                    Thread.Sleep(100); Application.DoEvents();
                                    _trycounter += 1;
                                }
                            }

                            if (_copied)
                            {
                                IAsyncResult _logresult = SCMS.CurrentSystemUser.LogActionAsync(UserAction.BackupDatabase, "Performed database backup into : " + lblPath.Text.ToSqlValidString().Replace("\\\\", "\\") + ".");
                                _logresult.WaitToFinish();
                                AddEvent(BackupEvent.Success, "Database backup operations has been completed.");
                                btnBrowse.Enabled = _isfilebrowsingenabled; btnBackup.Enabled = !_isautoclose;
                                pctLoad.Hide(); btnCancel.SendToBack(); _isrunning = false;

                                if (!_succeeded)
                                {
                                    _succeeded = true;
                                }
                            }
                        }
                        else
                        {
                            AddEvent(BackupEvent.Error, "Database backup file archiving failed.");
                            AddEvent(BackupEvent.Warning, "Cancelling database backup...");

                            Materia.RefreshAndManageCurrentProcess(); _trycounter = 0;
                            while (File.Exists(_filename) &&
                                   _trycounter < 30)
                            {
                                try { File.Delete(_filename); }
                                catch { }

                                _trycounter += 1; Thread.Sleep(100); Application.DoEvents();
                            }

                            _trycounter = 0;
                            _filename   = Application.StartupPath + "\\dbtemp\\" + Path.GetFileNameWithoutExtension(lblPath.Text) + ".7z";

                            while (File.Exists(_filename) &&
                                   _trycounter < 30)
                            {
                                try { File.Delete(_filename); }
                                catch { }

                                _trycounter += 1; Thread.Sleep(100); Application.DoEvents();
                            }

                            AddEvent(BackupEvent.Information, "Database backup operations has been cancelled.");
                            btnBrowse.Enabled = _isfilebrowsingenabled; btnBackup.Enabled = !_isautoclose;
                            pctLoad.Hide(); btnCancel.SendToBack(); _isrunning = false;

                            _trycounter = 0;

                            while (_trycounter < 10)
                            {
                                Thread.Sleep(100); Application.DoEvents();
                                _trycounter += 1;
                            }
                        }
                    }
                }
                else
                {
                    SCMS.LogError(this.GetType().Name, new Exception(_dumpexecresult.Error));
                    AddEvent(BackupEvent.Error, "Database backup operations has been cancelled.");
                    AddEvent(BackupEvent.Warning, "Cancelling database backup...");

                    Materia.RefreshAndManageCurrentProcess(); _trycounter = 0;
                    while (File.Exists(_filename) &&
                           _trycounter < 30)
                    {
                        try { File.Delete(_filename); }
                        catch { }

                        _trycounter += 1; Thread.Sleep(100); Application.DoEvents();
                    }

                    AddEvent(BackupEvent.Information, "Database backup operations has been cancelled.");
                    btnBrowse.Enabled = _isfilebrowsingenabled; btnBackup.Enabled = !_isautoclose;
                    pctLoad.Hide(); btnCancel.SendToBack(); _isrunning = false;

                    _trycounter = 0;

                    while (_trycounter < 10)
                    {
                        Thread.Sleep(100); Application.DoEvents();
                        _trycounter += 1;
                    }
                }
            }
        }
Пример #2
0
        private RestorePointInfo MakeRestorePoint()
        {
            RestorePointInfo _restorepoint = null;

            AddEvent(BackupEvent.Information, "Creating database restore point...");

            string _rpdir = Application.StartupPath + "\\Restore Points";

            if (!Directory.Exists(_rpdir))
            {
                try { Directory.CreateDirectory(_rpdir); }
                catch (Exception ex)
                {
                    SCMS.LogError(this.GetType().Name, ex);
                    AddEvent(BackupEvent.Error, "Can't create database restore point directory.");
                }
            }

            if (Directory.Exists(_rpdir))
            {
                Func <IDbConnection, DateTime> _serverdatedelegate = new Func <IDbConnection, DateTime>(MySql.GetServerDateAndTime);
                IAsyncResult _serverdateresult = _serverdatedelegate.BeginInvoke(SCMS.Connection, null, _serverdatedelegate);
                _serverdateresult.WaitToFinish();
                DateTime _serverdatetime = _serverdatedelegate.EndInvoke(_serverdateresult);
                string   _rpfilename     = _rpdir + "\\" + SCMS.ServerConnection.Database.ToUpper() + "_" + SCMS.CurrentCompany.Company + "_RESTORE_POINT_" + VisualBasic.Format(_serverdatetime, "dd_MM_yyyy_HH_mm_ss") + ".sql";
                int      _trycounter     = 0;

                MySqlDumpParameterCollection _parameters = new MySqlDumpParameterCollection();
                _parameters.Add(MySqlDumpParameters.CompleteInsert);
                _parameters.Add(MySqlDumpParameters.HexBlob);
                _parameters.Add(MySqlDumpParameters.Routines);
                _parameters.Add("--skip-extended-insert");
                _parameters.Add(MySqlDumpParameters.Triggers);
                _parameters.Add(MySqlDumpParameters.Quick);

                Func <string, string, MySqlDumpParameterCollection, MySqlResult> _dumpdelegate = new Func <string, string, MySqlDumpParameterCollection, MySqlResult>(MySql.Dump);
                IAsyncResult _dumpresult = _dumpdelegate.BeginInvoke(SCMS.ServerConnection.ToString(), _rpfilename, _parameters, null, _dumpdelegate);

                AddEvent(BackupEvent.Information, "Restore point creation started.");

                while (!_dumpresult.IsCompleted &&
                       !_cancelled)
                {
                    Thread.Sleep(1); Application.DoEvents();
                }

                if (_cancelled)
                {
                    AddEvent(BackupEvent.Warning, "Cancelling restore point creation...");

                    _trycounter = 0;

                    while (File.Exists(_rpfilename) &&
                           _trycounter <= 30)
                    {
                        try { File.Delete(_rpfilename); }
                        catch { }

                        _trycounter += 1;
                        Thread.Sleep(100); Application.DoEvents();
                    }

                    Materia.RefreshAndManageCurrentProcess();
                    AddEvent(BackupEvent.Information, "Restore point creation cancelled.");
                }
                else
                {
                    MySqlResult _result = _dumpdelegate.EndInvoke(_dumpresult);
                    if (_result.Succeeded)
                    {
                        string    _path  = Application.StartupPath + "\\Xml\\restorepoints.xml";
                        DataTable _table = SCMS.XmlToTable(_path);

                        if (_table != null)
                        {
                            bool _created = false; DataRow _newrow = null;

                            object[]             _values = new object[_table.Columns.Count];
                            DataColumnCollection _cols = _table.Columns;
                            _values[_cols["DateAndTime"].Ordinal] = _serverdatetime;
                            _values[_cols["Filename"].Ordinal]    = _rpfilename;
                            _values[_cols["Company"].Ordinal]     = SCMS.CurrentCompany.Company;
                            _values[_cols["Server"].Ordinal]      = SCMS.ServerConnection.Server;
                            _values[_cols["Database"].Ordinal]    = SCMS.ServerConnection.Database;

                            try
                            {
                                _newrow = _table.Rows.Add(_values);
                                _table.AcceptChanges();
                                _table.WriteXml(_path, XmlWriteMode.WriteSchema);
                                _created = true;
                            }
                            catch (Exception ex)
                            {
                                SCMS.LogError(this.GetType().Name, ex);
                                AddEvent(BackupEvent.Error, "Failed to create and / or complete database restore point.");

                                _trycounter = 0;

                                while (File.Exists(_rpfilename) &&
                                       _trycounter <= 30)
                                {
                                    try { File.Delete(_rpfilename); }
                                    catch { }

                                    _trycounter += 1;
                                    Thread.Sleep(100); Application.DoEvents();
                                }
                            }

                            if (_created)
                            {
                                _restorepoint = new RestorePointInfo(VisualBasic.CLng(_newrow["DetailId"]));
                            }

                            _table.Dispose(); _table = null;
                            Materia.RefreshAndManageCurrentProcess();
                        }
                        else
                        {
                            AddEvent(BackupEvent.Error, "Failed to create and / or complete database restore point.");

                            _trycounter = 0;

                            while (File.Exists(_rpfilename) &&
                                   _trycounter <= 30)
                            {
                                try { File.Delete(_rpfilename); }
                                catch { }

                                _trycounter += 1;
                                Thread.Sleep(100); Application.DoEvents();
                            }

                            Materia.RefreshAndManageCurrentProcess();
                        }
                    }
                    else
                    {
                        SCMS.LogError(this.GetType().Name, new Exception(_result.Error));
                        AddEvent(BackupEvent.Error, "Failed to create and / or complete database restore point.");

                        _trycounter = 0;

                        while (File.Exists(_rpfilename) &&
                               _trycounter <= 30)
                        {
                            try { File.Delete(_rpfilename); }
                            catch { }

                            _trycounter += 1;
                            Thread.Sleep(100); Application.DoEvents();
                        }

                        Materia.RefreshAndManageCurrentProcess();
                    }
                }
            }

            return(_restorepoint);
        }