public void SyncFileSystemReplicasOneWay( SyncId sourceReplicaId, SyncId destinationReplicaId, string sourceReplicaRootPath, string destinationReplicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { using (var sourceProvider = createFileSyncProvider( sourceReplicaId.GetGuidId(), sourceReplicaRootPath, filter, options)) using (var destinationProvider = createFileSyncProvider( destinationReplicaId.GetGuidId(), destinationReplicaRootPath, filter, options)) { destinationProvider.AppliedChange += destinationProvider_AppliedChange; destinationProvider.SkippedChange += destinationProvider_SkippedChange; sourceProvider.DetectChanges(); destinationProvider.DetectChanges(); synchronizeProviders(destinationProvider, sourceProvider); } }
public static void SyncFileSystemReplicasOneWay( string sourceReplicaRootPath, string destinationReplicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { FileSyncProvider sourceProvider = null; FileSyncProvider destinationProvider = null; try { sourceProvider = new FileSyncProvider( sourceReplicaRootPath, filter, options); destinationProvider = new FileSyncProvider( destinationReplicaRootPath, filter, options); destinationProvider.AppliedChange += new EventHandler<AppliedChangeEventArgs>(OnAppliedChange); destinationProvider.SkippedChange += new EventHandler<SkippedChangeEventArgs>(OnSkippedChange); SyncOrchestrator agent = new SyncOrchestrator(); agent.LocalProvider = sourceProvider; agent.RemoteProvider = destinationProvider; agent.Direction = SyncDirectionOrder.Upload; // Sync source to destination Console.WriteLine("Synchronizing changes to replica: " + destinationProvider.RootDirectoryPath); agent.Synchronize(); } finally { // Release resources if (sourceProvider != null) sourceProvider.Dispose(); if (destinationProvider != null) destinationProvider.Dispose(); } }
public bool Sync(string sDir, string tDir, FileSyncScopeFilter filter, FileSyncOptions options) { try { _fileEngine.CheckTargetDirExists(tDir); var SourceProvider = _fileEngine.CreateProvider(sDir, filter, options); var DestinationProvider = _fileEngine.CreateProvider(tDir, filter, options); _fileEngine.DetectChanges(SourceProvider); SourceProvider = _fileEngine.CreateProvider(sDir, filter, options); SourceProvider = _fileEngine.SetPreviewModeFlag(SourceProvider, true); DestinationProvider = _fileEngine.SetPreviewModeFlag(DestinationProvider, true); DestinationProvider = _fileEngine.AttachApplyingChangeEventHandler(DestinationProvider); DestinationProvider = _fileEngine.AttachAppliedChangeEventHandler(DestinationProvider); DestinationProvider = _fileEngine.AttachSkippedChangeEventHandler(DestinationProvider); var agent = _fileEngine.CreateSyncOrchestrator(SourceProvider, DestinationProvider); agent = _fileEngine.SetSyncDirection(agent); _fileEngine.Synchronize(agent); return true; } catch (Exception e) { // log teh error mon return false; } }
public void SyncFolders(string sourceFolder, string targetFolder) { if (sourceFolder.Equals("")) return; const string idFileName = "filesync.id"; var sourceId = getOrCreateReplicaId(Path.Combine(sourceFolder, idFileName)); var targetId = getOrCreateReplicaId(Path.Combine(targetFolder, idFileName)); // Set options for the sync operation var options = FileSyncOptions.ExplicitDetectChanges; var filter = new FileSyncScopeFilter(); filter.FileNameExcludes.Add(idFileName); // Exclude the id file // Explicitly detect changes on both replicas upfront, to avoid two change // detection passes for the two-way sync DetectChangesOnFileSystemReplica( sourceId, sourceFolder, filter, options); DetectChangesOnFileSystemReplica( targetId, targetFolder, filter, options); // Sync SyncFileSystemReplicasOneWay( sourceId, targetId, sourceFolder, targetFolder, filter, options); }
/// <summary> /// Starts the synchronization job /// </summary> /// <returns>Returns a boolean to indicate if the synchronization was successful</returns> private bool InternalStartSync() { try { // Configure sync options FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleConflictLoserFiles | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates; // Configure sync filters FileSyncScopeFilter filter = new FileSyncScopeFilter(); // Update metadata of the folders before sync to // check for any changes or modifications DetectChangesonFileSystemReplica(leftPath, filter, options); DetectChangesonFileSystemReplica(rightPath, filter, options); // Start the 2-way sync SyncFileSystemReplicasOneWay(leftPath, rightPath, null, options, false); SyncFileSystemReplicasOneWay(rightPath, leftPath, null, options, false); return(true); } catch (Exception e) { return(false); } }
static void Main(string[] args) { string fileSourcePath = @"C:\Users\davamix\Downloads\pruebas\source"; string fileTargetPath = @"C:\Users\davamix\Downloads\pruebas\target"; //try //{ FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates | FileSyncOptions.RecycleConflictLoserFiles; FileSyncScopeFilter filter = new FileSyncScopeFilter(); filter.FileNameExcludes.Add("*.info"); DetectChangesOnFileSystemReplica(fileSourcePath, filter, options); DetectChangesOnFileSystemReplica(fileTargetPath, filter, options); SyncFileSystemReplicaOneWay(fileSourcePath, fileTargetPath, null, options); SyncFileSystemReplicaOneWay(fileTargetPath, fileSourcePath, null, options); //} //catch (Exception e) //{ // Console.WriteLine("Exception for FileSync provider\n-> {0}", e.Message); //} Console.WriteLine("FIN"); Console.Read(); }
public void SetLocalSource(string FqDirName) { if (!Directory.Exists(FqDirName)) { Console.WriteLine("Please ensure that the local target directory exists."); throw new ArgumentException("Please ensure that the local target directory exists."); } string _localPathName = FqDirName; FileSyncProvider fileSyncProvider = null; FileSyncScopeFilter filter = new FileSyncScopeFilter(); filter.FileNameExcludes.Add(".*"); filter.FileNameExcludes.Add("filesync.metadata"); // TODO: Exclude subdirectories and remove this hack for (int i = 0; i < 100; i++) { filter.SubdirectoryExcludes.Add("" + i); } try { fileSyncProvider = new FileSyncProvider(_localPathName, filter, new FileSyncOptions()); } catch (ArgumentException) { fileSyncProvider = new FileSyncProvider(Guid.NewGuid(), _localPathName, filter, new FileSyncOptions()); } //fileSyncProvider.ApplyingChange += new EventHandler<ApplyingChangeEventArgs>(DownloadingFile); orchestrator.LocalProvider = fileSyncProvider; }
private void SyncFolders() { this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Syncing Folders: {0} and {1}. Direction: {2}", this.Source.GetMetadata("FullPath"), this.Destination.GetMetadata("FullPath"), this.Direction)); if (!Directory.Exists(this.Destination.GetMetadata("FullPath"))) { this.LogTaskMessage(MessageImportance.Low, string.Format(CultureInfo.CurrentCulture, "Creating Destination Folder: {0}", this.Destination.GetMetadata("FullPath"))); Directory.CreateDirectory(this.Destination.GetMetadata("FullPath")); } Guid sourceSyncId = GetSyncId(Path.Combine(this.Source.GetMetadata("FullPath"), this.IdFileName)); Guid destinationSyncId = GetSyncId(Path.Combine(this.Destination.GetMetadata("FullPath"), this.IdFileName)); FileSyncScopeFilter filter = new FileSyncScopeFilter(); // Exclude the IdFileName by default filter.FileNameExcludes.Add(this.IdFileName); // add any other exclusions if (this.ExclusionFilters != null) { foreach (ITaskItem exfilter in this.ExclusionFilters) { FileInfo f = new FileInfo(exfilter.ItemSpec); this.LogTaskMessage(MessageImportance.Low, string.Format(CultureInfo.CurrentCulture, "Adding ExclusionFilter: {0}", f.Name)); filter.FileNameExcludes.Add(f.Name); } } // Detect Changes this.DetectChanges(sourceSyncId, this.Source.GetMetadata("FullPath"), filter); this.DetectChanges(destinationSyncId, this.Destination.GetMetadata("FullPath"), filter); // Synchronise this.SyncFiles(sourceSyncId, destinationSyncId, filter); }
private void DetectChanges(Guid syncId, string path, FileSyncScopeFilter filter) { using (FileSyncProvider provider = new FileSyncProvider(syncId, path, filter, this.syncOptions)) { provider.DetectChanges(); } }
private void SyncFileOperate(string sourceRootPath, string destRootPath, string tempDir, string trashDir) { FileSyncScopeFilter filter = new FileSyncScopeFilter(); filter.FileNameExcludes.Add("*.metadata"); FileSyncOptions options = FileSyncOptions.None; //DetectChanges DetectChangesOnFileSystemReplica(sourceRootPath, filter, options, sourceRootPath, "filesync.metadata", tempDir, trashDir); DetectChangesOnFileSystemReplica(destRootPath, filter, options, destRootPath, "filesync.metadata", tempDir, trashDir); try { ThreadInteropUtils.OpeMainFormControl(() => { this.richTextBox1.Text += "Start synchronizing..." + Environment.NewLine; }, this); //SyncChanges Both Ways SyncOperationStatistics syncOperationStatistics = null; SyncFileUtils.SyncFileSystemReplicasOneWay(sourceRootPath, destRootPath, filter, options, sourceRootPath, "filesync.metadata", destRootPath, "filesync.metadata", tempDir, trashDir, ref syncOperationStatistics); //ThreadInteropUtils.OpeMainFormControl(() => //{ // this.richTextBox1.Text += "Synchronizing file upload changes... " + syncOperationStatistics.UploadChangesApplied.ToString() + Environment.NewLine; //}, this); } catch (Exception e) { throw e; } }
/// <summary> /// Does Sync operation to store change events into a list of FileData objects /// </summary> private int InternalPreviewSync() { try { fileData = new List <FileData>(); // Configure sync options FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleConflictLoserFiles | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates; // Configure sync filters FileSyncScopeFilter filter = new FileSyncScopeFilter(); filter.SubdirectoryExcludes.Add(TRACKBACK_FOLDER_NAME); filter.FileNameExcludes.Add(METADATA_FILE_NAME); // Add filters for file types for (int i = 0; i < excludeData.ExcludeFileTypeList.Count; i++) { filter.FileNameExcludes.Add("*" + excludeData.ExcludeFileTypeList[i]); } // Add filters for file names for (int i = 0; i < excludeData.ExcludeFileNameList.Count; i++) { filter.FileNameExcludes.Add(excludeData.ExcludeFileNameList[i]); } // Add filters for folders for (int i = 0; i < excludeData.ExcludeFolderList.Count; i++) { filter.SubdirectoryExcludes.Add(excludeData.ExcludeFolderList[i]); } // Update metadata of the folders before sync to // check for any changes or modifications DetectChangesonFileSystemReplica(leftPath, filter, options); DetectChangesonFileSystemReplica(rightPath, filter, options); // Start the 2-way sync SyncFileSystemReplicasOneWay(leftPath, rightPath, null, options); SyncFileSystemReplicasOneWay(rightPath, leftPath, null, options); return(0); } catch (SyncException) { return(1); } catch (DirectoryNotFoundException) { return(2); } catch (Exception) { return(3); } }
private static void Main(string[] args) { const string replica1RootPath = "_R0"; const string replica2RootPath = "_R1"; if (Directory.Exists(replica1RootPath)) { Directory.Delete(replica1RootPath, true); } Directory.CreateDirectory(replica1RootPath); if (Directory.Exists(replica2RootPath)) { Directory.Delete(replica2RootPath, true); } Directory.CreateDirectory(replica2RootPath); try { // Set options for the synchronization operation const FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates | FileSyncOptions.RecycleConflictLoserFiles; var filter = new FileSyncScopeFilter(); //filter.FileNameExcludes.Add("*.lnk"); // Exclude all *.lnk files Guid replica1Guid = Guid.Parse("181517DE-B950-4e62-9582-56F01884288D"); Guid replica2Guid = Guid.Parse("86C9D79E-679D-4d33-A051-AA4EEFF17E55"); using (var provider0 = new FileSyncProvider(replica1Guid, replica1RootPath, filter, options)) { using (var provider1 = new FileSyncProvider(replica2Guid, replica2RootPath, filter, options)) //using (var provider1 = new TestProvider(replica2Guid, Path.Combine(replica2RootPath, "_replica.sdf"))) { var agent = CreateAgent( provider0, provider1 ); while (true) { provider0.DetectChanges(); provider1.DetectChanges(); agent.Synchronize(); Thread.Sleep(25); } } } } catch (Exception e) { Console.WriteLine("\nException from File Synchronization Provider:\n" + e); } }
// Main sync happens here private static void SynchronizeOnce( FileSyncScopeFilter filter, string localPathName, string containerName, CloudStorageAccount storageAccount) { // Setup Provider AzureBlobStore blobStore = new AzureBlobStore(containerName, storageAccount); AzureBlobSyncProvider azureProvider = new AzureBlobSyncProvider(containerName, blobStore); azureProvider.ApplyingChange += new EventHandler <ApplyingBlobEventArgs>(UploadingFile); FileSyncProvider fileSyncProvider = null; if (filter == null) { try { fileSyncProvider = new FileSyncProvider(localPathName); } catch (ArgumentException) { fileSyncProvider = new FileSyncProvider(Guid.NewGuid(), localPathName); } } else { try { fileSyncProvider = new FileSyncProvider(localPathName, filter, FileSyncOptions.None); } catch (ArgumentException) { fileSyncProvider = new FileSyncProvider(Guid.NewGuid(), localPathName, filter, FileSyncOptions.None); } } fileSyncProvider.ApplyingChange += new EventHandler <ApplyingChangeEventArgs>(WindowsAzureBlob2FileSystemSync.DownloadingFile); try { SyncOrchestrator orchestrator = new SyncOrchestrator(); orchestrator.LocalProvider = fileSyncProvider; orchestrator.RemoteProvider = azureProvider; orchestrator.Direction = SyncDirectionOrder.DownloadAndUpload; orchestrator.Synchronize(); } catch (Exception ex) { Trace.TraceError("Failed to Synchronize. Error: {0}", ex.Message); } finally { fileSyncProvider.Dispose(); } }
/// <summary> /// Run method, executed by a dedicated Task to analyze file system changes and start syncing operations /// </summary> private void Run() { // Set options for the synchronization operation FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates; FileSyncScopeFilter filter = new FileSyncScopeFilter(); if (AppSettings.Default.ExcludedFiles.Trim().Length > 0) { var excludedTypes = AppSettings.Default.ExcludedFiles.Split(','); foreach (var exType in excludedTypes) { if (exType != null && exType.Trim().Length > 0) { filter.FileNameExcludes.Add(exType); } } } else { Logger.Log("No file filter specified", LogInfo.Info, VerbosityInfoLevel.V2); } if (AppSettings.Default.ExcludedFolders.Trim().Length > 0) { var excludedFolders = AppSettings.Default.ExcludedFolders.Split(','); foreach (var exFolder in excludedFolders) { if (exFolder != null && exFolder.Trim().Length > 0) { filter.SubdirectoryExcludes.Add(exFolder); } } } else { Logger.Log("No folder filter specified", LogInfo.Info, VerbosityInfoLevel.V2); } while (running) { try { DetectChangesOnFileSystemReplica(AppSettings.Default.Path1, filter, options); DetectChangesOnFileSystemReplica(AppSettings.Default.Path2, filter, options); SyncFileSystemReplicasOneWay(AppSettings.Default.Path1, AppSettings.Default.Path2, filter, options); SyncFileSystemReplicasOneWay(AppSettings.Default.Path2, AppSettings.Default.Path1, filter, options); } catch (Exception e) { Logger.Log("Error on sync provider execution:\n {0}", LogInfo.Error, VerbosityInfoLevel.V1, e.ToString()); } Thread.Sleep(AppSettings.Default.CheckIntervalSec * 1000); } }
public void DetectChangesOnFileSystemReplica( SyncId replicaId, string replicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { using (var provider = createFileSyncProvider(replicaId.GetGuidId(), replicaRootPath, filter, options)) provider.DetectChanges(); }
public void SetScopeFilter(string[] filters) { FileSyncScopeFilter scopeFilter = new FileSyncScopeFilter(); foreach (string filter in filters) { scopeFilter.FileNameIncludes.Add(filter.ToUpperInvariant()); } ScopeFilter = scopeFilter; }
private Guid DetectChanges(string replicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { if (Provider == null) { Provider = GenerateProvider(); } Provider.DetectChanges(); return(Provider.ReplicaId); }
public Settings(string localDir, string remoteDir) { dirLocalSync = localDir + GetDirLocalName(remoteDir); RefreshPaths(localDir, remoteDir); optFilter = new FileSyncScopeFilter(); optFilter.AttributeExcludeMask = excludeFileAttributes; foreach (string s in Settings.excludeFileExtensions) { optFilter.FileNameExcludes.Add(s); } }
public void DoBackup() { notifier.NotifyStart(); var readCredential = new NetworkCredential(command.SourceCredential.UserId, PasswordEncryption.Decrypt(command.SourceCredential.Password), command.SourceCredential.Domain); var writeCredential = new NetworkCredential(command.DestinationCredential.UserId, PasswordEncryption.Decrypt(command.DestinationCredential.Password), command.DestinationCredential.Domain); using (new NetworkPermission(command.SourcePath, readCredential)) using (new NetworkPermission(command.DestinationPath, writeCredential)) { var filter = new FileSyncScopeFilter(); SyncFileSystemReplicasOneWay(command.SourcePath, command.DestinationPath, filter, FileSyncOptions.None); } notifier.NotifyEnd(); }
/// <summary> /// Replicates the <see cref="TVA.Historian.IArchive"/>. /// </summary> protected override void ReplicateArchive() { // Connect to remote share if specified. string archiveLocation = ArchiveLocation; string replicaLocation = ReplicaLocation; if (replicaLocation.StartsWith(@"\\") && replicaLocation.Contains(':') && replicaLocation.Contains('@')) { // Format: \\[<domain>\]<username>:<password>@<network share> string share = @"\\" + replicaLocation.Substring(replicaLocation.IndexOf('@') + 1); string login = replicaLocation.Substring(2, replicaLocation.IndexOf(':') - 2); string password = replicaLocation.Substring(replicaLocation.IndexOf(':') + 1, replicaLocation.IndexOf('@') - replicaLocation.IndexOf(':') - 1); replicaLocation = share; string[] loginParts = login.Split('\\'); if (loginParts.Length == 2) FilePath.ConnectToNetworkShare(replicaLocation, loginParts[0], password, loginParts[1]); else FilePath.ConnectToNetworkShare(replicaLocation, login, password, Environment.UserDomainName); } FileSyncProvider syncSource = null; FileSyncProvider syncDestination = null; try { // Setup file synchronization filter. FileSyncScopeFilter synchFilter = new FileSyncScopeFilter(); synchFilter.FileNameIncludes.Add("*_to_*.d"); // Setup file synchronization providers. syncSource = new FileSyncProvider(archiveLocation, synchFilter, FileSyncOptions.CompareFileStreams); syncDestination = new FileSyncProvider(replicaLocation, synchFilter, FileSyncOptions.CompareFileStreams); syncDestination.ApplyingChange += SyncDestination_ApplyingChange; syncDestination.AppliedChange += SyncDestination_AppliedChange; // Setup and start file synchronization agent. SyncOrchestrator syncAgent = new SyncOrchestrator(); syncAgent.LocalProvider = syncSource; syncAgent.RemoteProvider = syncDestination; syncAgent.Direction = SyncDirectionOrder.Upload; syncAgent.Synchronize(); } finally { // Release resource used by synchronization providers. if (syncSource != null) syncSource.Dispose(); if (syncDestination != null) syncDestination.Dispose(); } }
public static void SyncFileSystemReplicasOneWay( string sourceReplicaRootPath, string destinationReplicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { FileSyncProvider sourceProvider = null; FileSyncProvider destinationProvider = null; try { // Instantiate source and destination providers, with a null filter (the filter // was specified in DetectChangesOnFileSystemReplica()), and options for both. sourceProvider = new FileSyncProvider( sourceReplicaRootPath, filter, options); destinationProvider = new FileSyncProvider( destinationReplicaRootPath, filter, options); // Register event handlers so that we can write information // to the console. //destinationProvider.AppliedChange += // new EventHandler<AppliedChangeEventArgs>(OnAppliedChange); //destinationProvider.SkippedChange += // new EventHandler<SkippedChangeEventArgs>(OnSkippedChange); // Use SyncCallbacks for conflicting items. //SyncCallbacks destinationCallbacks = destinationProvider.DestinationCallbacks; //destinationCallbacks.ItemConflicting += new EventHandler<ItemConflictingEventArgs>(OnItemConflicting); //destinationCallbacks.ItemConstraint += new EventHandler<ItemConstraintEventArgs>(OnItemConstraint); SyncOrchestrator agent = new SyncOrchestrator(); agent.LocalProvider = sourceProvider; agent.RemoteProvider = destinationProvider; agent.Direction = SyncDirectionOrder.Upload; // Upload changes from the source to the destination. Console.WriteLine("Synchronizing changes to replica: " + destinationProvider.RootDirectoryPath); agent.Synchronize(); } finally { // Release resources. if (sourceProvider != null) { sourceProvider.Dispose(); } if (destinationProvider != null) { destinationProvider.Dispose(); } } }
public override TaskStatus Run() { this.Info("Synchronising folders..."); bool success = true; try { string idFileName = "filesync.id"; Guid replica1Id = GetReplicaId(Path.Combine(this.SrcFolder, idFileName)); Guid replica2Id = GetReplicaId(Path.Combine(this.destFolder, idFileName)); // Set options for the sync operation FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates | FileSyncOptions.RecycleConflictLoserFiles; FileSyncScopeFilter filter = new FileSyncScopeFilter(); filter.FileNameExcludes.Add(idFileName); // Exclude the id file // Explicitly detect changes on both replicas upfront, to avoid two change // detection passes for the two-way sync DetectChangesOnFileSystemReplica(replica1Id, this.SrcFolder, filter, options); DetectChangesOnFileSystemReplica(replica2Id, this.destFolder, filter, options); // Sync source to destination SyncFileSystemReplicasOneWay(replica1Id, replica2Id, this.SrcFolder, this.destFolder, filter, options); success &= true; } catch (ThreadAbortException) { throw; } catch (Exception e) { Logger.ErrorFormat("Error from File Sync Provider: {0}\n", e.Message); success &= false; } Status status = Status.Success; if (!success) { status = Status.Error; } this.Info("Task finished."); return(new TaskStatus(status, false)); }
private void button2_Click_1(object sender, EventArgs e) { FileSyncProvider sourceProvider = null; FileSyncProvider destinationProvider = null; try { FileSyncScopeFilter filter = new FileSyncScopeFilter(); filter.FileNameExcludes.Add("*.metadata"); FileSyncOptions options = FileSyncOptions.None; sourceProvider = new FileSyncProvider(sourceRootPath, filter, options); destinationProvider = new FileSyncProvider(destRootPath, filter, options); //destinationProvider.AppliedChange += new EventHandler<AppliedChangeEventArgs>(OnAppliedChange); //destinationProvider.SkippedChange += new EventHandler<SkippedChangeEventArgs>(OnSkippedChange); SyncOrchestrator agent = new SyncOrchestrator(); agent.LocalProvider = sourceProvider; agent.RemoteProvider = destinationProvider; //agent.Direction = SyncDirectionOrder.Upload; // Sync source to destination agent.Direction = SyncDirectionOrder.DownloadAndUpload; // Sync source to destination Console.WriteLine("Synchronizing changes to replica: " + destinationProvider.RootDirectoryPath); System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); agent.Synchronize(); stopwatch.Stop(); this.richTextBox1.Text = "Sync framework test total time:" + stopwatch.ElapsedMilliseconds + "ms"; } catch (Exception exc) { throw exc; } finally { // Release resources if (sourceProvider != null) { sourceProvider.Dispose(); } if (destinationProvider != null) { destinationProvider.Dispose(); } } }
public void Start() { try { // Set options for the synchronization session. In this case, options specify // that the application will explicitly call FileSyncProvider.DetectChanges, and // that items should be moved to the Recycle Bin instead of being permanently deleted. FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates | FileSyncOptions.RecycleConflictLoserFiles; // Create a filter that excludes all *.lnk files. The same filter should be used // by both providers. FileSyncScopeFilter filter = new FileSyncScopeFilter(); if (fileNameExcludes != null && fileNameExcludes.Count > 0) { foreach (var item in fileNameExcludes) { filter.FileNameExcludes.Add(item); } } // Explicitly detect changes on both replicas before syncyhronization occurs. // This avoids two change detection passes for the bidirectional synchronization // that we will perform. DetectChangesOnFileSystemReplica(replica1RootPath, filter, options); DetectChangesOnFileSystemReplica(replica2RootPath, filter, options); // Synchronize the replicas in both directions. In the first session replica 1 is // the source, and in the second session replica 2 is the source. The third parameter // (the filter value) is null because the filter is specified in DetectChangesOnFileSystemReplica(). SyncFileSystemReplicasOneWay(replica1RootPath, replica2RootPath, null, options); if (syncOption == SyncOptions.Both) { SyncFileSystemReplicasOneWay(replica2RootPath, replica1RootPath, null, options); } } catch (Exception e) { //Console.WriteLine("\nException from File Sync Provider:\n" + e.ToString()); if (logger != null) { logger.Error(e); } } }
public static void WatchFileSystem(string sourceDirectory, string targetDirectory) { if (string.IsNullOrEmpty(sourceDirectory) || string.IsNullOrEmpty(targetDirectory) || !Directory.Exists(sourceDirectory) || !Directory.Exists(targetDirectory)) { return; } try { // Set options for the synchronization operation FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates | FileSyncOptions.RecycleConflictLoserFiles; FileSyncScopeFilter filter = new FileSyncScopeFilter(); //filter.FileNameExcludes.Add("*.lnk"); // Exclude all *.lnk files filter.FileNameIncludes.Add("*.mp4"); filter.FileNameIncludes.Add("*.mkv"); filter.FileNameIncludes.Add("*.avi"); filter.FileNameIncludes.Add("*.divx"); filter.FileNameIncludes.Add("*.png"); filter.FileNameIncludes.Add("*.jpg"); filter.FileNameIncludes.Add("*.gif"); filter.FileNameIncludes.Add("*.zip"); filter.FileNameIncludes.Add("*.rar"); filter.FileNameIncludes.Add("*.mp3"); // Explicitly detect changes on both replicas upfront, to avoid two change // detection passes for the two-way synchronization //DetectChangesOnFileSystemReplica( // sourceDirectory, filter, null); //DetectChangesOnFileSystemReplica( // targetDirectory, filter, options); // Synchronization in both directions SyncFileSystemReplicasOneWay(sourceDirectory, targetDirectory, filter, options); //SyncFileSystemReplicasOneWay(sourceDirectory, targetDirectory, null, options); } catch (Exception e) { Logging.Log("\nException from File Synchronization Provider:\n" + e.ToString()); } }
private static void DetectChangesOnFileSystemReplica(string replicaPath, FileSyncScopeFilter filter, FileSyncOptions options) { FileSyncProvider provider = null; try { provider = new FileSyncProvider(replicaPath, filter, options); provider.DetectChanges(); } finally { if (provider != null) { provider.Dispose(); } } }
/// <summary> /// Does actual presync preparations /// </summary> /// <returns></returns> private bool InternalPreSync() { try { // Reset all counters before every synchronization countChanges = 0; countDoneChanges = 0; freeDiskSpaceForLeft = 0; freeDiskSpaceForRight = 0; diskSpaceNeededForLeft = 0; diskSpaceNeededForRight = 0; isCheckForLeftDone = false; // Configure sync options FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleConflictLoserFiles | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates; // Configure sync filters FileSyncScopeFilter filter = new FileSyncScopeFilter(); // Update metadata of the folders before sync to // check for any changes or modifications DetectChangesonFileSystemReplica(leftPath, filter, options); DetectChangesonFileSystemReplica(rightPath, filter, options); // Start the 2-way sync if (!SyncFileSystemReplicasOneWay(leftPath, rightPath, null, options, true)) { return(false); } if (!SyncFileSystemReplicasOneWay(rightPath, leftPath, null, options, true)) { return(false); } return(true); } catch (Exception e) { return(false); } }
public static void DetectChangesOnFileSystemReplica(string replicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options, string metadataPath, string metadataFile, string tempDir, string trashDir) { FileSyncProvider provider = null; try { provider = new FileSyncProvider(replicaRootPath, filter, options, metadataPath, metadataFile, tempDir, trashDir); provider.DetectChanges(); } finally { // Release resources if (provider != null) { provider.Dispose(); } } }
public static void DetectChangesOnFileSystemReplica( string replicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { FileSyncProvider provider = null; try { provider = new FileSyncProvider(replicaRootPath, filter, options); provider.DetectChanges(); } finally { // Release resources if (provider != null) provider.Dispose(); } }
/// <summary> /// Detect the changes done to the folder /// <para>Updates the metadata</para> /// </summary> /// <param name="replicaRootPath">This parameter is the folder path to be checked</param> /// <param name="filter">This parameter is the filter which will be used during synchronization</param> /// <param name="options">This parameter holds the synchronization options</param> private static void DetectChangesonFileSystemReplica(string replicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { FileSyncProvider provider = null; try { provider = new FileSyncProvider(replicaRootPath, filter, options); provider.DetectChanges(); } finally { // Release resources or memory if (provider != null) { provider.Dispose(); } } }
/// <summary> /// Synchronize two folders (source and destination). The sync operation is ONE WAY only (From source to destination) /// </summary> /// <param name="sourceReplicaRootPath">source syncing path</param> /// <param name="destinationReplicaRootPath">destination syncing path</param> /// <param name="filter">syncing filter</param> /// <param name="options">syncing options</param> private void SyncFileSystemReplicasOneWay( string sourceReplicaRootPath, string destinationReplicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { FileSyncProvider sourceProvider = null; FileSyncProvider destinationProvider = null; try { sourceProvider = new FileSyncProvider( sourceReplicaRootPath, filter, options); destinationProvider = new FileSyncProvider( destinationReplicaRootPath, filter, options); destinationProvider.AppliedChange += new EventHandler <AppliedChangeEventArgs>(OnAppliedChange); destinationProvider.SkippedChange += new EventHandler <SkippedChangeEventArgs>(OnSkippedChange); destinationProvider.ApplyingChange += DestinationProvider_ApplyingChange; SyncOrchestrator agent = new SyncOrchestrator(); agent.LocalProvider = sourceProvider; agent.RemoteProvider = destinationProvider; agent.Direction = SyncDirectionOrder.Upload; // Sync source to destination var ret = agent.Synchronize(); if (ret.UploadChangesTotal != 0) { Logger.Log("Synchronizing '{0}', tot changes={1} ({2} done/{3} errors)", LogInfo.Info, VerbosityInfoLevel.V3, destinationProvider.RootDirectoryPath, ret.UploadChangesTotal, ret.UploadChangesApplied, ret.UploadChangesFailed); } } finally { // Release resources if (sourceProvider != null) { sourceProvider.Dispose(); } if (destinationProvider != null) { destinationProvider.Dispose(); } } }
public static void GravarMetadados(string pasta, FileSyncScopeFilter filtro) { FileSyncProvider provider = null; const FileSyncOptions opcoes = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates | FileSyncOptions.RecycleConflictLoserFiles; try { provider = new FileSyncProvider(pasta, filtro, opcoes); provider.DetectChanges(); } finally { if (provider != null) provider.Dispose(); } }
/// <summary> /// Start the synchronization in one direction /// </summary> /// <param name="sourcePath">This parameter holds the source folder path</param> /// <param name="destPath">This parameter holds the destination folder path</param> /// <param name="filter">This parameter is the filter which will be used during synchronization</param> /// <param name="options">This parameter holds the synchronization options</param> private void SyncFileSystemReplicasOneWay(string sourcePath, string destPath, FileSyncScopeFilter filter, FileSyncOptions options) { FileSyncProvider sourceProvider = null; FileSyncProvider destProvider = null; try { sourceProvider = new FileSyncProvider(sourcePath, filter, options); destProvider = new FileSyncProvider(destPath, filter, options); sourceProvider.PreviewMode = true; destProvider.PreviewMode = true; destProvider.Configuration.ConflictResolutionPolicy = ConflictResolutionPolicy.ApplicationDefined; SyncCallbacks destinationCallBacks = destProvider.DestinationCallbacks; destinationCallBacks.ItemConflicting += new EventHandler <ItemConflictingEventArgs>(OnItemConflicting); destinationCallBacks.ItemConstraint += new EventHandler <ItemConstraintEventArgs>(OnItemConstraint); destProvider.ApplyingChange += new EventHandler <ApplyingChangeEventArgs>(OnApplyingChange); SyncOrchestrator agent = new SyncOrchestrator(); agent.LocalProvider = sourceProvider; agent.RemoteProvider = destProvider; agent.Direction = SyncDirectionOrder.Upload; agent.Synchronize(); } finally { if (sourceProvider != null) { sourceProvider.Dispose(); } if (destProvider != null) { destProvider.Dispose(); } } }
public void SyncFileSystemReplicasOneWay( string sourceReplicaRootPath, string destinationReplicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { FileSyncProvider sourceProvider = null; FileSyncProvider destinationProvider = null; try { sourceProvider = new FileSyncProvider( sourceReplicaRootPath, filter, options); destinationProvider = new FileSyncProvider( destinationReplicaRootPath, filter, options); destinationProvider.AppliedChange += new EventHandler <AppliedChangeEventArgs>(OnAppliedChange); destinationProvider.SkippedChange += new EventHandler <SkippedChangeEventArgs>(OnSkippedChange); SyncOrchestrator agent = new SyncOrchestrator(); agent.LocalProvider = sourceProvider; agent.RemoteProvider = destinationProvider; agent.Direction = SyncDirectionOrder.DownloadAndUpload; // Sync source to destination //Console.WriteLine("Synchronizing changes to replica: " + // destinationProvider.RootDirectoryPath); logException.LogExceptionToDisk("Synchronizing changes to replica: " + destinationProvider.RootDirectoryPath); agent.Synchronize(); } finally { // Release resources if (sourceProvider != null) { sourceProvider.Dispose(); } if (destinationProvider != null) { destinationProvider.Dispose(); } } }
public void DetectChangesOnFileSystemReplica( Guid replicaId, string replicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { FileSyncProvider provider = null; try { provider = new FileSyncProvider(replicaId, replicaRootPath, filter, options); provider.DetectChanges(); } finally { // Release resources if (provider != null) { provider.Dispose(); } } }
public void SyncFileSystemReplicasOneWay( Guid sourceReplicaId, Guid destinationReplicaId, string sourceReplicaRootPath, string destinationReplicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { FileSyncProvider sourceProvider = null; FileSyncProvider destinationProvider = null; try { sourceProvider = new FileSyncProvider( sourceReplicaId, sourceReplicaRootPath, filter, options); destinationProvider = new FileSyncProvider( destinationReplicaId, destinationReplicaRootPath, filter, options); destinationProvider.AppliedChange += OnAppliedChange; destinationProvider.SkippedChange += OnSkippedChange; var agent = new SyncOrchestrator { LocalProvider = sourceProvider, RemoteProvider = destinationProvider, Direction = SyncDirectionOrder.Upload }; InfoFormat("Synchronizing changes to replica: {0}", destinationProvider.RootDirectoryPath); agent.Synchronize(); } finally { // Release resources if (sourceProvider != null) { sourceProvider.Dispose(); } if (destinationProvider != null) { destinationProvider.Dispose(); } } }
public static void DetectChangesOnFileSystemReplica( SyncId syncid, string replicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options, DirectoryInfo syncDir, string metadataName) { FileSyncProvider provider = null; try { provider = new FileSyncProvider( syncid.GetGuidId(), replicaRootPath, filter, options, syncDir.FullName, metadataName, syncDir.FullName, null); provider.DetectChanges(); } finally { // Release resources if (provider != null) provider.Dispose(); } }
public static void Main(string[] args) { if (args.Length < 2 || string.IsNullOrEmpty(args[0]) || string.IsNullOrEmpty(args[1]) || !Directory.Exists(args[0]) || !Directory.Exists(args[1])) { Console.WriteLine( "Usage: FileSyncSample [valid directory path 1] [valid directory path 2]"); return; } string replica1RootPath = args[0]; string replica2RootPath = args[1]; try { // Set options for the sync operation FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates | FileSyncOptions.RecycleConflictLoserFiles; FileSyncScopeFilter filter = new FileSyncScopeFilter(); filter.FileNameExcludes.Add("*.lnk"); // Exclude all *.lnk files // Explicitly detect changes on both replicas upfront, to avoid two change // detection passes for the two-way sync DetectChangesOnFileSystemReplica( replica1RootPath, filter, options); DetectChangesOnFileSystemReplica( replica2RootPath, filter, options); // Sync in both directions SyncFileSystemReplicasOneWay(replica1RootPath, replica2RootPath, null, options); SyncFileSystemReplicasOneWay(replica2RootPath, replica1RootPath, null, options); } catch (Exception e) { Console.WriteLine("\nException from File Sync Provider:\n" + e.ToString()); } }
private void SyncFiles(Guid sourceSyncId, Guid destinationSyncId, FileSyncScopeFilter filter) { using (FileSyncProvider sourceProvider = new FileSyncProvider(sourceSyncId, this.Source.GetMetadata("FullPath"), filter, this.syncOptions)) { using (FileSyncProvider destinationProvider = new FileSyncProvider(destinationSyncId, this.Destination.GetMetadata("FullPath"), filter, this.syncOptions)) { if (this.ShowOutput) { // Hook up some events so the user can see what is happening destinationProvider.AppliedChange += this.OnAppliedChange; destinationProvider.SkippedChange += this.OnSkippedChange; sourceProvider.AppliedChange += this.OnAppliedChange; sourceProvider.SkippedChange += this.OnSkippedChange; } SyncOrchestrator agent = new SyncOrchestrator { LocalProvider = sourceProvider, RemoteProvider = destinationProvider, Direction = this.direction }; agent.Synchronize(); } } }
public static void Sincronizar(string pastaOrigem, string pastaDestino, FileSyncScopeFilter filtro) { FileSyncProvider providerOrigem = null; FileSyncProvider providerDestino = null; try { const FileSyncOptions opcoes = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates | FileSyncOptions.RecycleConflictLoserFiles; providerOrigem = new FileSyncProvider(pastaOrigem, filtro, opcoes); providerDestino = new FileSyncProvider(pastaDestino, filtro, opcoes); providerDestino.AppliedChange += OnAppliedChange; providerDestino.SkippedChange += OnSkippedChange; var agent = new SyncOrchestrator { LocalProvider = providerOrigem, RemoteProvider = providerDestino, Direction = SyncDirectionOrder.Upload }; Console.WriteLine("Sincronizando: {0}", providerDestino.RootDirectoryPath); agent.Synchronize(); } finally { if (providerOrigem != null) providerOrigem.Dispose(); if (providerDestino != null) providerDestino.Dispose(); } }
public bool SyncNotes() { logException.LogExceptionToDisk("Inside Sync Notes..."); string localPath = ConfigurationManager.AppSettings["localPath"]; //@"C:\Users\rpoondla\Documents\Visual Studio 2015\Projects\OneNoteApplication\OneNoteApplication\bin\Debug"; //args[0]; string remotePath = ConfigurationManager.AppSettings["RemotePath"]; //@"\\10.213.154.154\f\Ravali"; //args[1]; logException.LogExceptionToDisk("localPath: " + localPath + "RemotePath: " + remotePath); try { // Set options for the synchronization operation FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates | FileSyncOptions.RecycleConflictLoserFiles; FileSyncScopeFilter filter = new FileSyncScopeFilter(); filter.FileNameIncludes.Add("*.txt"); //FileNameExcludes.Add("*.lnk"); // Exclude all *.lnk files // Explicitly detect changes on both replicas upfront, to avoid two change // detection passes for the two-way synchronization DetectChangesOnFileSystemReplica( localPath, filter, options); DetectChangesOnFileSystemReplica( remotePath, filter, options); // Synchronization in both directions SyncFileSystemReplicasOneWay(localPath, remotePath, null, options); SyncFileSystemReplicasOneWay(remotePath, localPath, null, options); } catch (Exception e) { logException.LogExceptionToDisk("\nException from File Synchronization Provider:\n" + e.ToString()); return(false); } return(true); }
private bool SyncFolders() { var success = true; try { const string idFileName = "filesync.id"; var replica1Id = GetReplicaId(Path.Combine(SrcFolder, idFileName)); var replica2Id = GetReplicaId(Path.Combine(DestFolder, idFileName)); // Set options for the sync operation const FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates | FileSyncOptions.RecycleConflictLoserFiles; var filter = new FileSyncScopeFilter(); filter.FileNameExcludes.Add(idFileName); // Exclude the id file // Explicitly detect changes on both replicas upfront, to avoid two change // detection passes for the two-way sync DetectChangesOnFileSystemReplica(replica1Id, SrcFolder, filter, options); DetectChangesOnFileSystemReplica(replica2Id, DestFolder, filter, options); // Sync source to destination SyncFileSystemReplicasOneWay(replica1Id, replica2Id, SrcFolder, DestFolder, filter, options); } catch (ThreadAbortException) { throw; } catch (Exception e) { Logger.ErrorFormat("Error from File Sync Provider: {0}\n", e.Message); success = false; } return(success); }
private void SyncFileSystemReplicasOneWay( string sourceReplicaRootPath, string destinationReplicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { FileSyncProvider sourceProvider = null; FileSyncProvider destinationProvider = null; try { sourceProvider = new FileSyncProvider( sourceReplicaRootPath, filter, options); destinationProvider = new FileSyncProvider( destinationReplicaRootPath, filter, options); destinationProvider.AppliedChange += new EventHandler<AppliedChangeEventArgs>(OnAppliedChange); destinationProvider.SkippedChange += new EventHandler<SkippedChangeEventArgs>(OnSkippedChange); destinationProvider.CopyingFile += new EventHandler<CopyingFileEventArgs>(OnCopyingFile); SyncOrchestrator agent = new SyncOrchestrator(); agent.LocalProvider = sourceProvider; agent.RemoteProvider = destinationProvider; agent.Direction = SyncDirectionOrder.Upload; agent.Synchronize(); notifier.NotifyInfo("Backup in progress.."); } catch(Exception ex) { notifier.NotifyError(ex.Message); } finally { if (sourceProvider != null) sourceProvider.Dispose(); if (destinationProvider != null) destinationProvider.Dispose(); } }
static void Main(string[] args) { string accountName = null; string accountKey = null; string containerName = null; string localPathName = null; string excludePaths = null; string fileNameIncludesToSync = null; string excludeSubDirectories = null; string syncFrequencyInSeconds = null; CloudStorageAccount storageAccount = null; FileSyncScopeFilter filter = null; try { try { if (RoleEnvironment.IsAvailable) { // Store main thread handle WindowsAzureBlob2FileSystemSync._mainThread = Thread.CurrentThread; // Read configuration settings accountName = RoleEnvironment.GetConfigurationSettingValue("FileSystemDurabilityPlugin.StorageAccountName"); accountKey = RoleEnvironment.GetConfigurationSettingValue("FileSystemDurabilityPlugin.StorageAccountPrimaryKey"); containerName = RoleEnvironment.GetConfigurationSettingValue("FileSystemDurabilityPlugin.SyncContainerName"); // FileSystemDurabilityPlugin.LocalFolderToSync must be relative to web site root or approot // Check if sitesroot\0 exists. We only synchronize the firt web site string appRootDir = Environment.GetEnvironmentVariable("RoleRoot") + @"\sitesroot\0"; if (!Directory.Exists(appRootDir)) { // May be WorkerRole appRootDir = Environment.GetEnvironmentVariable("RoleRoot") + @"\approot"; } try { // Make appRootDir writable. DirectorySecurity sec = Directory.GetAccessControl(appRootDir); SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null); sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); Directory.SetAccessControl(appRootDir, sec); } catch (Exception ex) { Trace.TraceError("Failed to make directory {0} writable. Error: {1}", appRootDir, ex.Message); Environment.Exit(-1); } // Set sync folder on local VM localPathName = Path.Combine(appRootDir, RoleEnvironment.GetConfigurationSettingValue("FileSystemDurabilityPlugin.LocalFolderToSync")); fileNameIncludesToSync = RoleEnvironment.GetConfigurationSettingValue("FileSystemDurabilityPlugin.FileNameIncludesToSync"); excludePaths = RoleEnvironment.GetConfigurationSettingValue("FileSystemDurabilityPlugin.ExcludePathsFromSync"); excludeSubDirectories = RoleEnvironment.GetConfigurationSettingValue("FileSystemDurabilityPlugin.ExcludeSubDirectories"); syncFrequencyInSeconds = RoleEnvironment.GetConfigurationSettingValue("FileSystemDurabilityPlugin.SyncFrequencyInSeconds"); } else { // Outside role envionment, read command line argument Trace.TraceError("Outside role envionment. Synchronization not possible."); Environment.Exit(-1); } } catch (Exception ex) { Trace.TraceError("Failed to read configuration settings. Error: {0}", ex.Message); Environment.Exit(-1); } if (!Directory.Exists(localPathName)) { Trace.TraceError("Please ensure that the local target directory exists."); Environment.Exit(-1); } // // Setup Store // if (accountName.Equals("devstoreaccount1")) { storageAccount = CloudStorageAccount.DevelopmentStorageAccount; } else { storageAccount = new CloudStorageAccount(new StorageCredentialsAccountAndKey(accountName, accountKey), true); } // // Create container if needed // CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); blobClient.GetContainerReference(containerName).CreateIfNotExist(); // Whether to include specific files only if (!string.IsNullOrEmpty(fileNameIncludesToSync)) { if (filter == null) { filter = new FileSyncScopeFilter(); } string[] fileNameIncludesToSyncInfo = fileNameIncludesToSync.Split(','); foreach (string fileIncludes in fileNameIncludesToSyncInfo) { filter.FileNameIncludes.Add(fileIncludes); } } // Set exclude path filter if (!string.IsNullOrEmpty(excludePaths)) { if (filter == null) { filter = new FileSyncScopeFilter(); } string[] excludePathInfo = excludePaths.Split(','); foreach (string excludePath in excludePathInfo) { filter.SubdirectoryExcludes.Add(excludePath); } } // Whether to exclude directoraries if (excludeSubDirectories.Equals("true")) { if (filter == null) { filter = new FileSyncScopeFilter(); } filter.AttributeExcludeMask = FileAttributes.Directory; } if (syncFrequencyInSeconds.Equals("-1")) { SynchronizeOnce(filter, localPathName, containerName, storageAccount); } else { // Need to synchronize periodically // Register event handler for roleinstance stopping and changed events RoleEnvironment.Stopping += WindowsAzureBlob2FileSystemSync.RoleEnvironmentStopping; RoleEnvironment.Changed += WindowsAzureBlob2FileSystemSync.RoleEnvironmentChanged; int frequencyInSecond = int.Parse(syncFrequencyInSeconds, System.Globalization.NumberStyles.Integer); if (frequencyInSecond > 0) { // Start Synchronization periodically while (true) { try { SynchronizeOnce(filter, localPathName, containerName, storageAccount); } catch (Exception ex) { Trace.TraceError("Failed to Synchronize. Error: {0}", ex.Message); } // Check new value for SyncFrequencyInSeconds, it can be modified syncFrequencyInSeconds = RoleEnvironment.GetConfigurationSettingValue("FileSystemDurabilityPlugin.SyncFrequencyInSeconds"); int currentFrequencyInSecond = int.Parse(syncFrequencyInSeconds, System.Globalization.NumberStyles.Integer); if (frequencyInSecond != currentFrequencyInSecond) { Trace.TraceInformation("Changing sync frequency to {0} seconds.", currentFrequencyInSecond); frequencyInSecond = currentFrequencyInSecond; } try { if (frequencyInSecond > 0) { Thread.Sleep(TimeSpan.FromSeconds(frequencyInSecond)); } else { // Pause the thread Thread.Sleep(Timeout.Infinite); } } catch (ThreadInterruptedException) { Trace.TraceInformation("File Synchronization thread interrupted. Configuration settings might have changed."); } } } } } catch (Exception ex) { Trace.TraceError(ex.Message); } }
public void DetectChangesOnFileSystemReplica(SyncId replicaId, string replicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { throw new System.NotImplementedException(); }
public void SyncFileSystemReplicasOneWay(SyncId sourceReplicaId, SyncId destinationReplicaId, string sourceReplicaRootPath, string destinationReplicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { throw new System.NotImplementedException(); }
public static void sync_Main(string[] args) { string replica1RootPath = args[0]; string replica2RootPath = args[1]; string s_metadata = "source_metadata"; string t_metadata = "target_metadata"; DirectoryInfo syncDir = new DirectoryInfo(RootDir); if (!syncDir.Exists) syncDir.Create(); SyncId sourceId = GetSyncId(syncDir.FullName + "\\Source_File.ID"); SyncId destId = GetSyncId(syncDir.FullName + "\\Target_File.ID"); // Set options for the synchronization operation FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates | FileSyncOptions.RecycleConflictLoserFiles; //exclude file name or extentions FileSyncScopeFilter filter = new FileSyncScopeFilter(); //filter.FileNameExcludes.Add("*.lnk"); // Exclude all *.lnk files if (FilterExtension != null) { foreach (string s in FilterExtension) { if (!String.IsNullOrEmpty(s)) { filter.FileNameExcludes.Add(s); } } } //keep me alive while (true) { // Explicitly detect changes on both replicas upfront, to avoid two change // detection passes for the two-way synchronization DetectChangesOnFileSystemReplica( sourceId, replica1RootPath, filter, options, syncDir, s_metadata); DetectChangesOnFileSystemReplica( destId, replica2RootPath, filter, options, syncDir, t_metadata); // Synchronization in both directions SyncFileSystemReplicasOneWay( sourceId, destId, replica1RootPath, replica2RootPath, syncDir, s_metadata, t_metadata, null, options); Thread.Sleep(1000); SyncFileSystemReplicasOneWay( destId, sourceId, replica2RootPath, replica1RootPath, syncDir, t_metadata, s_metadata, null, options); Thread.Sleep(5000); } }
/// <summary> /// Start the synchronization in one direction /// </summary> /// <param name="sourcePath">This parameter holds the source folder path</param> /// <param name="destPath">This parameter holds the destination folder path</param> /// <param name="filter">This parameter is the filter which will be used during synchronization</param> /// <param name="options">This parameter holds the synchronization options</param> private void SyncFileSystemReplicasOneWay(string sourcePath, string destPath, FileSyncScopeFilter filter, FileSyncOptions options) { FileSyncProvider sourceProvider = null; FileSyncProvider destProvider = null; try { sourceProvider = new FileSyncProvider(sourcePath, filter, options); destProvider = new FileSyncProvider(destPath, filter, options); sourceProvider.PreviewMode = true; destProvider.PreviewMode = true; destProvider.Configuration.ConflictResolutionPolicy = ConflictResolutionPolicy.ApplicationDefined; SyncCallbacks destinationCallBacks = destProvider.DestinationCallbacks; destinationCallBacks.ItemConflicting += new EventHandler<ItemConflictingEventArgs>(OnItemConflicting); destinationCallBacks.ItemConstraint += new EventHandler<ItemConstraintEventArgs>(OnItemConstraint); destProvider.ApplyingChange += new EventHandler<ApplyingChangeEventArgs>(OnApplyingChange); SyncOrchestrator agent = new SyncOrchestrator(); agent.LocalProvider = sourceProvider; agent.RemoteProvider = destProvider; agent.Direction = SyncDirectionOrder.Upload; agent.Synchronize(); } finally { if (sourceProvider != null) sourceProvider.Dispose(); if (destProvider != null) destProvider.Dispose(); } }
//This is our way of abstracting out this dependency since I'm sure it probably ties // us to the filesystem. private FileSyncProvider createFileSyncProvider(Guid replicaId, string replicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { var provider = new FileSyncProvider(replicaId, replicaRootPath, filter, options); return provider; }
public static void SyncFileSystemReplicasOneWay( SyncId sourceId, SyncId destId, string sourceReplicaRootPath, string destinationReplicaRootPath, DirectoryInfo syncDir, string sourceMetadata, string targetMetadata, FileSyncScopeFilter filter, FileSyncOptions options) { FileSyncProvider sourceProvider = null; FileSyncProvider destinationProvider = null; try { sourceProvider = new FileSyncProvider( sourceId.GetGuidId(), sourceReplicaRootPath, filter, options, syncDir.FullName, sourceMetadata, syncDir.FullName, null); destinationProvider = new FileSyncProvider( destId.GetGuidId(), destinationReplicaRootPath, filter, options, syncDir.FullName, targetMetadata, syncDir.FullName, null); destinationProvider.AppliedChange += OnAppliedChange; destinationProvider.SkippedChange += OnSkippedChange; SyncOrchestrator agent = new SyncOrchestrator { LocalProvider = sourceProvider, RemoteProvider = destinationProvider, Direction = SyncDirectionOrder.Upload }; // Sync source to destination //Console.WriteLine("Synchronizing changes to replica: " + // destinationProvider.RootDirectoryPath); agent.Synchronize(); } finally { // Release resources if (sourceProvider != null) sourceProvider.Dispose(); if (destinationProvider != null) destinationProvider.Dispose(); } }
public static void syncMain(string[] args) { if (args.Length < 2 || string.IsNullOrEmpty(args[0]) || string.IsNullOrEmpty(args[1]) || !Directory.Exists(args[0]) || !Directory.Exists(args[1])) { Console.WriteLine( "Usage: FileSyncSample [valid directory path 1] [valid directory path 2]"); return; } string replica1RootPath = args[0]; string replica2RootPath = args[1]; try { // Set options for the sync operation FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates | FileSyncOptions.RecycleConflictLoserFiles; FileSyncScopeFilter filter = new FileSyncScopeFilter(); filter.FileNameExcludes.Add("*.lnk"); // Exclude all *.lnk files // Explicitly detect changes on both replicas upfront, to avoid two change // detection passes for the two-way sync DetectChangesOnFileSystemReplica( replica1RootPath, filter, options); DetectChangesOnFileSystemReplica( replica2RootPath, filter, options); // Sync in both directions SyncFileSystemReplicasOneWay(replica1RootPath, replica2RootPath, null, options); SyncFileSystemReplicasOneWay(replica2RootPath, replica1RootPath, null, options); } catch (Exception e) { Console.WriteLine("\nException from File Sync Provider:\n" + e.ToString()); } }
private void DoSync() { try { Log = new ObservableCollection<string>(); //reset log FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleConflictLoserFiles | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates; FileSyncScopeFilter filter = new FileSyncScopeFilter(); filter.FileNameExcludes.Add("*.pete"); // Avoid two change detection passes for the two-way sync FindFileSystemReplicaChanges(Path1, filter, options); FindFileSystemReplicaChanges(Path2, filter, options); // Sync both ways OneWaySyncFileSystemReplicas(Path1, Path2, null, options); OneWaySyncFileSystemReplicas(path2, Path1, null, options); ReloadFileLists(); } catch (Exception exc) { Console.WriteLine(exc.Message); } }
public static void OneWaySyncFileSystemReplicas(string sourceReplicaRootPath, string destinationReplicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { FileSyncProvider path1Provider = null; FileSyncProvider path2Provider = null; try { path1Provider = new FileSyncProvider(sourceReplicaRootPath, filter, options); path2Provider = new FileSyncProvider(destinationReplicaRootPath, filter, options); path2Provider.SkippedChange += OnSkippedChange; path2Provider.AppliedChange += OnAppliedChange; SyncOrchestrator manager = new SyncOrchestrator(); manager.LocalProvider = path1Provider; manager.RemoteProvider = path2Provider; manager.Direction = SyncDirectionOrder.Upload; manager.Synchronize(); } finally { if (path1Provider != null) path1Provider.Dispose(); if (path2Provider != null) path2Provider.Dispose(); } }
public FileSyncScopeFilter CreateFilter() { //throw new NotImplementedException(); FileSyncScopeFilter filter = new FileSyncScopeFilter(); filter.FileNameExcludes.Add("*.metadata"); return filter; }
//public static void DetectChangesOnFileSystemReplica( //string replicaRootPath, //FileSyncScopeFilter filter, FileSyncOptions options) //{ // FileSyncProvider provider = null; // try // { // provider = new FileSyncProvider(replicaRootPath, filter, options); // provider.DetectChanges(); // } // finally // { // // Release resources // if (provider != null) // provider.Dispose(); // } //} public static void SyncFileSystemReplicasOneWay( string sourceReplicaRootPath, string destinationReplicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options) { FileSyncProvider sourceProvider = null; FileSyncProvider destinationProvider = null; try { sourceProvider = new FileSyncProvider( sourceReplicaRootPath, filter, options); //filter.SubdirectoryExcludes.Add(@".\"); destinationProvider = new FileSyncProvider( destinationReplicaRootPath, filter, options); sourceProvider.AppliedChange += new EventHandler<AppliedChangeEventArgs>(OnAppliedChange); sourceProvider.SkippedChange += new EventHandler<SkippedChangeEventArgs>(OnSkippedChange); sourceProvider.DetectChanges(); sourceProvider.CopyingFile += (s, a) => { string file = a.FilePath; int percent = a.PercentCopied; }; sourceProvider.AppliedChange += (s, a) => { string file = a.NewFilePath; var change = a.ChangeType; }; sourceProvider.DetectedChanges += (s, a) => { //a.TotalDirectoriesFound }; destinationProvider.CopyingFile += (s, a) => { string file = a.FilePath; int percent = a.PercentCopied; FileIO.FileTransferProgress(null, new FileTransferProgressEventArgs(percent, Path.GetFileName(file))); }; destinationProvider.AppliedChange += (s, a) => { string file = a.NewFilePath; }; destinationProvider.AppliedChange += (s, a) => { if (a.ChangeType == ChangeType.Delete) return; string file = a.NewFilePath; string serverPath = FileIO.GetPath(destinationReplicaRootPath) + file; if (FileIO.IsDirectory(serverPath)) return; FileIO.FinshedFileTransfer(null, new FinshedFileTransferEventArgs(Path.GetFileName(serverPath))); string localDirectory = FileIO.GetPath(FileIO.GetPath(sourceReplicaRootPath) + file); string localFile = localDirectory + Path.GetFileName(file); string topLevelDir = FileIO.GetPath(sourceReplicaRootPath) + FileIO.GetTopLevelFolder(file); try { File.Delete(localFile); } catch (Exception ex) { Logging.Log(ex.Message); } if (topLevelDir == FileIO.GetPath(sourceReplicaRootPath)) return; //bool isMoreTransfers = filter.FileNameIncludes.Any(x => Directory.GetFiles(localDirectory, x) != null); foreach(string filterVal in filter.FileNameIncludes) { if (Directory.EnumerateFiles(topLevelDir, filterVal, SearchOption.AllDirectories).Count() > 0) return; } //if (isMoreTransfers) return; try { FileIO.ForceDeleteDirectory(topLevelDir); } catch (Exception ex) { Logging.Log(ex.Message); } }; //SyncOrchestrator agent = new SyncOrchestrator(); _agent.LocalProvider = sourceProvider; _agent.RemoteProvider = destinationProvider; _agent.Direction = SyncDirectionOrder.Upload; Logging.Log("Synchronizing changes to replica: " + destinationProvider.RootDirectoryPath); _agent.StateChanged += (s, a) => { if (a.OldState == SyncOrchestratorState.Uploading && a.NewState == SyncOrchestratorState.Ready) { string path = FileIO.GetPath(destinationReplicaRootPath); string metadataFile = path + _metadataFile; try { Logging.Log("Syncronization complete, cleanning up..."); File.Delete(metadataFile); foreach (string tmpFile in Directory.EnumerateFiles(path, "*.tmp")) { File.Delete(tmpFile); } //FileSync.WatchFileSystem(sourceReplicaRootPath, destinationReplicaRootPath); } catch (Exception ex) { Logging.LogError(ex.Message); } } }; _agent.SessionProgress += (s, a) => { uint p = a.CompletedWork; }; _agent.Synchronize(); //keep background worker alive //while (true) { }; } catch (Exception ex) { Logging.Log(ex.Message); } finally { // Release resources if (sourceProvider != null) sourceProvider.Dispose(); if (destinationProvider != null) destinationProvider.Dispose(); //if (_agent != null) _agent.Cancel(); _agent = null; } }
//public static void Main(string[] args) //private string _destDir; //public string DestDir //{ // get { return _destDir; } // set { _destDir = value; } //} // create logger, filter public void Backup(string srcDir) { if (!Directory.Exists(srcDir )) { throw new InvalidDataException(); return; } string replica1RootPath = srcDir; string apath = srcDir.Replace(":", "XVN"); string replica2RootPath = Path.Combine(@"\\192.168.0.210\Dir1", apath); try { // If the dest dir does not exist, then create if (!Directory.Exists(replica2RootPath)) { Directory.CreateDirectory(replica2RootPath); } // Set options for the sync operation FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates | FileSyncOptions.RecycleConflictLoserFiles; FileSyncScopeFilter filter = new FileSyncScopeFilter(); filter.FileNameExcludes.Add("*.lnk"); // Exclude all *.lnk files // Explicitly detect changes on both replicas upfront, to avoid two change // detection passes for the two-way sync // Above not necessary, sync in one direction only DetectChangesOnFileSystemReplica(replica1RootPath, filter, options); DetectChangesOnFileSystemReplica(replica2RootPath, filter, options); // Sync in one direction only SyncFileSystemReplicasOneWay(replica1RootPath, replica2RootPath, null, options); //SyncFileSystemReplicasOneWay(replica2RootPath, replica1RootPath, null, options); } catch (Exception e) { Console.WriteLine("\nException from File Sync Provider:\n" + e.ToString()); } }
/// <summary> /// Does Sync operation to store change events into a list of FileData objects /// </summary> private int InternalPreviewSync() { try { fileData = new List<FileData>(); // Configure sync options FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleConflictLoserFiles | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates; // Configure sync filters FileSyncScopeFilter filter = new FileSyncScopeFilter(); filter.SubdirectoryExcludes.Add(TRACKBACK_FOLDER_NAME); filter.FileNameExcludes.Add(METADATA_FILE_NAME); // Add filters for file types for (int i = 0; i < excludeData.ExcludeFileTypeList.Count; i++) { filter.FileNameExcludes.Add("*" + excludeData.ExcludeFileTypeList[i]); } // Add filters for file names for (int i = 0; i < excludeData.ExcludeFileNameList.Count; i++) { filter.FileNameExcludes.Add(excludeData.ExcludeFileNameList[i]); } // Add filters for folders for (int i = 0; i < excludeData.ExcludeFolderList.Count; i++) { filter.SubdirectoryExcludes.Add(excludeData.ExcludeFolderList[i]); } // Update metadata of the folders before sync to // check for any changes or modifications DetectChangesonFileSystemReplica(leftPath, filter, options); DetectChangesonFileSystemReplica(rightPath, filter, options); // Start the 2-way sync SyncFileSystemReplicasOneWay(leftPath, rightPath, null, options); SyncFileSystemReplicasOneWay(rightPath, leftPath, null, options); return 0; } catch (SyncException) { return 1; } catch (DirectoryNotFoundException) { return 2; } catch (Exception) { return 3; } }
public FileSyncProvider CreateProvider(string RootPath, FileSyncScopeFilter filter, FileSyncOptions options) { //throw new NotImplementedException(); FileSyncProvider provider = null; try { provider = new FileSyncProvider(RootPath, filter, options); } catch (Exception e) { throw e; } return provider; }