«

Nis 17

Remote Raspbian OS’deki Mariadb’nin yedeğini Windows Lokaline yedekleyen C# desktop uygulaması

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Text;
using System.Linq;
using System.Security.Policy;
using System.Threading.Tasks;
using System.Windows.Forms;

private void btnReports_Click(object sender1, EventArgs e1)
{

StreamReader sRead = File.OpenText(@”config.txt”);
string metin;
while ((metin = sRead.ReadLine()) != null)
{
// Okunan veriler textbox içerisine atılıyor.
string phrase = metin;
string[] words = phrase.Split(‘,’);

if (words[0] != null && words[1] != null && words[2] != null && words[3] != null)
{
server = words[0];
veritabaniAdi = words[1];
kullaniciAdi = words[2];
sifre = Descrpyt(words[3]);
}

}

// İşlem nesnesini oluşturun
string dateTimeString = DateTime.Now.ToString(“yyyy.MM.dd__HH.mm”);
Process process = new Process();

// Hedef dizini oluştur
string targetDirectory = @”C:\DB_YEDEK”;
if (!Directory.Exists(targetDirectory))
{
Directory.CreateDirectory(targetDirectory);
}

// Başlangıç bilgilerini ayarlayın
process.StartInfo.FileName = @”C:\Program Files\MySQL\MySQL Workbench 8.0 CE\mysqldump.exe”;
// –skip-triggers –no-create-info : parametrelerini kullanarak yalnızca verileri içeren ve tablo yapısını içermeyen bir SQL dosyası oluşturur.
process.StartInfo.Arguments = $” –result-file=\”C:\\DB_YEDEK\\{dateTimeString}.sql\” –user={kullaniciAdi} -p{sifre} –host={server} –port=3306 –default-character-set=utf8 –protocol=tcp –skip-triggers –no-create-info \”h4ck3r_charge\””;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;

// Olayları tanımlayın
process.OutputDataReceived += (sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
Console.WriteLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
Console.WriteLine(“Error: ” + e.Data);
}
};

// Komutu çalıştırın
process.Start();

// Çıktı ve hata akışlarını başlatın
Console.WriteLine(“VERİTABANI YEDEKLEME İŞLEMİ DEVAM EDİYOR…”);
//MessageBox.Show(“VERİTABANI YEDEKLEME İŞLEMİ DEVAM EDİYOR…”, “Bilgi”, MessageBoxButtons.OK, MessageBoxIcon.Information);
process.BeginOutputReadLine();

process.BeginErrorReadLine();

// İşlem tamamlanana kadar bekle
process.WaitForExit();

// Yedek dosyasını sıkıştırın
string backupFilePath = $”{targetDirectory}\\{dateTimeString}.sql”;
string compressedFilePath = $”{targetDirectory}\\{dateTimeString}.gz”;
using (FileStream fileToCompress = File.OpenRead(backupFilePath))
{
using (FileStream compressedFileStream = File.Create(compressedFilePath))
{
using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
{
fileToCompress.CopyTo(compressionStream);
}
}
}

string inputFile = $”{targetDirectory}\\{dateTimeString}.sql”;
string compressedFile = $”{targetDirectory}\\{dateTimeString}.gz”;
string encryptedFile = $”{targetDirectory}\\{dateTimeString}.zip”;
string decryptedFile = $”{targetDirectory}\\{dateTimeString}.sql2″;
string password = “AnahtarSecret24April”;

// Dosyayı sıkıştır
CompressFile(inputFile, compressedFile);

// Dosyayı şifrele
EncryptFile(compressedFile, encryptedFile, password);

// Dosyayı çöz
//DecryptFile(encryptedFile, decryptedFile, password);

// Orijinal dosyayı sil
DeleteFile(inputFile);

Console.WriteLine(“Dosya sıkıştırma, şifreleme ve çözme işlemi tamamlandı.”);

Console.WriteLine(“Yedekleme tamamlandı.”);
MessageBox.Show($”VERİTABANI YEDEKLEME İŞLEMİ TAMAMLANDI…\n\n DOSYA KONUMU: {targetDirectory}\\{dateTimeString}.zip”, “Bilgi”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

static void CompressFile(string inputFile, string compressedFile)
{
using (FileStream inputStream = File.OpenRead(inputFile))
{
using (FileStream compressedStream = File.Create(compressedFile))
{
using (GZipStream gzipStream = new GZipStream(compressedStream, CompressionMode.Compress))
{
inputStream.CopyTo(gzipStream);
}
}
}
}

static void EncryptFile(string inputFile, string encryptedFile, string password)
{
byte[] salt = GenerateSalt(); // Tuz oluştur

using (Aes aesAlg = Aes.Create())
{
aesAlg.KeySize = 256; // 256-bit AES kullan
aesAlg.BlockSize = 128; // 128-bit blok boyutu
aesAlg.Padding = PaddingMode.PKCS7; // Dolgu modu
aesAlg.Mode = CipherMode.CBC; // CBC modu

// Anahtar ve IV oluştur
var keyDerivationFunction = new Rfc2898DeriveBytes(password, salt, 10000);
aesAlg.Key = keyDerivationFunction.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = keyDerivationFunction.GetBytes(aesAlg.BlockSize / 8);

using (FileStream inputFileStream = File.OpenRead(inputFile))
{
using (FileStream encryptedFileStream = File.Create(encryptedFile))
{
using (CryptoStream cryptoStream = new CryptoStream(encryptedFileStream, aesAlg.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputFileStream.Read(buffer, 0, buffer.Length)) > 0)
{
cryptoStream.Write(buffer, 0, bytesRead);
}
}
}
}
}
}

static void DecryptFile(string encryptedFile, string decryptedFile, string password)
{
byte[] salt = GenerateSalt(); //

using (Aes aesAlg = Aes.Create())
{
aesAlg.KeySize = 256; // 256-bit AES kullan
aesAlg.BlockSize = 128; // 128-bit blok boyutu
aesAlg.Padding = PaddingMode.PKCS7; // Dolgu modu
aesAlg.Mode = CipherMode.CBC; // CBC modu

// Anahtar ve IV oluştur
var keyDerivationFunction = new Rfc2898DeriveBytes(password, salt, 10000);
aesAlg.Key = keyDerivationFunction.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = keyDerivationFunction.GetBytes(aesAlg.BlockSize / 8);

try
{
using (FileStream encryptedFileStream = File.OpenRead(encryptedFile))
{
using (FileStream decryptedFileStream = File.Create(decryptedFile))
{
using (CryptoStream cryptoStream = new CryptoStream(encryptedFileStream, aesAlg.CreateDecryptor(), CryptoStreamMode.Read))
{
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = cryptoStream.Read(buffer, 0, buffer.Length)) > 0)
{
decryptedFileStream.Write(buffer, 0, bytesRead);
}
}
}
}
}
catch (CryptographicException ex)
{
Console.WriteLine(“Hata: ” + ex.Message);
}
}
}

static byte[] GenerateSalt()
{
byte[] salt = new byte[16];
using (var rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(salt);
}
return salt;
}

static void DeleteFile(string filePath)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}

}

Bir Cevap Yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

Şu HTML etiketlerini ve özelliklerini kullanabilirsiniz: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>