Esempio n. 1
0
        /// <summary>Builds the command-line switches for Robocopy.</summary>
        private static string BuildSwitches(MirrorTask task, string sourceFolder)
        {
            string basicSwitches = string.IsNullOrEmpty(task.CustomRobocopySwitches)
                                ? Properties.Settings.Default.RobocopySwitches
                                : task.CustomRobocopySwitches;

            // if supported, use Robocopy's backup mode to avoid access denied errors
            if (UacHelper.IsRobocopyBackupModeSupported())
            {
                // do the basic switches include restartable mode (/z)?
                int zIndex = basicSwitches.IndexOf("/z ", StringComparison.OrdinalIgnoreCase);
                if (zIndex < 0 && basicSwitches.EndsWith("/z", StringComparison.OrdinalIgnoreCase))
                {
                    zIndex = basicSwitches.Length - 2;
                }

                // if so, change /z to /zb to enable restartable mode with backup mode fallback on access denied,
                // else add /b for normal backup mode
                if (zIndex >= 0)
                {
                    basicSwitches = basicSwitches.Substring(0, zIndex) + "/zb" + basicSwitches.Substring(zIndex + 2);
                }
                else
                {
                    basicSwitches += " /b";
                }
            }

            var switches = new StringBuilder();

            switches.Append(basicSwitches);

            if (!string.IsNullOrEmpty(task.ExtendedAttributes))
            {
                switches.Append(" /copy:dat");
                switches.Append(task.ExtendedAttributes);
            }

            if (task.DeleteExtraItems)
            {
                switches.Append(" /purge");
            }

            if (!task.OverwriteNewerFiles)
            {
                switches.Append(" /xo");                 // exclude older files in the source folder
            }
            if (!string.IsNullOrEmpty(task.ExcludedAttributes))
            {
                switches.Append(" /xa:");
                switches.Append(task.ExcludedAttributes);
            }

            if (task.ExcludedFiles.Count > 0)
            {
                switches.Append(" /xf");
                foreach (string file in task.ExcludedFiles)
                {
                    AppendPathOrWildcard(switches, sourceFolder, file);
                }
            }

            if (task.ExcludedFolders.Count > 0)
            {
                switches.Append(" /xd");
                foreach (string folder in task.ExcludedFolders)
                {
                    AppendPathOrWildcard(switches, sourceFolder, folder);
                }
            }

            return(switches.ToString());
        }
Esempio n. 2
0
 /// <summary>
 /// Gets the name for the scheduled task (to be) associated with the specified mirror task.
 /// </summary>
 private static string GetName(MirrorTask mirrorTask)
 {
     return(string.Format("DeeMirror ({0})", mirrorTask.Guid));
 }
Esempio n. 3
0
        /// <summary>
        /// Recreates a task from the specified XML node.
        /// </summary>
        public static MirrorTask Deserialize(XElement taskElement)
        {
            if (taskElement == null)
            {
                throw new ArgumentNullException("taskElement");
            }

            var task = new MirrorTask();

            task.Guid   = taskElement.Attribute("guid").Value;
            task.Source = taskElement.Element("source").Value;

            var element = taskElement.Element("useVolumeShadowCopy");

            if (element != null)
            {
                task.UseVolumeShadowCopy = bool.Parse(element.Value);
            }

            element = taskElement.Element("exclusions");
            if (element != null)
            {
                foreach (var file in element.Elements("file"))
                {
                    if (!string.IsNullOrEmpty(file.Value))
                    {
                        task.ExcludedFiles.Add(file.Value);
                    }
                }
                foreach (var folder in element.Elements("folder"))
                {
                    if (!string.IsNullOrEmpty(folder.Value))
                    {
                        task.ExcludedFolders.Add(folder.Value);
                    }
                }

                // migrate from DeeMirror format prior to v1.0
                foreach (var item in element.Elements("item"))
                {
                    if (string.IsNullOrEmpty(item.Value))
                    {
                        continue;
                    }

                    if (item.Value[0] == Path.DirectorySeparatorChar &&
                        Directory.Exists(task.Source + item.Value))
                    {
                        task.ExcludedFolders.Add(item.Value);
                    }
                    else
                    {
                        task.ExcludedFiles.Add(item.Value);
                    }
                }
            }

            element = taskElement.Element("excludedAttributes");
            if (element != null)
            {
                task.ExcludedAttributes = element.Value;
            }

            task.Target = taskElement.Element("target").Value;

            element = taskElement.Element("extendedAttributes");
            if (element != null)
            {
                task.ExtendedAttributes = element.Value;
            }

            element = taskElement.Element("overwriteNewerFiles");
            if (element != null)
            {
                task.OverwriteNewerFiles = bool.Parse(element.Value);
            }
            else             // for backwards compatibility:
            {
                task.OverwriteNewerFiles = true;
            }

            element = taskElement.Element("deleteExtraItems");
            if (element != null)
            {
                task.DeleteExtraItems = bool.Parse(element.Value);
            }

            element = taskElement.Element("customRobocopySwitches");
            if (element != null)
            {
                task.CustomRobocopySwitches = element.Value;
            }

            // for backwards compatibility:
            element = taskElement.Element("lastOperation");
            if (element == null)             // for further backwards compatibility
            {
                element = taskElement.Element("lastBackup");
            }
            if (element != null)
            {
                task.LastOperation = DateTime.ParseExact(element.Value, "u",
                                                         System.Globalization.CultureInfo.InvariantCulture).ToLocalTime();
            }

            return(task);
        }