Exemple #1
0
        private async Task <string> _ValidateRunAsync(int i)
        {
            var file = this.Files.ElementAtOrDefault(i);

            // Write Log
            string t = FormatCli.H2($"Task: { (i + 1).ToString() }");

            t += FormatCli.KeyValue("File", file.File);
            t += FormatCli.KeyValue("Checksum", file.Checksum);

            //Checks
            var path = DirFile.Combine(this.AppSettings.Path.Export, file.File);

            if (!System.IO.File.Exists(path))
            {
                t += Environment.NewLine;
                t += $"Error: Could not find file {path}";
                return(t);
            }

            var checksum = await Task.Run(() => FilesModel.Hash(path));

            if (checksum != file.Checksum)
            {
                t += Environment.NewLine;
                t += $"Error: Checksum is not identical, backup maybe corrupted!";
                return(t);
            }

            t += FormatCli.KeyValue("Status", "Valid!");

            return(t);
        }
Exemple #2
0
        private string _ImportParams(int i)
        {
            var file = this.Files.ElementAtOrDefault(i);

            // Write Log
            string t = FormatCli.H2($"Task: { (i + 1).ToString() }");

            t += FormatCli.KeyValue("File", file.File);
            t += FormatCli.KeyValue("Checksum", file.Checksum);

            //Checks
            var path = DirFile.Combine(this.AppSettings.Path.Export, file.File);

            if (!System.IO.File.Exists(path))
            {
                t += Environment.NewLine;
                t += $"Error: Could not find file {path}";
                return(t);
            }
            this.Import.File = path;

            t += FormatCli.H3("Parameter");
            var connector = this.Import.Config.Format == "p" ? new Connectors.Psql(AppSettings).BinName : this.Import.BinName;

            t += $"{ connector } { this.Import.GetPatameters(this.Connect) }" + Environment.NewLine;

            return(t);
        }
Exemple #3
0
        /// <summary>
        /// Create a overview
        /// </summary>
        /// <returns></returns>
        public string OverviewExport()
        {
            var log = FormatCli.KeyValue("Name", this.Name);

            log += FormatCli.KeyValue("Tasks", this.Tasks.ToString());
            log += FormatCli.KeyValue("File", this.Export.File);

            log += FormatCli.KeyValue("PostgreSQL Server", $"{this.Connect.Server}:{this.Connect.Port.ToString()}");
            log += FormatCli.KeyValue("PostgreSQL Database", this.Connect.Database);
            log += FormatCli.KeyValue("PostgreSQL User", this.Connect.User);
            log += FormatCli.KeyValue("PostgreSQL Password", !string.IsNullOrEmpty(this.Connect.Password));

            log += FormatCli.KeyValue("pg_dump Data only", this.Export.Config.DataOnly);
            log += FormatCli.KeyValue("pg_dump Schema only", this.Export.Config.SchemaOnly);
            log += FormatCli.KeyValue("pg_dump Format", this.Export.Config.Format);
            if (this.Export.Config.Format == "d")
            {
                log += FormatCli.KeyValue("pg_dump Worker", this.Export.Config.Worker);
                log += FormatCli.KeyValue("pg_dump Tar export", this.Export.Config.TarDir);
            }

            if (this.Export.Tables?.Count() > 0)
            {
                log += FormatCli.KeyValue("pg_dump Single file", this.Export.Config.SingleFile);
                log += FormatCli.KeyValue("pg_dump Tables", string.Join(", ", this.Export.Tables));
            }
            if (this.Export.TablesExclude?.Count() > 0)
            {
                log += FormatCli.KeyValue("pg_dump Tables exclude", string.Join(", ", this.Export.TablesExclude));
            }

            return(log);
        }
Exemple #4
0
        /// <summary>
        /// Run Task
        /// </summary>
        /// <param name="tstart">Function called when job start</param>
        /// <param name="tasks"></param>
        /// <param name="trun"></param>
        /// <param name="start"></param>
        /// <param name="task"></param>
        /// <param name="end"></param>
        /// <param name="streamLog"></param>
        /// <returns></returns>
        public async Task TaskRun(
            Func <string> tstart,
            int tasks,
            Func <int, Task <string> > trun,
            Action <string> start,
            Action <string, int> task,
            Action <string> end,
            bool streamLog = false)
        {
            string result = tstart();

            // ExportRunAsync start
            start(result);

            // Start watch
            var timer = new Stopwatch();

            timer.Start();

            for (int i = 0; i < tasks; i++)
            {
                // Run Task
                string t = Environment.NewLine;
                t += await trun(i);

                // ExportRunAsync task
                if (streamLog)
                {
                    task(t, i);
                }
                else
                {
                    result += t;
                    task(result, i);
                }
            }

            // Stop watch
            timer.Stop();

            // Write Log
            string e = Environment.NewLine;

            e += FormatCli.KeyValue("Total Duration", timer.ElapsedMilliseconds + " ms");

            // ExportRunAsync task
            if (streamLog)
            {
                end(e);
            }
            else
            {
                result += e;
                end(result);
            }
        }
Exemple #5
0
        private string _ExportParams(int i)
        {
            // Write Log
            string t = FormatCli.H2($"Task: { (i + 1).ToString() }");

            t += FormatCli.KeyValue("File", this.Export.FileName);
            t += FormatCli.H3("Parameter");
            t += $"{ this.Export.BinName } { this.Export.GetPatameters(this.Connect) }" + Environment.NewLine;

            this.ExportTaskPrepare();

            return(t);
        }
