private void DeleteOldEntries(string fileName, int overflow) { var backupFileName = fileName + ".bak"; try { if (_fileStoreService.Exists(backupFileName)) { _fileStoreService.DeleteFile(backupFileName); } // Creating a backup copy of the log in case a crash happends. _fileStoreService.TryMove(fileName, backupFileName, false); // read the content of file and remove old entries string fileContentString = string.Empty; _fileStoreService.TryReadTextFile(fileName, out fileContentString); var logContent = RemoveOlderEntries(fileContentString, overflow); // delete current log file en rewrite it without old entries _fileStoreService.DeleteFile(fileName); _fileStoreService.WriteFile(fileName, x => GenerateStreamFromString(logContent)); // deleting the backup copy _fileStoreService.DeleteFile(backupFileName); } catch (Exception ex) { LogError(ex); } }
/// <inheritdoc /> public async Task RestoreBackup() { if (!connectivity.IsConnected) { return; } var backups = await backupService.GetFileNames(); if (backups.Contains(DatabaseConstants.BACKUP_NAME)) { var backupStream = await backupService.Restore(DatabaseConstants.BACKUP_NAME, DatabaseConstants.BACKUP_NAME); fileStore.WriteFile(DatabaseConstants.BACKUP_NAME, backupStream.ReadToEnd()); var moveSucceed = fileStore.TryMove(DatabaseConstants.BACKUP_NAME, ApplicationContext.DbPath, true); if (!moveSucceed) { throw new BackupException("Error Moving downloaded backup file"); } settingsManager.LastDatabaseUpdate = DateTime.Now; } }
private async Task DownloadBackup() { if (!connectivity.IsConnected) { return; } var backups = await cloudBackupService.GetFileNames() .ConfigureAwait(false); if (backups.Contains(DatabaseConstants.BACKUP_NAME)) { var backupStream = await cloudBackupService.Restore(DatabaseConstants.BACKUP_NAME, DatabaseConstants.BACKUP_NAME) .ConfigureAwait(false); fileStore.WriteFile(DatabaseConstants.BACKUP_NAME, backupStream.ReadToEnd()); var moveSucceed = fileStore.TryMove(DatabaseConstants.BACKUP_NAME, EfCoreContext.DbPath, true); if (!moveSucceed) { throw new BackupException("Error Moving downloaded backup file"); } } }
/// <inheritdoc /> public async Task RestoreBackup() { if (!connectivity.IsConnected) { return; } var backups = await backupService.GetFileNames(); // Dispose dbfactory to release the dbFile. dbFactory.Dispose(); if (backups.Contains(DatabaseConstants.BACKUP_NAME)) { var backupStream = await backupService.Restore(DatabaseConstants.BACKUP_NAME, DatabaseConstants.BACKUP_NAME); fileStore.WriteFile(DatabaseConstants.BACKUP_NAME, backupStream.ReadToEnd()); var moveSucceed = fileStore.TryMove(DatabaseConstants.BACKUP_NAME, DatabaseConstants.DB_NAME, true); if (!moveSucceed) { throw new BackupException("Error Moving downloaded backup file"); } } else if (backups.Contains(DatabaseConstants.BACKUP_NAME_OLD)) { var backupStream = await backupService.Restore(DatabaseConstants.BACKUP_NAME_OLD, DatabaseConstants.BACKUP_NAME_OLD); fileStore.WriteFile(DatabaseConstants.BACKUP_NAME_OLD, backupStream.ReadToEnd()); // Execute migration await dbFactory.Init(); fileStore.TryMove(DatabaseConstants.BACKUP_NAME_OLD, DatabaseConstants.DB_NAME_OLD, true); await dbFactory.MigrateOldDatabase(); fileStore.DeleteFile(DatabaseConstants.DB_NAME_OLD); } dbFactory.Dispose(); settingsManager.LastDatabaseUpdate = DateTime.Now; }
/// <summary> /// Restores an existing backup from the backupservice. /// </summary> public async Task RestoreBackup() { if (!connectivity.IsConnected) { return; } var backupStream = await backupService.Restore(DatabaseConstants.BACKUP_NAME, DatabaseConstants.BACKUP_NAME); fileStore.WriteFile(DatabaseConstants.BACKUP_NAME, backupStream.ReadToEnd()); var moveSucceed = fileStore.TryMove(DatabaseConstants.BACKUP_NAME, DatabaseConstants.DB_NAME, true); if (!moveSucceed) { throw new BackupException("Error Moving downloaded backup file"); } await dbFactory.Init(); settingsManager.LastDatabaseUpdate = DateTime.Now; }
/// <summary> /// Restores an existing backup from the backupservice. /// If it was an old backup, it will delete the existing db an make an migration. /// After the restore it will perform a reload of the data so that the cache works with the new data. /// </summary> public async Task RestoreBackup() { if (!connectivity.IsConnected) { return; } await backupService.Restore(DatabaseConstants.BACKUP_NAME, DatabaseConstants.BACKUP_NAME); var moveSucceed = fileStore.TryMove(DatabaseConstants.BACKUP_NAME, DatabaseConstants.DB_NAME, true); if (!moveSucceed) { throw new BackupException("Error Moving downloaded backup file"); } databaseManager.CreateDatabase(); PaymentRepository.IsCacheMarkedForReload = true; settingsManager.LastDatabaseUpdate = DateTime.Now; }