Beispiel #1
0
        private async Task DownloadBackupAsync()
        {
            DateTime backupDate = await GetBackupDateAsync();

            if (settingsFacade.LastDatabaseUpdate > backupDate)
            {
                return;
            }

            List <string> backups = await cloudBackupService.GetFileNamesAsync();

            if (backups.Contains(DatabaseConstants.BACKUP_NAME))
            {
                using (Stream backupStream = await cloudBackupService.RestoreAsync(DatabaseConstants.BACKUP_NAME,
                                                                                   DatabaseConstants.BACKUP_NAME))
                {
                    fileStore.WriteFile(DatabaseConstants.BACKUP_NAME, backupStream.ReadToEnd());
                }

                bool moveSucceed = fileStore.TryMove(DatabaseConstants.BACKUP_NAME,
                                                     DatabasePathHelper.GetDbPath(),
                                                     true);

                if (!moveSucceed)
                {
                    throw new BackupException("Error Moving downloaded backup file");
                }
                contextAdapter.RecreateContext();
            }
        }
Beispiel #2
0
        public void GetDbPath_Platform_CorrectPath(AppPlatform platform, string expectedPathSegment)
        {
            // Arrange
            ExecutingPlatform.Current = platform;

            // Act
            string result = DatabasePathHelper.GetDbPath();

            // Assert
            result.ShouldContain(expectedPathSegment);
        }
Beispiel #3
0
        private async Task EnqueueBackupTaskAsync(int attempts = 0)
        {
            if (!connectivity.IsConnected)
            {
                throw new NetworkConnectionException();
            }

            logger.Info("Enqueue Backup upload.");

            await semaphoreSlim.WaitAsync(ServiceConstants.BACKUP_OPERATION_TIMEOUT,
                                          cancellationTokenSource.Token);

            try
            {
                if (await cloudBackupService.UploadAsync(fileStore.OpenRead(DatabasePathHelper.GetDbPath())))
                {
                    logger.Info("Upload complete. Release Semaphore.");
                    semaphoreSlim.Release();
                }
                else
                {
                    cancellationTokenSource.Cancel();
                }
            }
            catch (FileNotFoundException ex)
            {
                logger.Error(ex, "Backup failed because database was not found.");
            }
            catch (OperationCanceledException ex)
            {
                logger.Error(ex, "Enqueue Backup failed.");
                await Task.Delay(ServiceConstants.BACKUP_REPEAT_DELAY);
                await EnqueueBackupTaskAsync(attempts + 1);
            }
            catch (ServiceException ex)
            {
                logger.Error(ex, "ServiceException when tried to enqueue Backup.");
                throw;
            }
            catch (BackupAuthenticationFailedException ex)
            {
                logger.Error(ex, "BackupAuthenticationFailedException when tried to enqueue Backup.");
                throw;
            }

            logger.Warn("Enqueue Backup failed.");
        }
Beispiel #4
0
        private async Task <BackupRestoreResult> DownloadBackupAsync(BackupMode backupMode)
        {
            DateTime backupDate = await GetBackupDateAsync();

            if (settingsFacade.LastDatabaseUpdate > backupDate && backupMode == BackupMode.Automatic)
            {
                logger.Info("Local backup is newer than remote. Don't download backup");
                return(BackupRestoreResult.Canceled);
            }

            List <string> backups = await cloudBackupService.GetFileNamesAsync();

            if (backups.Contains(DatabaseConstants.BACKUP_NAME))
            {
                logger.Info("New backup found. Starting download.");
                using (Stream backupStream = await cloudBackupService.RestoreAsync(DatabaseConstants.BACKUP_NAME,
                                                                                   DatabaseConstants.BACKUP_NAME))
                {
                    fileStore.WriteFile(DatabaseConstants.BACKUP_NAME, backupStream.ReadToEnd());
                }

                logger.Info("Backup downloaded. Replace current file.");

                bool moveSucceed = fileStore.TryMove(DatabaseConstants.BACKUP_NAME,
                                                     DatabasePathHelper.GetDbPath(),
                                                     true);

                if (!moveSucceed)
                {
                    throw new BackupException("Error Moving downloaded backup file");
                }

                logger.Info("Recreate database context.");
                contextAdapter.RecreateContext();

                return(BackupRestoreResult.NewBackupRestored);
            }

            return(BackupRestoreResult.BackupNotFound);
        }
Beispiel #5
0
        private async Task DownloadBackupAsync()
        {
            DateTime backupDate = await GetBackupDateAsync();

            if (settingsFacade.LastDatabaseUpdate > backupDate)
            {
                logger.Info("Last local change is after the last adjustment on the remote backup.");
                return;
            }

            List <string> backups = await cloudBackupService.GetFileNamesAsync();

            if (backups.Contains(DatabaseConstants.BACKUP_NAME))
            {
                logger.Info("New backup found. Starting download.");
                using (Stream backupStream = await cloudBackupService.RestoreAsync(DatabaseConstants.BACKUP_NAME,
                                                                                   DatabaseConstants.BACKUP_NAME))
                {
                    fileStore.WriteFile(DatabaseConstants.BACKUP_NAME, backupStream.ReadToEnd());
                }

                logger.Info("Backup downloaded. Replace current file.");

                bool moveSucceed = fileStore.TryMove(DatabaseConstants.BACKUP_NAME,
                                                     DatabasePathHelper.GetDbPath(),
                                                     true);

                if (!moveSucceed)
                {
                    throw new BackupException("Error Moving downloaded backup file");
                }

                logger.Info("Recreate database context.");
                contextAdapter.RecreateContext();
            }
        }
Beispiel #6
0
        private async Task DownloadBackup()
        {
            if (!connectivity.IsConnected)
            {
                return;
            }

            var backups = await cloudBackupService.GetFileNames();

            if (backups.Contains(DatabaseConstants.BACKUP_NAME))
            {
                using (var backupStream = await cloudBackupService.Restore(DatabaseConstants.BACKUP_NAME, DatabaseConstants.BACKUP_NAME))
                {
                    fileStore.WriteFile(DatabaseConstants.BACKUP_NAME, backupStream.ReadToEnd());
                }

                var moveSucceed = fileStore.TryMove(DatabaseConstants.BACKUP_NAME, DatabasePathHelper.GetDbPath(), true);

                if (!moveSucceed)
                {
                    throw new BackupException("Error Moving downloaded backup file");
                }
            }
        }