diff --git a/EgtBEAMWALL.DataLayer/App.config b/EgtBEAMWALL.DataLayer/App.config index 3790f96d..450f9b92 100644 --- a/EgtBEAMWALL.DataLayer/App.config +++ b/EgtBEAMWALL.DataLayer/App.config @@ -6,7 +6,7 @@ - + diff --git a/EgtBEAMWALL.DataLayer/DbConfig.cs b/EgtBEAMWALL.DataLayer/DbConfig.cs index 869ae727..64947d18 100644 --- a/EgtBEAMWALL.DataLayer/DbConfig.cs +++ b/EgtBEAMWALL.DataLayer/DbConfig.cs @@ -1,7 +1,10 @@ -using System; +using Ionic.Zip; +using System; using System.Diagnostics; using System.IO; +using System.Linq; using System.Reflection; +using System.Xml.Linq; namespace EgtBEAMWALL.DataLayer { @@ -18,6 +21,7 @@ namespace EgtBEAMWALL.DataLayer public static string DATABASE_SERV = "127.0.0.1"; public static string DATABASE_USER = "EgtUser"; + public static string ZIP_PWD = "viacremasca-viacremasca-viacremasca-viacremasca"; #endregion Public Fields @@ -74,13 +78,14 @@ namespace EgtBEAMWALL.DataLayer } /// - /// Effettua DUMP del DB dato utente admin + percorso salvataggio + /// Effettua DUMP del DB dato utente admin + percorso salvataggio (zip cifrato) /// - /// Percorso di salvataggio del dump (*.sql) + /// Percorso di salvataggio del dump (*.zip) + /// Nome del DB da processare (se vuoto quello di default calcolato) /// Nome o Percorso Eseguibile mysqldump (opzionale) /// Opzioni in fase di export (default: --skip-extended-insert) /// - public static bool DataBaseDumpToFile(string outFilePath, string mysqlDumpPath = "mysqldump", string exportOpt = "--skip-extended-insert") + public static bool DataBaseDumpToFile(string zipFilePath, string dbName = "", string mysqlDumpPath = "mysqldump", string exportOpt = "--skip-extended-insert") { bool fatto = false; // fix path eseguibile @@ -88,36 +93,63 @@ namespace EgtBEAMWALL.DataLayer { mysqlDumpPath = "mysqldump"; } - // aggiungo sql finale - if (!outFilePath.EndsWith(".sql")) + if (string.IsNullOrEmpty(dbName)) { - outFilePath = $"{outFilePath}.sql"; + dbName = DATABASE_NAME; } + // verifica zip finale + if (!zipFilePath.EndsWith(".zip")) + { + zipFilePath = $"{zipFilePath}.zip"; + } + // creo nome file per export sql (temporaneo) + string tempSqlPath = zipFilePath.Replace(".zip", ".sql"); // esecuzione script x dump del DB - string dirPath = Path.GetDirectoryName(outFilePath); + string dirPath = Path.GetDirectoryName(zipFilePath); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } - // se ci fosse già file elimino... - if (File.Exists(outFilePath)) + // se ci fosse già file elimino (sql/zip) + if (File.Exists(tempSqlPath)) { - File.Delete(outFilePath); + File.Delete(tempSqlPath); } - // chiamo script esterno... importante parametro "--skip-extended-insert" altrimenti problemi con restore (anche se + verboso e lento in backup...) - string callScript = $"\"{mysqlDumpPath}\" -u{DATABASE_USER} -p{DATABASE_PWD} {DATABASE_NAME} {exportOpt} > {outFilePath}"; + if (File.Exists(zipFilePath)) + { + File.Delete(zipFilePath); + } + // creazione SQL, tramite script esterno... importante parametro "--skip-extended-insert" altrimenti problemi con restore (anche se + verboso e lento in backup...) + string callScript = $"\"{mysqlDumpPath}\" -u{DATABASE_USER} -p{DATABASE_PWD} {dbName} {exportOpt} > {tempSqlPath}"; ExecuteCommand(callScript); + // compressione con pwd! + using (ZipFile zip = new ZipFile()) + { + // aggiungo pwd + zip.Password = ZIP_PWD; + // inserisco file + zip.AddFile(tempSqlPath, ""); + zip.Save(zipFilePath); + fatto = true; + } + // elimino file sql temporaneo... + if (File.Exists(tempSqlPath)) + { + File.Delete(tempSqlPath); + } return fatto; } /// /// Effettua restore come overwrite del DB di DEFAULT... irreversibile /// - /// Percorso di lettura del dump (*.sql) + /// Percorso di lettura del dump cifrato (*.zip) + /// Nome del DB da processare (se vuoto quello di default calcolato) + /// Percorso cartella temp x esecuzione /// Nome o Percorso Eseguibile mysql (opzionale) /// Opzioni in fase di import (default: --force) /// - public static bool DataBaseRestoreFromFile(string inFilePath, string mysqlPath = "mysql", string importOpt = "--force") + public static bool DataBaseRestoreFromFile(string zipFilePath, string dbName = "", string tempFolder="", string mysqlPath = "mysql", string importOpt = "--force") { bool fatto = false; // fix path eseguibile @@ -125,14 +157,46 @@ namespace EgtBEAMWALL.DataLayer { mysqlPath = "mysql"; } - // aggiungo sql finale - if (!inFilePath.EndsWith(".sql")) + if (string.IsNullOrEmpty(dbName)) { - inFilePath = $"{inFilePath}.sql"; + dbName = DATABASE_NAME; + } + if (string.IsNullOrEmpty(tempFolder)) + { + tempFolder = "C:\\Temp"; + } + // verifica zip finale + if (!zipFilePath.EndsWith(".zip")) + { + zipFilePath = $"{zipFilePath}.zip"; + } + // elimino eventuale temp folder precedente... + if (Directory.Exists(tempFolder)) + { + Directory.Delete(tempFolder, true); + } + Directory.CreateDirectory(tempFolder); + // scompatto file zip --> sql + using (ZipFile zip = ZipFile.Read(zipFilePath)) + { + zip.Password = ZIP_PWD; + zip.ExtractAll(tempFolder, ExtractExistingFileAction.OverwriteSilently); + } + // verifico che ci sia 1 file sql... + var sqlFiles = Directory.GetFiles(tempFolder, "*.sql"); + if (sqlFiles != null && sqlFiles.Length == 1) + { + // verifico nome file per export sql (temporaneo) + string tempSqlPath = sqlFiles.FirstOrDefault(); + // chiamo script esterno... + string callScript = $"\"{mysqlPath}\" -u{DATABASE_USER} -p{DATABASE_PWD} {importOpt} {dbName} < {tempSqlPath}"; + ExecuteCommand(callScript); + // elimino il file sql importato... + File.Delete(tempSqlPath); + // elimino temp folder... + Directory.Delete(tempFolder, true); + fatto = true; } - // chiamo script esterno... - string callScript = $"\"{mysqlPath}\" -u{DATABASE_USER} -p{DATABASE_PWD} {importOpt} {DATABASE_NAME} < {inFilePath}"; - ExecuteCommand(callScript); return fatto; } @@ -184,6 +248,7 @@ namespace EgtBEAMWALL.DataLayer DATABASE_NAME = $"EgtBwDb_{masterKey}"; DATABASE_USER = $"user_{nKey}"; DATABASE_PWD = $"pwd_{sKey}"; + ZIP_PWD = $"{DATABASE_USER}:{DATABASE_PWD}:{DATABASE_USER}:{DATABASE_PWD}"; CONNECTION_STRING = $"server={DATABASE_SERV};port=3306;database={DATABASE_NAME};uid={DATABASE_USER};pwd={DATABASE_PWD};SslMode=None"; // stringa admin con utente root egalware... ADMIN_CONNECTION_STRING = $"server={DATABASE_SERV};port=3306;database=mysql;uid=root;pwd=Egalware_24068!;SslMode=none;CHARSET=utf8"; diff --git a/EgtBEAMWALL.DataLayer/EgtBEAMWALL.DataLayer.csproj b/EgtBEAMWALL.DataLayer/EgtBEAMWALL.DataLayer.csproj index b4fe5e9e..bb64eb8b 100644 --- a/EgtBEAMWALL.DataLayer/EgtBEAMWALL.DataLayer.csproj +++ b/EgtBEAMWALL.DataLayer/EgtBEAMWALL.DataLayer.csproj @@ -40,6 +40,9 @@ ..\packages\Portable.BouncyCastle.1.9.0\lib\net40\BouncyCastle.Crypto.dll + + ..\packages\DotNetZip.1.16.0\lib\net40\DotNetZip.dll + ..\ExtLibs\EgtWPFLib5.dll diff --git a/EgtBEAMWALL.DataLayer/packages.config b/EgtBEAMWALL.DataLayer/packages.config index 7893fa57..c37e7432 100644 --- a/EgtBEAMWALL.DataLayer/packages.config +++ b/EgtBEAMWALL.DataLayer/packages.config @@ -1,6 +1,7 @@  + diff --git a/EgtBEAMWALL.StressTest/App.config b/EgtBEAMWALL.StressTest/App.config index c77c56aa..4d5f0c7e 100644 --- a/EgtBEAMWALL.StressTest/App.config +++ b/EgtBEAMWALL.StressTest/App.config @@ -1,31 +1,51 @@ - + -
+
- + - + - - + + + + + + + + + + + + + + + + + + + + + + - - + + - + diff --git a/EgtBEAMWALL.StressTest/StressTest.Designer.cs b/EgtBEAMWALL.StressTest/StressTest.Designer.cs index 20952769..0425a1bb 100644 --- a/EgtBEAMWALL.StressTest/StressTest.Designer.cs +++ b/EgtBEAMWALL.StressTest/StressTest.Designer.cs @@ -38,6 +38,7 @@ namespace EgtBEAMWALL.StressTest this.btnDbDump = new System.Windows.Forms.Button(); this.btnDbRestore = new System.Windows.Forms.Button(); this.txtDumpFile = new System.Windows.Forms.TextBox(); + this.txtDbName = new System.Windows.Forms.TextBox(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // @@ -105,7 +106,7 @@ namespace EgtBEAMWALL.StressTest // this.btnDbDump.Location = new System.Drawing.Point(18, 136); this.btnDbDump.Name = "btnDbDump"; - this.btnDbDump.Size = new System.Drawing.Size(75, 23); + this.btnDbDump.Size = new System.Drawing.Size(75, 45); this.btnDbDump.TabIndex = 5; this.btnDbDump.Text = "DB Dump"; this.btnDbDump.UseVisualStyleBackColor = true; @@ -115,7 +116,7 @@ namespace EgtBEAMWALL.StressTest // this.btnDbRestore.Location = new System.Drawing.Point(338, 136); this.btnDbRestore.Name = "btnDbRestore"; - this.btnDbRestore.Size = new System.Drawing.Size(75, 23); + this.btnDbRestore.Size = new System.Drawing.Size(75, 45); this.btnDbRestore.TabIndex = 6; this.btnDbRestore.Text = "DB Restore"; this.btnDbRestore.UseVisualStyleBackColor = true; @@ -123,17 +124,26 @@ namespace EgtBEAMWALL.StressTest // // txtDumpFile // - this.txtDumpFile.Location = new System.Drawing.Point(118, 136); + this.txtDumpFile.Location = new System.Drawing.Point(118, 161); this.txtDumpFile.Name = "txtDumpFile"; this.txtDumpFile.Size = new System.Drawing.Size(186, 20); this.txtDumpFile.TabIndex = 3; - this.txtDumpFile.Text = "C:\\Temp\\MyDbDump.sql"; + this.txtDumpFile.Text = "C:\\Temp\\MyDbDump.zip"; + // + // txtDbName + // + this.txtDbName.Location = new System.Drawing.Point(118, 136); + this.txtDbName.Name = "txtDbName"; + this.txtDbName.Size = new System.Drawing.Size(186, 20); + this.txtDbName.TabIndex = 7; + this.txtDbName.Text = "egtbwdb_000142"; // // StressTest // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.txtDbName); this.Controls.Add(this.txtDumpFile); this.Controls.Add(this.btnDbRestore); this.Controls.Add(this.btnDbDump); @@ -159,6 +169,7 @@ namespace EgtBEAMWALL.StressTest private System.Windows.Forms.Button btnDbDump; private System.Windows.Forms.Button btnDbRestore; private System.Windows.Forms.TextBox txtDumpFile; + private System.Windows.Forms.TextBox txtDbName; } } diff --git a/EgtBEAMWALL.StressTest/StressTest.cs b/EgtBEAMWALL.StressTest/StressTest.cs index cc6b9a95..8acf08b6 100644 --- a/EgtBEAMWALL.StressTest/StressTest.cs +++ b/EgtBEAMWALL.StressTest/StressTest.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using System.IO; using System.Windows.Forms; namespace EgtBEAMWALL.StressTest @@ -60,12 +61,13 @@ namespace EgtBEAMWALL.StressTest //string binPath = "C:\\Program Files\\MariaDB 10.5\\bin"; //string myDumpPath = Path.Combine(binPath, "mysqldump.exe"); string outPath = txtDumpFile.Text.Trim(); + string dbName = txtDbName.Text.Trim(); if (!string.IsNullOrEmpty(outPath)) { // effettua dump //DataLayer.DbConfig.DataBaseDumpToFile(outPath, "mysqldump"); //DataLayer.DbConfig.DataBaseDumpToFile(outPath, "mysqldump", "--skip-extended-insert"); - DataLayer.DbConfig.DataBaseDumpToFile(outPath); + DataLayer.DbConfig.DataBaseDumpToFile(outPath, dbName); sw.Stop(); var elapsed = sw.Elapsed; labelResult.Text = $"{DateTime.Now:yyyy/MM/dd HH:mm:ss} | Dump Done in {elapsed.TotalSeconds:N3} sec!"; @@ -81,10 +83,12 @@ namespace EgtBEAMWALL.StressTest { labelResult.Text = "..."; string inPath = txtDumpFile.Text.Trim(); + string dbName = txtDbName.Text.Trim(); + string tempFolder = Path.Combine(Path.GetDirectoryName(inPath), "DbRestore"); Stopwatch sw = new Stopwatch(); sw.Start(); // effettua restore - DataLayer.DbConfig.DataBaseRestoreFromFile(inPath); sw.Stop(); + DataLayer.DbConfig.DataBaseRestoreFromFile(inPath, dbName, tempFolder); sw.Stop(); //string binPath = "C:\\Program Files\\MariaDB 10.5\\bin"; //string mysqlPath = Path.Combine(binPath, "mysql.exe"); //DataLayer.DbConfig.DataBaseRestoreFromFile(inPath, "mysql");