示例#1
0
        public static void Create(string source, string destination, string issuer, string comment)
        {
            if (!Directory.Exists(source))
                throw new ArgumentException("Source directory not found.");
            if (!Directory.Exists(destination))
                throw new ArgumentException("Destination directory not found.");

            var workingDirectory = new WorkingDirectory(source);
            workingDirectory.ImportFiles();

            var backupStatus = new BackupStatus();

            string newId = new Random().Next().ToString();

            if (File.Exists(Path.Combine(destination, "1.zip")))
            {
                File.Move(Path.Combine(destination, "1.zip"), Path.Combine(destination, newId + ".zip"));
            }
            else
            {
                newId = null;
            }

            backupStatus.ReadFiles(Path.Combine(destination, newId + ".zip"), true);

            workingDirectory.CreateIncrementalBackup(Path.Combine(destination, "1.zip"), backupStatus, newId, comment, issuer);
        }
示例#2
0
        public static void Combine(string zipPath, string resultPath, int maxDepth, string issuer,
                                   string comment)
        {
            if (!File.Exists(zipPath))
                throw new ArgumentException("Source directory not found.");
            if (!Directory.Exists(Path.GetDirectoryName(Path.GetFullPath(resultPath))))
                throw new ArgumentException("Destination directory not found.");
            if (File.Exists(resultPath))
                throw new ArgumentException("Destination file already exists.");

            var backupStatus = new BackupStatus();

            backupStatus.ReadFiles(zipPath, true, maxDepth);

            var groups = from x in backupStatus.Root.Children
                         group x by x.ArchivePath
                         into archives
                         select archives;

            using (var file = ZipFile.Open(resultPath, ZipArchiveMode.Create))
            {
                var informationFile = file.CreateEntry("info.xml");
                using (var stream = informationFile.Open())
                {
                    var information = new BackupInformation
                                          {
                                              DeletedFiles = backupStatus.LastDeletedFiles,
                                              ParentName = backupStatus.Root.Information.ParentName,
                                              CreationDate = DateTime.Now,
                                              Issuer = issuer,
                                              Comment = comment
                                          };
                    information.Save(stream);
                }
                foreach (var group in groups)
                {
                    using (var zipFile = ZipFile.Open(group.Key, ZipArchiveMode.Read))
                    {
                        foreach (var backupFile in group)
                        {
                            var relativeName = "data" + backupFile.VirtualPath.Substring(1);
                            var entry = file.CreateEntry(relativeName + "." + backupFile.FileHash);

                            using (var stream = entry.Open())
                            {
                                using (var fileStream = zipFile.GetEntry(entry.FullName).Open())
                                {
                                    fileStream.CopyTo(stream);
                                }
                            }
                        }
                    }
                }
            }
        }
        public void CreateIncrementalBackup(string fileName,  BackupStatus backupStatus, string parentHash = null, string comment = null, string issuer = null)
        {
            var addedFiles = Root.Children.Where(
                    a =>
                    !backupStatus.Root.Children.Any(m => m.VirtualPath == a.VirtualPath && m.FileHash == a.FileHash));
            var removedFiles = backupStatus.Root.Children.Where(
                    a =>
                    Root.Children.All(m => m.VirtualPath != a.VirtualPath));

            SaveBackup (fileName, parentHash, removedFiles, addedFiles, comment, issuer);
        }
        public void BackupStatusReadSingleFileCorrectly()
        {
            var backupStatus = new BackupStatus();

            backupStatus.ReadFiles(Path.Combine(BackupDirectory, "test.zip"));

            CollectionAssert.AllItemsAreUnique(backupStatus.Root.Children);

            Assert.AreEqual(4, backupStatus.Root.Children.Count);

            Assert.IsNotNull(backupStatus.Root.Children.SingleOrDefault(a => a.VirtualPath == "./test.txt"));
            Assert.IsNotNull(backupStatus.Root.Children.SingleOrDefault(a => a.VirtualPath == "./test2.txt"));
            Assert.IsNotNull(backupStatus.Root.Children.SingleOrDefault(a => a.VirtualPath == "./bin/test3.txt"));
            Assert.IsNotNull(backupStatus.Root.Children.SingleOrDefault(a => a.VirtualPath == "./bin/a/b/c/test4.txt"));

            var info = backupStatus.Root.Information;

            Assert.IsNotNull(info);
            Assert.IsNotNull(info.DeletedFiles);

            Assert.IsNotNull(info.DeletedFiles.SingleOrDefault(a => a == "./test_del.txt"));
            Assert.AreEqual("248990293", info.ParentName);
        }
        public void BackupStatusHasRootNode()
        {
            var backupStatus = new BackupStatus();

            Assert.IsNotNull(backupStatus.Root);
        }
示例#6
0
        public static void Extract(string source, string destination, int maxDepth, bool force)
        {
            if (!File.Exists(source))
                throw new ArgumentException("Source directory not found.");
            if (!Directory.Exists(destination))
                throw new ArgumentException("Destination directory not found.");

            var backupStatus = new BackupStatus();

            backupStatus.ReadFiles(source, true, maxDepth);

            var groups = from x in backupStatus.Root.Children
                         group x by x.ArchivePath
                             into archives
                             select archives;

            Parallel.ForEach(groups, group =>
            {
                using (var archive = ZipFile.Open(@group.Key, ZipArchiveMode.Read))
                {
                    foreach (var backupFile in @group)
                    {
                        var file = "data" + backupFile.VirtualPath.Substring(1) + "." +
                                   backupFile.FileHash;

                        var compressedFile = archive.GetEntry(file);
                        using (var stream = compressedFile.Open())
                        {
                            var resultFileName = Path.Combine(destination,
                                                              backupFile.VirtualPath
                                                                        .Substring(2));

                            Directory.CreateDirectory(Path.GetDirectoryName(resultFileName));

                            if (File.Exists(resultFileName) && !force)
                                Console.WriteLine(
                                    "Overriding files is disabled. Use --force to enable it.");
                            else if (force)
                                File.Delete(resultFileName);

                            using (var fileStream = File.Create(resultFileName))
                            {
                                stream.CopyTo(fileStream);
                            }
                        }
                    }
                }
            });
        }
示例#7
0
 public static BackupStatus GetStatus(string path)
 {
     var backup = new BackupStatus();
     backup.ReadFiles(path);
     return backup;
 }
示例#8
0
 public static IEnumerable<BackupFile> GetFiles(string source)
 {
     var backup = new BackupStatus();
     backup.ReadFiles(source);
     return backup.Root.Children;
 }
示例#9
0
 public static IEnumerable<string> GetDeletedFiles(string source)
 {
     var backup = new BackupStatus();
     backup.ReadFiles(source);
     return backup.Root.Information.DeletedFiles;
 }