void AddOrUpdateBaks(IEnumerable <string> bakupFilePaths) { if (_isBackingUp) { return; } _isBackingUp = true; foreach (var f in bakupFilePaths) { MigrateIfRequired(f); } #pragma warning disable IDE0067 // Dispose objects before losing scope BackgroundWorker worker = new BackgroundWorker() { WorkerReportsProgress = true }; #pragma warning restore IDE0067 // Dispose objects before losing scope worker.DoWork += SyncronisationResult.Sync; if (UpdateProgress != null) { worker.ProgressChanged += UpdateProgress; } bool isForUpdate = ParticipantAdded != null || ParticipantUpdated != null || ScreenedPatientAdded != null; if (isForUpdate) { worker.RunWorkerCompleted += WhenSyncronisationResultsAvailable; } if (AnyParticipantChange != null) { worker.RunWorkerCompleted += (o, e) => AnyParticipantChange(this, new LastUpdatedChangedEventAgs(LastCreateModifyParticipant())); } if (DatabaseUpdating != null) { DatabaseUpdating(this, new DatabaseUpdatingEventAgs(true)); worker.RunWorkerCompleted += (o, e) => DatabaseUpdating(this, new DatabaseUpdatingEventAgs(false)); } worker.RunWorkerCompleted += (o, e) => _isBackingUp = false; worker.RunWorkerCompleted += (o, e) => { string fn = App.DataDirectory + '\\' + _dbContext.DbName + _bakExtension; _dbContext.Dispose(); SyncronisationResult.RepairDb(fn); _dbContext = _createContext.Invoke(); worker.Dispose(); }; worker.RunWorkerAsync(new SyncronisationResult.SyncArgs { DestContext = _dbContext, DbFileNames = bakupFilePaths.ToList(), UpdateResults = isForUpdate }); }
public void Backup() { string bakFileName = _dbContext.BackupDb(); FileInfo bakFile = new FileInfo(bakFileName); if (!bakFile.Exists) { return; } #if DEBUG if (!(CloudDirectories.Count() == 1)) { throw new InvalidOperationException("Backup called without cloud directories set to a single directory"); } #endif string uniqueFileNameSuffix = '_' + LocalStudyCentres.First().DuplicateIdCheck.ToString("N"); string cloudDir = CloudDirectories.First(); string cloudZipName = cloudDir + '\\' + Path.GetFileNameWithoutExtension(bakFileName) + uniqueFileNameSuffix + ".zip"; var cloudFile = new FileInfo(cloudZipName); if (cloudFile.Exists) { DateTime?mostRecentEntry = SyncronisationResult.MostRecentEntry(_dbContext); if (mostRecentEntry == null || cloudFile.LastWriteTimeUtc >= mostRecentEntry) { return; } } _dbContext.Dispose(); // only necessary for ce SyncronisationResult.RepairDb(bakFileName); int dotPos = bakFileName.LastIndexOf('.'); string copiedFileName = bakFileName.Insert(dotPos, uniqueFileNameSuffix); File.Copy(bakFileName, copiedFileName, true); void work() { BackupHelper.ZipVerifyAndPutInCloudDir(copiedFileName, cloudDir); } new Thread(work).Start(); _dbContext = _createContext.Invoke(); }