using System.IO.Compression; using System.Collections.Specialized; using ConfigurationManager = System.Configuration.ConfigurationManager; using Docker.DotNet; /*using Microsoft.Extensions.Configuration; using System.Configuration; using System; using System.Diagnostics; using System.Net.Mime; using System.Text; using Devart.Data.PostgreSql; */ namespace backup_file; class Program { private static string ticketNumber; public static string FolderPath; //date of today public readonly static DateTime datetoday = DateTime.Now; public readonly static string todayDateInline = datetoday.ToString("yyyyMMdd"); //static void CompressFolder(string sourceFolder,string destinationFolder, string sourceTarget, string filenameTarget) static void CompressFolder(string sourceFolder,string destinationFolder, Dictionary sourceTargets) { //string compressionTarget = Path.Combine(sourceFolder, sourceTarget); //string destinationPath = Path.Combine(destinationFolder, filenameTarget); string destinationPath = Path.Combine(destinationFolder, "artefacts-"+ticketNumber+"-"+todayDateInline+".zip"); //for debug pruposes /* Console.WriteLine("compressionTarget"+compressionTarget); Console.WriteLine("destinationPath"+destinationPath); Console.WriteLine("destinationFolder"+destinationFolder); Console.WriteLine("sourceFolder"+sourceFolder); Console.WriteLine("sourceTarget"+sourceTarget); */ //creation of the zip file using (ZipArchive archive = ZipFile.Open(destinationPath, ZipArchiveMode.Update)) { /////////////////////////////// /// loop on every files and compress them /////////////////////////////// foreach (KeyValuePair file in sourceTargets) { string filePath = Path.Combine(sourceFolder,file.Value ); //basic check if the file or folder exist if (Path.Exists(filePath)) { Console.WriteLine("=================================================="); Console.WriteLine("Compression of " + filePath + " to " + destinationPath); //Check if we deal with a directory if (Directory.Exists(filePath)) { Console.WriteLine("This is a folder"); try { //get folder info DirectoryInfo directoryInfoFromDirectory = new DirectoryInfo(filePath); //get info of every files inside the folder FileInfo[] filesFromDirectory = directoryInfoFromDirectory.GetFiles("*"); foreach (FileInfo fileInfoFromDirectory in filesFromDirectory) { //compress every individual files based on the info provided above archive.CreateEntryFromFile(fileInfoFromDirectory.FullName, file.Key+"/"+fileInfoFromDirectory.Name); } } catch (Exception e) { Console.WriteLine("Compression not possible for " + file.Key); Console.WriteLine(e.Message); } } else { Console.WriteLine("This is a file"); var fileInfo = new FileInfo(filePath); try { //append the file into the archive archive.CreateEntryFromFile(fileInfo.FullName, fileInfo.Name); } catch (Exception e) { Console.WriteLine("Compression not possible for " + file.Key); Console.WriteLine(e.Message); } } } else { //closing the loop if the file doesn't exist Console.WriteLine("================= ERROR ========================="); Console.WriteLine(file.Value+" doesn't exist"); } } } } static void dockerConnection() { DockerClient client = new DockerClientConfiguration(new Uri("unix:///var/run/docker.sock")).CreateClient(); } /* static void BackupDatabase(string databaseName, string localDatabasePath) { var process = new Process(); var startInfo = new ProcessStartInfo(); startInfo.FileName = Path.Combine("PostgreSQL", "postgresql-restore.bat"); var database = databaseName; var outputPath = localDatabasePath; // use pg_dump, specifying the host, port, user, database to back up, and the output path. // the host, port, user, and database must be an exact match with what's inside your pgpass.conf (Windows) startInfo.Arguments = $@"{host} {port} {user} {database} ""{outputPath}"""; startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; process.StartInfo = startInfo; process.Start(); process.WaitForExit(); process.Close(); } */ static void Main(string[] args) { //https://blog.danskingdom.com/adding-and-accessing-custom-sections-in-your-c-app-config/ var Initialconfiguration = ConfigurationManager.GetSection("settings") as NameValueCollection; string backupPath = Initialconfiguration["backupPath"]; string applicationPath = Initialconfiguration["applicationPath"]; var SourceFiles = ConfigurationManager.GetSection("SourceFiles") as NameValueCollection; Dictionary SoftwareList = SourceFiles.AllKeys.ToDictionary(k => k, k => SourceFiles[k]); /////////////////////////////// //Break of the variables are not set /////////////////////////////// if (String.IsNullOrEmpty(backupPath) || String.IsNullOrEmpty(applicationPath)) { Console.WriteLine("The required variables are not set, please set backupPath and applicationPath in the app.config file "); Environment.Exit(1); } /////////////////////////////// // create the backup destination /////////////////////////////// Console.WriteLine("backupPath: " + backupPath); Console.WriteLine("application_path: " + applicationPath); Console.WriteLine("What is the ticket number ?"); ticketNumber = Console.ReadLine(); //ticketNumber = "DEPLOY-test"; FolderPath = Path.Combine(backupPath, ticketNumber); Console.WriteLine("Backup destination: "+FolderPath); if (!Directory.Exists(FolderPath)) { Directory.CreateDirectory(FolderPath); } CompressFolder(sourceFolder:applicationPath,destinationFolder:FolderPath,SoftwareList); //Add a pause after everything is completed Console.WriteLine("=================================================="); Console.WriteLine("Copy done"); Console.WriteLine("=================================================="); Console.ReadLine(); } }