Files
2026-09-03 10:50:01 +02:00

121 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Threading;
namespace StacjaProb
{
public partial class BackUpForm : Form
{
public BackUpForm()
{
InitializeComponent();
}
private void BackUpForm_Load(object sender, EventArgs e)
{
buttonClose.Visible = false;
label1.Visible = false;
}
private void buttonSearch_Click(object sender, EventArgs e)
{
try
{
// Create a SaveFileDialog to request a path and file name to save to.
SaveFileDialog saveRichBoxFile = new SaveFileDialog();
DateTime logdate = DateTime.Now;
// Initialize the SaveFileDialog to specify the RTF extension for the file.
saveRichBoxFile.DefaultExt = "*.sql";
saveRichBoxFile.Filter = "sql|*.sql";
saveRichBoxFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
saveRichBoxFile.FileName = logdate.ToString("yyyyMMdd_HHmmss") + "_softspm_backup";
saveRichBoxFile.FilterIndex = 2;
saveRichBoxFile.RestoreDirectory = true;
progressBar.Value = 0;
progressBar.Maximum = 10;
progressBar.Step = 1;
// Determine if the user selected a file name from the saveFileDialog.
if (saveRichBoxFile.ShowDialog() == System.Windows.Forms.DialogResult.OK && saveRichBoxFile.FileName.Length > 0)
{
buttonSearch.Enabled = false;
label1.Visible = true;
textBoxFilePath.Text = saveRichBoxFile.FileName;
Application.DoEvents();
// Save the contents of the RichTextBox into the file.
using (MySqlCommand cmd = new MySqlCommand())
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = mySqlFunctions.connection;
mySqlFunctions.connection.Open();
progressBar.PerformStep();
mb.ExportInfo.AddCreateDatabase = true;
progressBar.PerformStep();
mb.ExportInfo.ExportTableStructure = true;
progressBar.PerformStep();
mb.ExportInfo.ExportRows = true;
progressBar.PerformStep();
mb.ExportInfo.ExportProcedures = true;
progressBar.PerformStep();
mb.ExportInfo.ExportFunctions = true;
progressBar.PerformStep();
mb.ExportInfo.ExportTriggers = true;
progressBar.PerformStep();
mb.ExportInfo.ExportViews = true;
progressBar.PerformStep();
mb.ExportInfo.ExportEvents = true;
progressBar.PerformStep();
mb.ExportToFile(saveRichBoxFile.FileName);
progressBar.PerformStep();
label1.Visible = false;
buttonSearch.Visible = false;
buttonClose.Visible = true;
}
MessageBox.Show("Kopia zapasowa została pomyślnie zapisana w pliku:\n" + saveRichBoxFile.FileName);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void buttonClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}