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; }
private void butBackup_Click(object sender, System.EventArgs e) { if (!IsBackupTabValid()) { return; } //Ensure that the backup from and backup to paths are different. This is to prevent the live database //from becoming corrupt. if (this.textBackupFromPath.Text.Trim().ToLower() == this.textBackupToPath.Text.Trim().ToLower()) { MsgBox.Show(this, "The backup from path and backup to path must be different."); return; } //test saving defaults if (textBackupFromPath.Text != PrefC.GetString(PrefName.BackupFromPath) || textBackupToPath.Text != PrefC.GetString(PrefName.BackupToPath) || textBackupRestoreFromPath.Text != PrefC.GetString(PrefName.BackupRestoreFromPath) || textBackupRestoreToPath.Text != PrefC.GetString(PrefName.BackupRestoreToPath) || textBackupRestoreAtoZToPath.Text != PrefC.GetString(PrefName.BackupRestoreAtoZToPath)) { if (MsgBox.Show(this, MsgBoxButtons.YesNo, "Set as default?") && SaveTabPrefs()) { DataValid.SetInvalid(InvalidType.Prefs); } } string dbName = MiscData.GetCurrentDatabase(); if (InnoDb.HasInnoDbTables(dbName)) { //Database has innodb tables. Backup tool does not work on dbs with InnoDb tables. MsgBox.Show(this, "InnoDb tables detected. Backup tool cannot run with InnoDb tables."); return; } if (!Directory.Exists(ODFileUtils.CombinePaths(textBackupFromPath.Text, dbName))) // C:\mysql\data\opendental { MsgBox.Show(this, "Backup FROM path is invalid."); return; } if (!Directory.Exists(textBackupToPath.Text)) // D:\ { MsgBox.Show(this, "Backup TO path is invalid."); return; } _errorMessage = ""; 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(InstanceMethodBackup)); workerThread.Start(); //display the progress dialog to the user: FormP.ShowDialog(); if (FormP.DialogResult == DialogResult.Cancel) { workerThread.Abort(); return; } if (_errorMessage == "") { SecurityLogs.MakeLogEntry(Permissions.Backup, 0, Lan.g(this, "Database backup created at ") + textBackupToPath.Text); MessageBox.Show(Lan.g(this, "Backup complete.")); } else //Backup failed for some reason. { MessageBox.Show(_errorMessage); } Close(); }