COmpletato metodi con

- password zip
- gestione temp folder
- ripristino altri DB
This commit is contained in:
Samuele Locatelli
2023-07-27 12:23:53 +02:00
parent 8980da46be
commit dfb3e1f74a
7 changed files with 141 additions and 37 deletions
+84 -19
View File
@@ -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
}
/// <summary>
/// Effettua DUMP del DB dato utente admin + percorso salvataggio
/// Effettua DUMP del DB dato utente admin + percorso salvataggio (zip cifrato)
/// </summary>
/// <param name="outFilePath">Percorso di salvataggio del dump (*.sql)</param>
/// <param name="zipFilePath">Percorso di salvataggio del dump (*.zip)</param>
/// <param name="dbName">Nome del DB da processare (se vuoto quello di default calcolato)</param>
/// <param name="mysqlDumpPath">Nome o Percorso Eseguibile mysqldump (opzionale)</param>
/// <param name="exportOpt">Opzioni in fase di export (default: --skip-extended-insert)</param>
/// <returns></returns>
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;
}
/// <summary>
/// Effettua restore come overwrite del DB di DEFAULT... irreversibile
/// </summary>
/// <param name="inFilePath">Percorso di lettura del dump (*.sql)</param>
/// <param name="zipFilePath">Percorso di lettura del dump cifrato (*.zip)</param>
/// <param name="dbName">Nome del DB da processare (se vuoto quello di default calcolato)</param>
/// <param name="tempFolder">Percorso cartella temp x esecuzione</param>
/// <param name="mysqlPath">Nome o Percorso Eseguibile mysql (opzionale)</param>
/// <param name="importOpt">Opzioni in fase di import (default: --force)</param>
/// <returns></returns>
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} {DATABASE_NAME} < {inFilePath}";
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;
}
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";
@@ -40,6 +40,9 @@
<Reference Include="BouncyCastle.Crypto, Version=1.9.0.0, Culture=neutral, PublicKeyToken=0e99375e54769942, processorArchitecture=MSIL">
<HintPath>..\packages\Portable.BouncyCastle.1.9.0\lib\net40\BouncyCastle.Crypto.dll</HintPath>
</Reference>
<Reference Include="DotNetZip, Version=1.16.0.0, Culture=neutral, PublicKeyToken=6583c7c814667745, processorArchitecture=MSIL">
<HintPath>..\packages\DotNetZip.1.16.0\lib\net40\DotNetZip.dll</HintPath>
</Reference>
<Reference Include="EgtWPFLib5">
<HintPath>..\ExtLibs\EgtWPFLib5.dll</HintPath>
</Reference>
+1
View File
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="BouncyCastle" version="1.8.5" targetFramework="net472" />
<package id="DotNetZip" version="1.16.0" targetFramework="net472" />
<package id="EntityFramework" version="6.4.4" targetFramework="net452" />
<package id="Google.Protobuf" version="3.21.9" targetFramework="net472" />
<package id="K4os.Compression.LZ4" version="1.3.5" targetFramework="net472" />
+21 -1
View File
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
@@ -20,6 +20,26 @@
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.10.9.0" newVersion="6.10.9.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Google.Protobuf" publicKeyToken="a7d26565bac4d604" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.21.9.0" newVersion="3.21.9.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="K4os.Compression.LZ4.Streams" publicKeyToken="2186fa9121ef231d" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.5.0" newVersion="1.3.5.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="BouncyCastle.Crypto" publicKeyToken="0e99375e54769942" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.9.0.0" newVersion="1.9.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.data>
+15 -4
View File
@@ -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;
}
}
+6 -2
View File
@@ -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");