private void butSynch_Click(object sender, EventArgs e)
        {
            if (textUsername.Text == "")
            {
                MsgBox.Show(this, "Please enter a username first.");
                return;
            }
            if (_listReplicationServers.Count == 0)
            {
                MsgBox.Show(this, "Please add at servers to the list first.");
                return;
            }
            Cursor = Cursors.WaitCursor;
            string         databaseNameOriginal = MiscData.GetCurrentDatabase();
            string         compNameOriginal     = DataConnection.GetServerName();
            DataConnection dc;
            bool           isReplicationSucessfull = true;

            for (int i = 0; i < _listReplicationServers.Count; i++)
            {
                string compName = _listReplicationServers[i].Descript;
                dc = new DataConnection();
                try {
                    dc.SetDb(compName, databaseNameOriginal, textUsername.Text, textPassword.Text, "", "", DataConnection.DBtype);
                    //Connection is considered to be successfull at this point. Now restart the slave process to force replication.
                    string    command     = "STOP SLAVE; START SLAVE; SHOW SLAVE STATUS;";
                    DataTable slaveStatus = dc.GetTable(command);
                    //Wait for the slave process to become active again.
                    for (int j = 0; j < 40 && slaveStatus.Rows[0]["Slave_IO_Running"].ToString().ToLower() != "yes"; j++)
                    {
                        Thread.Sleep(1000);
                        command     = "SHOW SLAVE STATUS";
                        slaveStatus = dc.GetTable(command);
                    }
                    if (slaveStatus.Rows[0]["Slave_IO_Running"].ToString().ToLower() != "yes")
                    {
                        throw new ApplicationException(Lan.g(this, "Slave IO is not running on server") + " " + compName);
                    }
                    if (slaveStatus.Rows[0]["Slave_SQL_Running"].ToString().ToLower() != "yes")
                    {
                        throw new ApplicationException(Lan.g(this, "Slave SQL is not running on server") + " " + compName);
                    }
                    //Wait for replication to complete.
                    while (slaveStatus.Rows[0]["Slave_IO_State"].ToString().ToLower() != "waiting for master to send event" ||
                           slaveStatus.Rows[0]["Seconds_Behind_Master"].ToString() != "0")
                    {
                        slaveStatus = dc.GetTable(command);
                    }
                }
                catch (Exception ex) {
                    Cursor = Cursors.Default;
                    MessageBox.Show(Lan.g(this, "Error forcing synch on server") + " " + compName + ": " + ex.Message);
                    isReplicationSucessfull = false;
                    break;                    //Cancel operation.
                }
            }
            if (isReplicationSucessfull)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(Lan.g(this, "Database synch completed successfully."));
            }
            //Do not leave this function without connecting back to original database, or closing program.
            //At this point we are still connected to the last replication server in the list, try to connect back up to the original database.
            bool isReconnectSuccessful = false;

            while (!isReconnectSuccessful)
            {
                try {
                    //Reconnect back to original database.
                    dc = new DataConnection();
                    dc.SetDb(compNameOriginal, databaseNameOriginal, textUsername.Text, textPassword.Text, "", "", DataConnection.DBtype);
                    isReconnectSuccessful = true;                  //No exception thrown, leave while loop.
                }
                catch (Exception ex) {
                    if (MessageBox.Show(Lan.g(this, "Error reconnecting to server") + " " + compNameOriginal + ": " + ex.Message + "\r\n" +
                                        Lan.g(this, "Would you like to retry?  Cancel will close the program."), "", MessageBoxButtons.OKCancel) != DialogResult.OK)
                    {
                        FormOpenDental.S_ProcessKillCommand();
                    }
                }
            }
        }
