示例#1
0
        /// <summary>
        /// Generates the backup completion info file and adds the backup to the backup index.
        /// </summary>
        /// <param name="sourcePath">The path of the backup source directory.</param>
        /// <param name="targetPath">The path of the backup target directory.</param>
        /// <param name="backupName">The name of the backup directory.</param>
        /// <param name="results">The results of the backup.</param>
        /// <returns><c>true</c> if all the operations completed successfully, otherwise <c>false</c>.</returns>
        public bool CompleteBackup(string sourcePath, string targetPath, string backupName, BackupResults results)
        {
            var backupPath = BackupMeta.BackupPath(targetPath, backupName);

            bool success = true;

            BackupCompleteInfo completionInfo = new(DateTime.UtcNow, results.PathsSkipped, results.ManifestComplete);
            var completionInfoFilePath        = BackupMeta.CompleteInfoFilePath(backupPath);

            try {
                BackupCompleteInfoWriter.Write(completionInfoFilePath, completionInfo);
                Logger.Info($"Created backup completion info file \"{completionInfoFilePath}\"");
            }
            catch (BackupCompleteInfoFileIOException e) {
                Logger.Warning(
                    $"Failed to write backup completion info \"{completionInfoFilePath}\": {e.InnerException.Reason}");
                success = false;
            }

            var indexFilePath = BackupMeta.IndexFilePath(targetPath);

            try {
                BackupIndexWriter.AddEntry(indexFilePath, backupName, sourcePath);
                Logger.Info($"Added this backup to backup index");
            }
            catch (BackupIndexFileIOException e) {
                Logger.Warning($"Failed to add backup to backup index \"{indexFilePath}\": {e.InnerException.Reason}");
                success = false;
            }

            return(success);
        }
示例#2
0
        /// <summary>
        /// Creates the backup directory, the log file, the start info file, and the manifest writer.
        /// </summary>
        /// <param name="sourcePath">The path of the backup source directory.</param>
        /// <param name="targetPath">The path of the backup target directory.</param>
        /// <returns>A tuple of the backup directory name and manifest writer.</returns>
        /// <exception cref="ApplicationRuntimeError">If the backup directory, start info file, or manifest writer
        /// can't be created.</exception>
        public (string, BackupManifestWriter) InitialiseBackup(string sourcePath, string targetPath)
        {
            string backupName;

            try {
                backupName = BackupMeta.CreateBackupDirectory(targetPath);
            }
            catch (BackupDirectoryCreateException) {
                throw new ApplicationRuntimeError("Failed to create new backup directory");
            }
            Logger.Info($"Backup name: {backupName}");
            var backupPath = BackupMeta.BackupPath(targetPath, backupName);

            Logger.Info($"Created backup directory \"{backupPath}\"");

            var logFilePath = BackupMeta.LogFilePath(backupPath);

            try {
                Logger.FileHandler = new(logFilePath);
                Logger.Info($"Created log file \"{logFilePath}\"");
            }
            catch (LoggingException e) {
                // Not much we can do if we can't create the log file, just ignore and continue.
                Logger.Warning(e.Message);
            }

            var manifestFilePath = BackupMeta.ManifestFilePath(backupPath);
            BackupManifestWriter manifestWriter;

            try {
                manifestWriter = new(manifestFilePath);
            }
            catch (BackupManifestFileIOException e) {
                throw new ApplicationRuntimeError(
                          $"Failed to create backup manifest file \"{manifestFilePath}\": {e.InnerException.Reason}");
            }
            Logger.Info($"Created backup manifest file \"{manifestFilePath}\"");

            BackupStartInfo startInfo         = new(sourcePath, DateTime.UtcNow);
            var             startInfoFilePath = BackupMeta.StartInfoFilePath(backupPath);

            try {
                BackupStartInfoWriter.Write(startInfoFilePath, startInfo);
            }
            catch (BackupStartInfoFileIOException e) {
                throw new ApplicationRuntimeError(
                          $"Failed to write backup start info file \"{startInfoFilePath}\": {e.InnerException.Reason}");
            }
            Logger.Info($"Created backup start info file \"{startInfoFilePath}\"");

            return(backupName, manifestWriter);
        }
示例#3
0
        /// <summary>
        /// Runs the backup.
        /// </summary>
        /// <param name="config">The configuration of this backup.</param>
        /// <param name="previousBackupSum">Sum of the existing backup data for this source directory.</param>
        /// <param name="backupName">The name of the new backup directory.</param>
        /// <param name="manifestWriter">Writes the backup manifest. Must be in a newly-constructed state.</param>
        /// <returns>Results of the backup.</returns>
        /// <exception cref="ApplicationRuntimeError">If the backup fails.</exception>
        private BackupResults DoBackup(AppConfig config, BackupSum previousBackupSum, string backupName,
                                       BackupManifestWriter manifestWriter)
        {
            var backupPath = BackupMeta.BackupPath(config.TargetPath, backupName);

            Logger.Info("Running backup operation");
            try {
                return(BackupService.Run(config.SourcePath, config.ExcludePaths, previousBackupSum, backupPath,
                                         manifestWriter, Logger));
            }
            catch (BackupServiceException e) {
                throw new ApplicationRuntimeError(e.Message);
            }
        }