Esempio n. 1
0
        static void SyncBackups(string zipfolder, string targetServer, string targetAccount, string targetCertfile)
        {
            string rsyncbinary = Tools.RsyncBinary;

            string binfolder   = PathHelper.GetParentFolder(rsyncbinary);
            string rsyncfolder = PathHelper.GetParentFolder(binfolder);
            string appfolder   = PathHelper.GetParentFolder(rsyncfolder);

            string synccert = Path.Combine(rsyncfolder, "synccert", $"{targetCertfile}");
            string logfile  = GetLogFileName(appfolder, "SyncBackups");

            string source = "/cygdrive/" + char.ToLower(zipfolder[0]) + zipfolder.Substring(2).Replace("\\", "/");
            string target = $"{targetAccount}@{targetServer}:.";

            string?oldfolder = null;

            try
            {
                oldfolder = Directory.GetCurrentDirectory();
                Directory.SetCurrentDirectory(binfolder);

                for (int tries = 1; tries <= 5; tries++)
                {
                    using (new ContextLogger(new Dictionary <string, object>()
                    {
                        ["Tries"] = tries
                    }))
                    {
                        string[] files = Directory.GetFiles(zipfolder);

                        Log
                        .ForContext("FileCount", files.Length)
                        .Information("Syncing backup files: {Source} -> {Target}", source, LogHelper.Mask(target, new[] { targetServer, targetAccount }));

                        Stopwatch watch = Stopwatch.StartNew();

                        string args = $"--checksum --remove-source-files -a -l -e './ssh -o StrictHostKeyChecking=no -i {synccert}' {source} {target} --log-file {logfile}";

                        int result = RunCommand(Path.GetFileName(rsyncbinary), args);
                        watch.Stop();
                        Statistics.SyncTime += watch.Elapsed;
                        long elapsedms = (long)watch.Elapsed.TotalMilliseconds;

                        if (new FileInfo(logfile).Length > 0)
                        {
                            Log.Information("Reading logfile: {Logfile}", logfile);
                            string[] rows = File.ReadAllLines(logfile).Where(l => !l.Contains(".d..t...... ") && !l.Contains("<f..t...... ")).ToArray();
                            Log.ForContext("LogfileContent", LogHelper.TruncateLogFileContent(rows)).Information("rsync results");
                        }

                        Log.Information("Deleting logfile: {Logfile}", logfile);
                        File.Delete(logfile);

                        if (result == 0)
                        {
                            Log
                            .ForContext("ElapsedMS", elapsedms)
                            .ForContext("Zipfolder", zipfolder)
                            .Information("Sync success");
                            return;
                        }
                        else
                        {
                            Log
                            .ForContext("Binary", rsyncbinary)
                            .ForContext("Commandargs", LogHelper.Mask(args, new[] { targetServer, targetAccount }))
                            .ForContext("Result", result)
                            .ForContext("ElapsedMS", elapsedms)
                            .ForContext("Zipfolder", zipfolder)
                            .Warning("Sync fail");
                        }
                    }
                }
            }
            finally
            {
                if (oldfolder != null)
                {
                    Directory.SetCurrentDirectory(oldfolder);
                }
            }

            Statistics.SuccessCount = 0;
        }