예제 #1
0
        public static PerformConfig PrepaireConfig(string configfile, string logPath, string tmpPath)
        {
            if (string.IsNullOrEmpty(logPath))
            {
                logPath = "log";
            }
            if (string.IsNullOrEmpty(tmpPath))
            {
                tmpPath = "tmp";
            }

            var tmpWorkDirPath = Path.Combine(tmpPath, $"dackup-tmp-{DateTime.UtcNow:s}");

            DackupContext.Create(Path.Join(logPath, "dackup.log"), tmpWorkDirPath);

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Information()
                         .WriteTo.Console()
                         .WriteTo.File(DackupContext.Current.LogFile, rollingInterval: RollingInterval.Month, rollOnFileSizeLimit: true)
                         .CreateLogger();

            AppDomain.CurrentDomain.UnhandledException += (s, e) => Log.Error("*** Crash! ***", "UnhandledException");
            TaskScheduler.UnobservedTaskException      += (s, e) => Log.Error("*** Crash! ***", "UnobservedTaskException");

            return(PerformConfigHelper.LoadFrom(configfile));
        }
예제 #2
0
        public static Task <BackupTaskResult[]> RunBackup(PerformConfig cfg)
        {
            Log.Information("Dackup start backup task ");

            var backupTaskList   = PerformConfigHelper.ParseBackupTaskFromConfig(cfg);
            var backupTaskResult = new List <Task <BackupTaskResult> >();

            backupTaskList.ForEach(task =>
            {
                backupTaskResult.Add(task.BackupAsync());
            });
            return(Task.WhenAll(backupTaskResult.ToArray()));
        }
예제 #3
0
        public static Task <NotifyResult[]> RunNotify(PerformConfig cfg, Statistics statistics)
        {
            Log.Information("Dackup start notify task ");

            var notifyList = PerformConfigHelper.ParseNotifyFromConfig(cfg);

            var notifyResultList = new List <Task <NotifyResult> >();

            notifyList.ForEach(notify =>
            {
                notifyResultList.Add(notify.NotifyAsync(statistics));
            });

            return(Task.WhenAll(notifyResultList.ToArray()));
        }
예제 #4
0
        public static (Task <UploadResult[]>, Task <PurgeResult[]>) RunStorage(PerformConfig cfg)
        {
            Log.Information("Dackup start storage task ");

            var storageList             = PerformConfigHelper.ParseStorageFromConfig(cfg);
            var storageUploadResultList = new List <Task <UploadResult> >();
            var storagePurgeResultList  = new List <Task <PurgeResult> >();

            storageList.ForEach(storage =>
            {
                DackupContext.Current.GenerateFilesList.ForEach(file =>
                {
                    storageUploadResultList.Add(storage.UploadAsync(file));
                });
                storagePurgeResultList.Add(storage.PurgeAsync());
            });

            var storageUploadTasks = Task.WhenAll(storageUploadResultList.ToArray());
            var storagePurgeTasks  = Task.WhenAll(storagePurgeResultList.ToArray());

            return(storageUploadTasks, storagePurgeTasks);
        }
예제 #5
0
        public static int Main(string[] args)
        {
            var statistics = new Statistics();

            statistics.StartedAt = DateTime.Now;

            var app = new CommandLineApplication
            {
                Name        = "dackup",
                Description = "A backup app for your server or database or desktop",
            };

            app.HelpOption(inherited: true);

            app.Command("new", newCmd =>
            {
                newCmd.Description = "Generate a config file";

                var modelName = newCmd.Argument("model", "Name of the model").IsRequired();

                newCmd.OnExecute(() =>
                {
                    var fileName = Path.Combine(Environment.CurrentDirectory, modelName.Value + ".config");
                    PerformConfigHelper.GenerateMockupConfig(fileName);
                    return(1);
                });
            });

            app.Command("perform", performCmd =>
            {
                performCmd.Description = "Performing your backup by config";

                var configFile = performCmd.Option("--config-file  <FILE>", "Required. The File name of the config.", CommandOptionType.SingleValue)
                                 .IsRequired()
                                 .Accepts(v => v.ExistingFile());
                var logPathConfig = performCmd.Option("--log-path  <PATH>", "op. The File path of the log.", CommandOptionType.SingleValue);
                var tmpPathConfig = performCmd.Option("--tmp-path  <PATH>", "op. The tmp path.", CommandOptionType.SingleValue);

                performCmd.OnExecute(() =>
                {
                    try
                    {
                        var logPath = logPathConfig.Value();
                        var tmpPath = tmpPathConfig.Value();

                        var configFilePath = configFile.Value();

                        var performConfig = ApplicationHelper.PrepaireConfig(configFilePath, logPath, tmpPath);

                        statistics.ModelName = performConfig.Name;

                        Directory.CreateDirectory(DackupContext.Current.TmpPath);

                        // run backup
                        var backupTasks = ApplicationHelper.RunBackup(performConfig);
                        backupTasks.Wait();

                        statistics.FinishedAt = DateTime.Now;

                        // run store
                        var(storageUploadTasks, storagePurgeTasks) = ApplicationHelper.RunStorage(performConfig);
                        var storageTasks = Task.WhenAll(storageUploadTasks, storagePurgeTasks);

                        // run notify
                        var notifyTasks = ApplicationHelper.RunNotify(performConfig, statistics);

                        // wait
                        storageTasks.Wait();

                        ApplicationHelper.Clean();

                        notifyTasks.Wait();

                        Log.Information("Dackup done ");
                    }
                    catch (Exception exception)
                    {
                        Log.Error(Utils.FlattenException(exception));
                    }
                    finally
                    {
                        Log.CloseAndFlush();
                    }
                    return(1);
                });
            });

            app.OnExecute(() =>
            {
                app.ShowHelp();
                return(1);
            });

            return(app.Execute(args));
        }