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

            // Get extension/ fileending for the database backup-file
            string databaseExtension = KPSimpleBackupConfig.DEFAULT_BACKUP_FILE_EXTENSION;

            if (this.m_config.UseCustomBackupFileExtension)
            {
                databaseExtension = this.m_config.BackupFileExtension;
            }
            else
            {
                string databasePath = database.IOConnectionInfo.Path;
                databaseExtension = Path.GetExtension(databasePath);
            }

            List <String> paths          = this.m_config.BackupPath;
            String        dateTimeFormat = this.m_config.DateFormat;
            string        time           = DateTime.Now.ToString(dateTimeFormat);

            // Save backup database for each specified path and perform cleanup
            foreach (String backupFolderPath in paths)
            {
                try
                {
                    string dbBackupFileName = this.GetBackupFileName(database);
                    string path             = FILE_PREFIX + backupFolderPath + dbBackupFileName + "_" + time + databaseExtension;

                    this.m_logger.Log("Start backup of database '" + database.Name + "' to path: " + path, LogStatusType.Info);

                    IOConnectionInfo connection = IOConnectionInfo.FromPath(path);
                    database.SaveAs(connection, false, this.m_logger);

                    // Cleanup
                    string cleanupSearchPattern = dbBackupFileName + "_*" + databaseExtension;
                    this.Cleanup(backupFolderPath, cleanupSearchPattern, database.IOConnectionInfo.Path);

                    // perform long term backup if enabled in settings
                    if (this.m_config.UseLongTermBackup)
                    {
                        this.m_logger.Log("Perform LTB-Backup...", LogStatusType.Info);

                        LongTermBackupManager ltbManager = new LongTermBackupManager(backupFolderPath, dbBackupFileName, databaseExtension, database, m_config);
                        ltbManager.SetLogger(this.m_logger);
                        ltbManager.RunLtb();

                        this.m_logger.Log("Finished LTB-Backup...", LogStatusType.Info);
                    }

                    this.m_logger.Log("Finished backup of database '" + database.Name + "' to path: " + path, LogStatusType.Info);
                }
                catch (Exception e)
                {
                    this.m_logger.Log("Could not backup database!", LogStatusType.Error);
                }
            }
        }
Ejemplo n.º 2
0
        private void BackupAction(PwDatabase database)
        {
            // don't perform backup if configuration isn't finished
            if (!this.m_config.BackupConfigured)
            {
                return;
            }

            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);
                    warnings = !ltbManager.Run() || warnings;
                }

                // 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);
                m_PluginLogger.Log("KPSimpleBackup: Finished", LogStatusType.Info);
            }
        }
Ejemplo n.º 3
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);
            }
        }