/// <summary>
        /// Factory.
        /// </summary>
        /// <returns>Populated BackupConfig instance.</returns>
        public static BackupConfig Create()
        {
            var doc = new BackupConfig()
            {
                BackupTarget = new BackupTarget()
                {
                    Path = string.Empty
                },
                BackupSource = new BackupSource[]
                {
                    new ElephantBackup.BackupSource()
                    {
                        Path             = string.Empty,
                        ExcludeDirs      = string.Empty,
                        ExcludeFileTypes = string.Empty
                    }
                },
                Options = new Options()
                {
                    GlobalExcludeFileTypes = string.Empty,
                    GlobalExcludeDirs      = string.Empty,
                    Verify                 = true,
                    VerifySpecified        = true,
                    CreateLogFile          = true,
                    CreateLogFileSpecified = true
                }
            };

            return(doc);
        }
        public static BackupConfig CreateExample()
        {
            var doc = BackupConfig.Create();

            doc.BackupSource[0].Path           = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
            doc.Options.GlobalExcludeFileTypes = ".obj;";
            doc.Options.GlobalExcludeDirs      = "AppData;obj;bin";
            return(doc);
        }
示例#3
0
        /// <summary>
        /// Perform a backup.
        /// </summary>
        /// <returns>true if the backup succeeded</returns>
        private bool DoBackup()
        {
            var config = BackupConfig.Load();

            if (config == null)
            {
                Console.WriteLine("Error: Config file not found.");
                return(false);
            }
            if ((config?.BackupTarget == null) || (config.BackupTarget.Path == null))
            {
                Console.WriteLine("Error: No target specified");
                return(false);
            }
            if ((config?.BackupSource == null) || (config.BackupSource.Length == 0))
            {
                Console.WriteLine("Error: No sources specified");
                return(false);
            }

            var bm = new BackupManager(config);

            bm.OnError       += this.OnError;
            bm.OnInformation += this.OnInformation;

            var result = bm.DoBackup();  // actually do the backup.

            if (result.Success)
            {
                Console.WriteLine("Backup succeeded after {0}", result.Timetaken.ToString(@"hh\:mm\:ss"));
            }
            else
            {
                Console.WriteLine("Backup failed after {0} - {1}",
                                  result.Timetaken.ToString(@"hh\:mm\:ss"),
                                  (result.Exception != null) ? result.Exception.Message : "Unknown reason");
            }
            Console.WriteLine("Started at {0}", result.StartTime.ToString());
            Console.WriteLine("Finished at {0}", result.EndTime.ToString());
            Console.WriteLine("{0} bytes copied", result.BytesCopied);
            Console.WriteLine("{0} files copied", result.FilesCopied);
            Console.WriteLine("{0} directories copied", result.DirectoriesCopied);
            Console.WriteLine("{0} files skipped", result.FilesSkipped);
            Console.WriteLine("{0} directories skipped", result.DirectoriesSkipped);
            if (result.LogFilePath != null)
            {
                Console.WriteLine("Log file created at {0}", result.LogFilePath);
            }

            return(result.Success);
        }
示例#4
0
        /// <summary>
        /// Create a blank configuration file.
        /// </summary>
        private void DoCreateConfig()
        {
            string configPath = BackupConfig.GetConfigPath();

            if (File.Exists(configPath))
            {
                Console.WriteLine("{0} already exists. Overwrite (y/n)?", configPath);
                if (Console.ReadLine() != "y")
                {
                    return;
                }
            }

            using (var wtr = new StreamWriter(configPath, false, Encoding.UTF8))
            {
                var configStr = BackupConfig.CreateExample().ToString();
                wtr.Write(configStr);
            }
            Console.WriteLine("Config file written to {0}", configPath);
        }
 /// <summary>
 /// Constructor. Initialise a BackupManager object.
 /// </summary>
 /// <param name="config">Configuation.</param>
 public BackupManager(
     BackupConfig config)
 {
     this.config = config;
 }