Exemple #6
0
        /// <summary>
        /// Run export tasks
        /// </summary>
        /// <param name="start">Job start function</param>
        /// <param name="task">Task run function</param>
        /// <param name="end">Job end function</param>
        /// <param name="streamLog">Stream output</param>
        /// <returns>Task</returns>
        public async Task ValidateRunAsync(Action <string> start, Action <string, int> task, Action <string> end, bool streamLog = false)
        {
            if (this.Files?.Count == 0)
            {
                end("No task to process");
                return;
            }

            Func <string> prepare = () =>
            {
                // Write Log
                string result = FormatCli.H2("Job");
                result += this.OverviewExport();

                return(result);
            };

            await this.TaskRun(prepare, this.Files.Count, this._ValidateRunAsync, start, task, end, streamLog);
        }
Exemple #7
0
        /// <summary>
        /// Run export tasks
        /// </summary>
        /// <param name="start">Job start function</param>
        /// <param name="task">Task run function</param>
        /// <param name="end">Job end function</param>
        /// <param name="streamLog">Stream output</param>
        /// <returns>Task</returns>
        public async Task ExportRunAsync(Action <string> start, Action <string, int> task, Action <string> end, bool streamLog = false)
        {
            if (this.Tasks == 0)
            {
                end("No task to process");
                return;
            }

            Func <string> prepare = () =>
            {
                // Prepare
                this.ExportPrepare();

                // Write Log
                string result = FormatCli.H1("Job");
                result += this.OverviewExport();

                return(result);
            };

            await this.TaskRun(prepare, this.Tasks, this._ExportRunTaskAsync, start, task, end, streamLog);
        }
Exemple #8
0
        private async Task <string> _ImportRunTaskAsync(int i)
        {
            var file = this.Files.ElementAtOrDefault(i);

            // Write Log
            string t = FormatCli.H2($"Task: { (i + 1).ToString() }");

            t += FormatCli.KeyValue("File", file.File);
            t += FormatCli.KeyValue("Checksum", file.Checksum);

            //Checks
            var path = DirFile.Combine(this.AppSettings.Path.Export, file.File);

            if (!System.IO.File.Exists(path))
            {
                t += Environment.NewLine;
                t += $"Error: Could not find file {path}";
                return(t);
            }
            this.Import.File = path;

            // Validate
            if (this.Import.SkipValidation)
            {
                t += FormatCli.KeyValue("Status", "Skipped!");
            }
            else
            {
                var checksum = await Task.Run(() => FilesModel.Hash(path));

                if (checksum != file.Checksum)
                {
                    t += Environment.NewLine;
                    t += $"Error: Checksum is not identical, backup maybe corrupted!";
                    return(t);
                }

                t += FormatCli.KeyValue("Status", "Valid!");
            }

            // Decompress
            var decompress = await this.Import.DecompressAsync();

            // Import
            var task = await this.Import.RunAsync(this.Connect, this.Output);

            task.Duartion.Add(decompress.Duartion);

            t += FormatCli.KeyValue("Duration", task.Duartion.TotalMinutes + " min");
            if (this.Import.Config.Verbose)
            {
                t += FormatCli.H3("Parameter");
                var connector = this.Import.Config.Format == "p" ? new Connectors.Psql(AppSettings).BinName : this.Import.BinName;
                t += $"{ connector } { this.Import.GetPatameters(this.Connect) }" + Environment.NewLine;
            }

            if (!string.IsNullOrEmpty(task.Ouput))
            {
                t += FormatCli.H3("Output:");
                t += task.Ouput;
            }

            if (!string.IsNullOrEmpty(task.Error))
            {
                t += FormatCli.H3("Error:");
                t += task.Error;
            }

            return(t);
        }
Exemple #9
0
        private async Task <string> _ExportRunTaskAsync(int i)
        {
            var path = System.IO.Path.GetDirectoryName(this.Export.File);

            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }

            // ExportRunAsync
            var task = await this.Export.RunAsync(this.Connect, this.Output);

            // Compress export
            var compress = await this.Export.CompressAsync();

            task.Duartion.Add(compress.Duartion);

            // Add to files
            var exported = new FilesModel(this.Export.File, task);

            this.Files.Add(exported);

            // Write log JSON
            if (this.Tasks == 1 || this.Tasks == i + 1)
            {
                var logName = this.Export.File;
                if (this.Tasks != 1)
                {
                    logName = this.Export.File.Replace(this.Export.Tables.Last(), "export");
                }

                this.Import.Config = new PgConfig.PgRestoreModel(this.Export.Config);
                this.JobSave(logName + ".json");
            }

            // Write Log
            string t = FormatCli.H2($"Task: { (i + 1).ToString() }");

            t += FormatCli.KeyValue("File", this.Export.FileName);
            t += FormatCli.KeyValue("Duration", task.Duartion.TotalMinutes + " min");
            t += FormatCli.KeyValue("Checksum", exported.Checksum);
            if (this.Export.Config.Verbose)
            {
                t += FormatCli.H3("Parameter");
                t += $"{ this.Export.BinName } { this.Export.GetPatameters(this.Connect) }" + Environment.NewLine;
            }

            if (!string.IsNullOrEmpty(task.Ouput))
            {
                t += FormatCli.H3("Output:");
                t += task.Ouput;
            }

            if (!string.IsNullOrEmpty(task.Error))
            {
                t += FormatCli.H3("Error:");
                t += task.Error;
            }

            this.ExportTaskPrepare();

            return(t);
        }