Ejemplo n.º 1
0
        private void BackupAction(PwDatabase database)
        {
            // don't perform backup if configuration isn't finished
            if (!this.m_config.BackupConfigured)
            {
                return;
            }

            // start stopwatch to measure time needed for the backup
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            IStatusLogger swLogger = this.m_host.MainWindow.CreateShowWarningsLogger();

            try
            {
                m_host.MainWindow.UIBlockInteraction(true);
                bool warnings = false;

                BackupManager.SetKPMainWindowSwLogger(swLogger);
                swLogger.SetText("KPSimpleBackup: Backup started...", LogStatusType.Info);
                m_PluginLogger.Log("KPSimpleBackup: Backup started...", LogStatusType.Info);

                BasicBackupManager basicBackupManager = new BasicBackupManager(database);
                warnings = !basicBackupManager.Run() || warnings;

                // perform long term backup if enabled in settings
                if (m_config.UseLongTermBackup)
                {
                    LongTermBackupManager ltbManager = new LongTermBackupManager(database);
                    if (basicBackupManager.lastBackupFilePath != null)
                    {
                        ltbManager.SetTempDatabaseBackupFile(basicBackupManager.lastBackupFilePath);
                    }
                    warnings = !ltbManager.Run() || warnings;
                }

                // reset database modified property, as an up-to-date backup has now been created
                m_databaseModifiedAfterLastBackup = false;

                // backup KeePass configuration if enabled in settings
                if (m_config.BackupKeePassConfig)
                {
                    KPConfigBackupManager kPConfigBackupManager = new KPConfigBackupManager(database);
                    warnings = !kPConfigBackupManager.Run() || warnings;
                }

                if (warnings)
                {
                    swLogger.SetText("KPSimpleBackup: Backup finished with warnings, consider checking the logs!", LogStatusType.Info);
                    if (m_config.ShowBackupFailedWarning)
                    {
                        MessageService.ShowWarning(
                            "KPSimpleBackup: Backup finished with warnings, check the logs for details!"
                            );
                    }
                }
                else
                {
                    swLogger.SetText("KPSimpleBackup: Backup finished!", LogStatusType.Info);
                }
            }
            catch (Exception e)
            {
                swLogger.EndLogging();
                swLogger.SetText("KPSimpleBackup: Backup failed, see logs for details!", LogStatusType.Error);

                m_PluginLogger.Log("Could not backup database! Error:", LogStatusType.Error);
                m_PluginLogger.Log(e.ToString(), LogStatusType.Error);
            }
            finally
            {
                m_host.MainWindow.UIBlockInteraction(false);
                stopWatch.Stop();
                m_PluginLogger.Log("KPSimpleBackup: Finished in " + stopWatch.ElapsedMilliseconds + " ms.", LogStatusType.Info);
            }
        }