Пример #1
0
        private static async Task BackupHardLinks(BackupParams backupParams, IHardLinkHelper helper, SshClient sshClient)
        {
            Console.CursorVisible = false;
            var sw = System.Diagnostics.Stopwatch.StartNew();

            try
            {
                using (var vssHelper = new VssHelper(new DirectoryInfo(backupParams.RootDit).Root.Name))
                {
                    Console.WriteLine("Creating VSS snapshot...");

                    var actualRoot = vssHelper.CreateSnapshot()
                        ? vssHelper.GetSnapshotFilePath(backupParams.RootDit)
                        : backupParams.RootDit;

                    string[] TargetFilesEnumerator(string backupPathWin)
                    {
                        var backupPathUnix = PathHelpers.NormalizePathUnix(backupPathWin.Replace(backupParams.RemoteRootWin, backupParams.RemoteRootUnix));
                        var cmd            = sshClient.RunCommand($"find \"{backupPathUnix}\" -type f");
                        var result         = cmd.Result;
                        var es             = cmd.ExitStatus;

                        if (es != 0 || string.IsNullOrEmpty(result))
                        {
                            return(new string[0]);
                        }

                        var files = result.Split(new[] { "\r", "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
                                    .Where(x => !x.EndsWith(".bkp/info.txt"))
                                    .Select(x => PathHelpers.NormalizePathWin(x.Replace(backupParams.RemoteRootUnix, backupParams.RemoteRootWin)))
                                    .ToArray();

                        return(files);
                    }

                    var engine = new HardLinkBackupEngine(actualRoot, backupParams.Sources, backupParams.BackupRoots, backupParams.RemoteRootWin, true, helper, TargetFilesEnumerator);
                    engine.Log    += WriteLog;
                    engine.LogExt += WriteLogExt;
                    await engine.DoBackup();
                }

                sw.Stop();

                Console.WriteLine($"Done in {sw.Elapsed:hh\\:mm\\:ss}");
            }
            catch (Exception e)
            {
                sw.Stop();

                Console.WriteLine($"Failed (operation lasted {sw.Elapsed:hh\\:mm\\:ss})");
                Console.WriteLine(e);
            }
            finally
            {
                sw.Stop();
                Console.CursorVisible = true;
            }
        }
 public HardLinkBackupEngine(string rootDir, string[] sources, string[] backupRoots, string destination, bool allowSimultaneousReadWrite, IHardLinkHelper hardLinkHelper, Func <string, string[]> remoteFilesEnumerator)
 {
     //_rootDir = rootDir.EndsWith(@":\\") ? rootDir : rootDir.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
     _destination = destination.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
     _sources     = GetSources(rootDir, sources);
     _backupRoots = backupRoots;
     _allowSimultaneousReadWrite = allowSimultaneousReadWrite;
     _hardLinkHelper             = hardLinkHelper;
     _remoteFilesEnumerator      = remoteFilesEnumerator;
 }