Exemple #2
0
        private void butRestore_Click(object sender, System.EventArgs e)
        {
            if (textBackupRestoreFromPath.Text != "" && !textBackupRestoreFromPath.Text.EndsWith("" + Path.DirectorySeparatorChar))
            {
                MessageBox.Show(Lan.g(this, "Paths must end with ") + Path.DirectorySeparatorChar + ".");
                return;
            }
            if (textBackupRestoreToPath.Text != "" && !textBackupRestoreToPath.Text.EndsWith("" + Path.DirectorySeparatorChar))
            {
                MessageBox.Show(Lan.g(this, "Paths must end with ") + Path.DirectorySeparatorChar + ".");
                return;
            }
            if (ShouldUseAtoZFolder())
            {
                if (textBackupRestoreAtoZToPath.Text != "" && !textBackupRestoreAtoZToPath.Text.EndsWith("" + Path.DirectorySeparatorChar))
                {
                    MessageBox.Show(Lan.g(this, "Paths must end with ") + Path.DirectorySeparatorChar + ".");
                    return;
                }
            }
            if (Environment.OSVersion.Platform != PlatformID.Unix)
            {
                //dmg This check will not work on Linux, because mapped drives exist as regular (mounted) paths. Perhaps there
                //is another way to check for this on Linux.
                if (textBackupRestoreToPath.Text != "" && textBackupRestoreToPath.Text.StartsWith("" + Path.DirectorySeparatorChar))
                {
                    MsgBox.Show(this, "The restore database TO folder must be on this computer.");
                    return;
                }
            }
            //pointless to save defaults
            string dbName = MiscData.GetCurrentDatabase();

            if (InnoDb.HasInnoDbTables(dbName))
            {
                //Database has innodb tables. Restore tool does not work on dbs with InnoDb tables.
                MsgBox.Show(this, "InnoDb tables detected. Restore tool cannot run with InnoDb tables.");
                return;
            }
            if (!Directory.Exists(ODFileUtils.CombinePaths(textBackupRestoreFromPath.Text, dbName)))           // D:\opendental
            {
                MessageBox.Show(Lan.g(this, "Restore FROM path is invalid.  Unable to find folder named ") + dbName);
                return;
            }
            if (!Directory.Exists(ODFileUtils.CombinePaths(textBackupRestoreToPath.Text, dbName)))            // C:\mysql\data\opendental
            {
                MessageBox.Show(Lan.g(this, "Restore TO path is invalid.  Unable to find folder named ") + dbName);
                return;
            }
            if (ShouldUseAtoZFolder())
            {
                if (!Directory.Exists(textBackupRestoreAtoZToPath.Text))                 // C:\OpenDentalData\
                {
                    MsgBox.Show(this, "Restore A-Z images TO path is invalid.");
                    return;
                }
                string atozFull = textBackupRestoreAtoZToPath.Text;                                         // C:\OpenDentalData\
                //remove the trailing \
                atozFull = atozFull.Substring(0, atozFull.Length - 1);                                      // C:\OpenDentalData
                string atozDir = atozFull.Substring(atozFull.LastIndexOf(Path.DirectorySeparatorChar) + 1); // OpenDentalData
                if (!Directory.Exists(ODFileUtils.CombinePaths(textBackupRestoreFromPath.Text, atozDir)))   // D:\OpenDentalData
                {
                    MsgBox.Show(this, "Restore A-Z images FROM path is invalid.");
                    return;
                }
            }
            string        fromPath = ODFileUtils.CombinePaths(new string[] { textBackupRestoreFromPath.Text, dbName, "" }); // D:\opendental\
            DirectoryInfo dirInfo  = new DirectoryInfo(fromPath);                                                           //does not check to see if dir exists

            if (MessageBox.Show(Lan.g(this, "Restore from backup created on") + "\r\n"
                                + dirInfo.LastWriteTime.ToString("dddd") + "  " + dirInfo.LastWriteTime.ToString()
                                , "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {
                return;
            }
            Cursor = Cursors.WaitCursor;
            //stop the service--------------------------------------------------------------------------------------
            ServiceController sc = new ServiceController("MySQL");

            if (!ServicesHelper.Stop(sc))
            {
                MsgBox.Show(this, "Unable to stop MySQL service.");
                Cursor = Cursors.Default;
                return;
            }
            //rename the current database---------------------------------------------------------------------------
            //Get a name for the new directory
            string newDb = dbName + "backup_" + DateTime.Today.ToString("MM_dd_yyyy");

            if (Directory.Exists(ODFileUtils.CombinePaths(textBackupRestoreToPath.Text, newDb)))           //if the new database name already exists
            //find a unique one
            {
                int    uniqueID      = 1;
                string originalNewDb = newDb;
                do
                {
                    newDb = originalNewDb + "_" + uniqueID.ToString();
                    uniqueID++;
                }while(Directory.Exists(ODFileUtils.CombinePaths(textBackupRestoreToPath.Text, newDb)));
            }
            //move the current db (rename)
            Directory.Move(ODFileUtils.CombinePaths(textBackupRestoreToPath.Text, dbName)
                           , ODFileUtils.CombinePaths(textBackupRestoreToPath.Text, newDb));
            //Restore----------------------------------------------------------------------------------------------
            string toPath = textBackupRestoreToPath.Text;          // C:\mysql\data\

            Directory.CreateDirectory(ODFileUtils.CombinePaths(toPath, dirInfo.Name));
            FileInfo[] files = dirInfo.GetFiles();
            curVal = 0;          //curVal gets increased
            for (int i = 0; i < files.Length; i++)
            {
                File.Copy(files[i].FullName, ODFileUtils.CombinePaths(new string[] { toPath, dirInfo.Name, files[i].Name }));
            }
            //start the service--------------------------------------------------------------------------------------
            ServicesHelper.Start(sc);
            Cursor = Cursors.Default;
            //restore A-Z folder, and give user a chance to cancel it.
            if (ShouldUseAtoZFolder())
            {
                FormP        = new FormProgress();
                FormP.MaxVal = 100;              //We will be setting maxVal from worker thread.  (double)fileSize/1024;
                FormP.NumberMultiplication = 100;
                FormP.DisplayText          = ""; //We will set the text from the worker thread.
                FormP.NumberFormat         = "N1";
                //start the thread that will perform the database copy
                Thread workerThread = new Thread(new ThreadStart(InstanceMethodRestore));
                workerThread.Start();
                //display the progress dialog to the user:
                FormP.ShowDialog();
                if (FormP.DialogResult == DialogResult.Cancel)
                {
                    workerThread.Abort();
                    return;
                }
            }
            Version programVersionDb  = new Version(PrefC.GetStringNoCache(PrefName.ProgramVersion));
            Version programVersionCur = new Version(Application.ProductVersion);

            if (programVersionDb != programVersionCur)
            {
                MsgBox.Show(this, "The restored database version is different than the version installed and requires a restart.  The program will now close.");
                FormOpenDental.S_ProcessKillCommand();
                return;
            }
            else
            {
                DataValid.SetInvalid(Cache.GetAllCachedInvalidTypes().ToArray());
            }
            MsgBox.Show(this, "Done");
            Close();
            return;
        }