public static bool Restore(Window pSourceWindow, DataBaseRestoreFrom pRestoreFrom) { try { //FrameworkUtils.ShowWaitingCursor(); Init(); bool restoreResult = false; string sql = string.Empty; string fileName = string.Empty; string fileNamePacked = string.Empty; //default pathBackups from Settings, can be Overrided in ChooseFromFilePickerDialog Mode string pathBackups = _pathBackups; DataBaseBackupFileInfo fileInfo = null; Guid systemBackupGuid = Guid.Empty; //Required to assign current FileName and FileNamePacked after restore, else name will be the TempName ex acegvpls.soj & n2sjiamk.32o sys_systembackup systemBackup = null; string currentFileName = string.Empty, currentFileNamePacked = string.Empty, currentFilePath = string.Empty, currentFileHash = string.Empty; sys_userdetail currentUserDetail = null; switch (pRestoreFrom) { case DataBaseRestoreFrom.SystemBackup: fileInfo = GetSelectRecordFileName(pSourceWindow); //Equal to Filename not FileNamePacked fileNamePacked = fileInfo.FileNamePacked; if (_debug) { _log.Debug(string.Format("RestoreBackup: FileNamePacked:[{0}], FileHashDB:[{1}], FileHashFile:[{2}] FileHashValid:[{3}]", fileInfo.FileNamePacked, fileInfo.FileHashDB, fileInfo.FileHashFile, fileInfo.FileHashValid)); } if (fileInfo.Response != ResponseType.Cancel && !fileInfo.FileHashValid) { //#EQUAL#1 string message = string.Format(resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "dialog_message_database_restore_error_invalid_backup_file"), fileNamePacked); Utils.ShowMessageTouch(pSourceWindow, DialogFlags.Modal, new Size(600, 300), MessageType.Error, ButtonsType.Ok, resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "global_error"), message); return(false); } break; case DataBaseRestoreFrom.ChooseFromFilePickerDialog: FileFilter fileFilterBackups = Utils.GetFileFilterBackups(); PosFilePickerDialog dialog = new PosFilePickerDialog(pSourceWindow, DialogFlags.DestroyWithParent, fileFilterBackups, FileChooserAction.Open); ResponseType response = (ResponseType)dialog.Run(); if (response == ResponseType.Ok) { fileNamePacked = dialog.FilePicker.Filename; //Override Default pathBackups pathBackups = string.Format("{0}/", Path.GetDirectoryName(fileNamePacked)); dialog.Destroy(); } else { /* IN009164 */ dialog.Destroy(); return(false); } break; default: break; } if (GlobalFramework.DatabaseType != DatabaseType.MSSqlServer) { fileName = Path.ChangeExtension(fileNamePacked, _fileExtension); } else { //Require to assign filename and packed filename from fileInfo fileName = fileInfo.FileName; fileNamePacked = fileInfo.FileName; } if (fileName != string.Empty) { if (_debug) { _log.Debug(string.Format("Restore Filename:[{0}] to pathBackups[{1}]", fileNamePacked, pathBackups)); } if (GlobalFramework.DatabaseType != DatabaseType.MSSqlServer) { // Old Method before PluginSoftwareVendor Implementation //restoreResult = Utils.ZipUnPack(fileNamePacked, pathBackups, true); restoreResult = GlobalFramework.PluginSoftwareVendor.RestoreBackup(SettingsApp.SecretKey, fileNamePacked, pathBackups, true); if (_debug) { _log.Debug(string.Format("RestoreBackup: unpackResult:[{0}]", restoreResult)); } } if (restoreResult || GlobalFramework.DatabaseType == DatabaseType.MSSqlServer) { //Get properties from SystemBackup Object before Restore, to Assign Properties after Restore (FilePath,FileHash,User,Terminal) sql = string.Format("SELECT Oid FROM sys_systembackup WHERE fileNamePacked = '{0}';", Path.GetFileName(fileNamePacked)); systemBackupGuid = FrameworkUtils.GetGuidFromQuery(sql); if (systemBackupGuid != Guid.Empty) { systemBackup = (sys_systembackup)FrameworkUtils.GetXPGuidObject(GlobalFramework.SessionXpo, typeof(sys_systembackup), systemBackupGuid); currentFileName = systemBackup.FileName; currentFileNamePacked = systemBackup.FileNamePacked; currentFilePath = systemBackup.FilePath; currentFileHash = systemBackup.FileHash; currentUserDetail = systemBackup.User; } //Send fileNamePacked only to show its name in success dialog if (Restore(pSourceWindow, fileName, fileNamePacked, systemBackup)) { //Audit DATABASE_RESTORE FrameworkUtils.Audit("DATABASE_RESTORE", string.Format(resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "audit_message_database_restore"), fileNamePacked)); //Required to DropIdentity before get currentDocumentFinanceYear Object, else it exists in old non restored Session GlobalFramework.SessionXpo.DropIdentityMap(); //Get Current Active FinanceYear fin_documentfinanceyears currentDocumentFinanceYear = ProcessFinanceDocumentSeries.GetCurrentDocumentFinanceYear(); //Disable all Active FinanceYear Series and SeriesTerminal if Exists if (currentDocumentFinanceYear != null) { ProcessFinanceDocumentSeries.DisableActiveYearSeriesAndTerminalSeries(currentDocumentFinanceYear); } //Restore SystemBackup properties else it keeps temp names after Restore acegvpls.soj & n2sjiamk.32o, empty hash etc if (systemBackupGuid != Guid.Empty) { //ReFresh UserDetail from Repository currentUserDetail = (currentUserDetail != null) ? (sys_userdetail)FrameworkUtils.GetXPGuidObject(GlobalFramework.SessionXpo, typeof(sys_userdetail), currentUserDetail.Oid) : null; //Get Current Restored systemBackup Object systemBackup = (sys_systembackup)FrameworkUtils.GetXPGuidObject(GlobalFramework.SessionXpo, typeof(sys_systembackup), systemBackupGuid); systemBackup.FileName = currentFileName; systemBackup.FileNamePacked = currentFileNamePacked; systemBackup.FilePath = currentFilePath; systemBackup.FileHash = currentFileHash; systemBackup.User = currentUserDetail; systemBackup.Save(); } //If Cant get Record, because works on ChooseFromFilePickerDialog, we must recreate Record from file, only in the case of record with miss fileNamePacked else { sql = "DELETE FROM sys_systembackup WHERE FilePath IS NULL AND FileHash IS NULL AND User IS NULL;"; GlobalFramework.SessionXpo.ExecuteNonQuery(sql); } //Audit FrameworkUtils.Audit("APP_CLOSE"); //Call QuitWithoutConfirmation without Audit LogicPos.QuitWithoutConfirmation(false); return(true); } else { return(false); } } else { //#EQUAL#1 string message = string.Format(resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "dialog_message_database_restore_error_invalid_backup_file"), fileNamePacked); Utils.ShowMessageTouch(pSourceWindow, DialogFlags.Modal, new Size(600, 300), MessageType.Error, ButtonsType.Ok, resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "global_error"), message); return(false); } } else { return(false); } } catch (Exception ex) { _log.Error(ex.Message, ex); return(false); } finally { //FrameworkUtils.HideWaitingCursor(); } }
/* ERR201810#15 - Database backup issues */ //public static bool Backup() //{ // return Backup(null); //} public static bool Backup(Window pSourceWindow) { bool backupResult = true; string fileName = string.Empty; string fullFileNamePacked = string.Empty; string fileHash = string.Empty; Thread thread; try { //FrameworkUtils.ShowWaitingCursor(); Init(); /* IN009164 - Begin */ string xpoConnectionString = string.Format(GlobalFramework.Settings["xpoConnectionString"], GlobalFramework.DatabaseName.ToLower()); XpoDefault.DataLayer = XpoDefault.GetDataLayer(xpoConnectionString, AutoCreateOption.None); Session SessionXpoForBackupPurposes = new Session(XpoDefault.DataLayer) { LockingOption = LockingOption.None }; _log.Debug(string.Format("bool Backup(Window pSourceWindow) :: Init XpoDefault.DataLayer [ {0} ]", SessionXpoForBackupPurposes.ToString())); /* IN009164 - End */ //Initialize object before start Actions, to allocate database (automatic backups) and assign CreatedAt, this way next Terminal Skip Backup when trigger backup event sys_systembackup systemBackup = new sys_systembackup(SessionXpoForBackupPurposes) { FileName = Path.GetRandomFileName(), FileNamePacked = Path.GetRandomFileName(), DataBaseType = GlobalFramework.DatabaseType, Version = FrameworkUtils.GetNextTableFieldID("sys_systembackup", "Version", false), Terminal = (pos_configurationplaceterminal)SessionXpoForBackupPurposes.GetObjectByKey(typeof(pos_configurationplaceterminal), GlobalFramework.LoggedTerminal.Oid) }; systemBackup.Save(); switch (GlobalFramework.DatabaseType) { case DatabaseType.MonoLite: case DatabaseType.SQLite: fileName = GetBackupFileName(_fileExtension, systemBackup.Version, ""); //Non Thread //resultBackup = BackupSQLite(fileName); //Thread thread = new Thread(() => backupResult = BackupSQLite(fileName)); Utils.ThreadStart(pSourceWindow, thread); break; case DatabaseType.MSSqlServer: fileName = GetBackupFileName(_fileExtension, systemBackup.Version, ""); //Non Thread //resultBackup = BackupMSSqlServer(fileName); //Thread thread = new Thread(() => backupResult = BackupMSSqlServer(Path.GetFileName(fileName), SessionXpoForBackupPurposes)); Utils.ThreadStart(pSourceWindow, thread); break; case DatabaseType.MySql: fileName = GetBackupFileName(_fileExtension, systemBackup.Version, ""); //Non Thread //resultBackup = BackupMySql(_backupConnectionString, fileName); //Thread thread = new Thread(() => backupResult = BackupMySql(_backupConnectionString, fileName)); Utils.ThreadStart(pSourceWindow, thread); break; default: break; } /* IN007007 */ _log.Debug(string.Format("Backup DatabaseType: [ {0} ] to FileName: [ {1} ], resultBackup:[ {2} ]", GlobalFramework.DatabaseType, fileName, backupResult)); if (_debug) { _log.Debug(string.Format("Backup DatabaseType: [ {0} ] to FileName: [ {1} ], resultBackup:[ {2} ]", GlobalFramework.DatabaseType, fileName, backupResult)); } if (backupResult) { //Update SystemBackup after Backup systemBackup.FileName = Path.GetFileName(fileName); systemBackup.FilePath = Path.GetDirectoryName(fileName); //Extra Protection for System Automatic Backups, with unlogged users sys_userdetail userDetail = (GlobalFramework.LoggedUser != null) ? (sys_userdetail)SessionXpoForBackupPurposes.GetObjectByKey(typeof(sys_userdetail), GlobalFramework.LoggedUser.Oid) : null; if (userDetail != null) { systemBackup.User = userDetail; } //Non MSSqlServer Work: Cant Get Remote File Sizes, Hash etc from LPDev Backups if (GlobalFramework.DatabaseType != DatabaseType.MSSqlServer) { //systemBackup.FileSize = new FileInfo(fileName).Length; systemBackup.FileNamePacked = Path.ChangeExtension(systemBackup.FileName, SettingsApp.BackupExtension); //Compress File : Required OSSlash fullFileNamePacked = FrameworkUtils.OSSlash(string.Format(@"{0}\{1}", systemBackup.FilePath, systemBackup.FileNamePacked)); // Old Method before PluginSoftwareVendor Implementation //backupResult = Utils.ZipPack(new string[] { fileName }, fullFileNamePacked); backupResult = GlobalFramework.PluginSoftwareVendor.BackupDatabase(SettingsApp.SecretKey, new string[] { fileName }, fullFileNamePacked); // Add FileHash if (backupResult) { systemBackup.FileHash = FrameworkUtils.MD5HashFile(fullFileNamePacked); } } //MSSqlServer else { //Must Assign FileName to FileNamePacked else next backup violate iFileNamePacked_SystemBackup Index systemBackup.FileNamePacked = systemBackup.FileName; } if (backupResult) { //Commit Object systemBackup.Save(); //Remove Temporary Backup /* ERR201810#15 - Database backup issues */ //try { if (File.Exists(fileName)) { File.Delete(fileName); } } //catch (Exception ex) { _log.Error(ex.Message, ex); } //Post Backup FrameworkUtils.Audit("DATABASE_BACKUP", string.Format(resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "audit_message_database_backup"), (fullFileNamePacked != string.Empty) ? fullFileNamePacked : systemBackup.FileNamePacked )); //Moved to Thread Outside > Only Show if not in Silence Mode if (pSourceWindow != null) { Utils.ShowMessageTouch(pSourceWindow, DialogFlags.Modal, _sizeDialog, MessageType.Info, ButtonsType.Close, resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "global_information"), string.Format(resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "dialog_message_database_backup_successfully"), systemBackup.FileNamePacked)); } } else { //On Error Delete Object form Database, this way we dont have a invalid Backup systemBackup.Delete(); /* * IN007007 * * This implementation covers only "non-DatabaseType.MSSqlServer" database, when calling the method to secure-compact database file: * > GlobalFramework.PluginSoftwareVendor.BackupDatabase...Utils.ZipPack * * Please note that variable "backupResult" never changes its value when "DatabaseType.MSSqlServer", therefore is not being covered by this. */ // Show only when "Silent Mode" is on if (pSourceWindow != null) { Utils.ShowMessageTouch(pSourceWindow, DialogFlags.Modal, _sizeDialog, MessageType.Warning, ButtonsType.Close, resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "global_information"), string.Format(resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "dialog_message_database_backup_error_when_secure_compacting"), systemBackup.FileNamePacked)); } _log.Debug($"DataBaseBackup.Backup(Window pSourceWindow): {string.Format(resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "dialog_message_database_backup_error_when_secure_compacting"), systemBackup.FileNamePacked)}"); } } else { //Moved to Thread Outside > Only Show if not in Silence Mode if (pSourceWindow != null) { Utils.ShowMessageTouch(pSourceWindow, DialogFlags.Modal, _sizeDialog, MessageType.Error, ButtonsType.Close, resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "global_error"), string.Format(resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "dialog_message_database_backup_error"), Path.GetFileName(fileName))); } } /* IN009164 */ SessionXpoForBackupPurposes.Disconnect(); } catch (Exception ex) { _log.Error("bool Backup(Window pSourceWindow) :: Error during backup process: " + ex.Message, ex); // _log.Error("bool Backup(Window pSourceWindow) :: Error during backup process on Session [ " + SessionXpoForBackupPurposes.ToString() + " ]: " + ex.Message, ex); // SessionXpoForBackupPurposes.Disconnect(); } return(backupResult); }