How to backup mysql database using C# windows application (VS 2019)



Nu Get Packages as below



Form.cs 

using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DB_Backup_2019_winform
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                string root = "";
                string ziproot = "";

                #region CREATE
                try
                {
                    string directorypath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Log\DirectoryInfo.txt");
                    string[] dirfiles = File.ReadAllLines(directorypath);

                     root = dirfiles[0] + "\\" + DateTime.Now.ToString("yyyyMMddHH");
                     ziproot = dirfiles[1] + "\\" + DateTime.Now.ToString("yyyyMMddHH");

                    // If directory does not exist, create it. 
                    if (!Directory.Exists(root))
                    {
                        Directory.CreateDirectory(root);
                    }

                    if (!Directory.Exists(dirfiles[1]))
                    {
                        Directory.CreateDirectory(dirfiles[1]);
                    }
                }
                catch (Exception ex)
                {
                    Lib.Exception.Log("Form1", "Form1_Load CREATE directory", ex.ToString());
                }
                #endregion CREATE

                #region READ
                string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Log\DBInfo.txt");
                string[] files = File.ReadAllLines(path);

                foreach (var item in files)
                {
                    string dbName = "";
                    try
                    {
                        string[] array = item.Split('#');
                        string constring = array[0];
                        dbName = array[1].Trim().ToString();

                        string file = root + "\\" + dbName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".sql";

                        using (MySqlConnection conn = new MySqlConnection(constring))
                        {
                            using (MySqlCommand cmd = new MySqlCommand())
                            {
                                using (MySqlBackup mb = new MySqlBackup(cmd))
                                {
                                    cmd.Connection = conn;
                                    conn.Open();
                                    mb.ExportToFile(file);
                                    conn.Close();
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Lib.Exception.Log("Form1", "Form1_Load BackupLoop", dbName + "_" + ex.ToString());
                    }
                }
                #endregion READ

                #region ZIP
                try
                {
                    string zipPath = ziproot + ".zip";
                    //string extractPath = @"c:\example\extract";

                    if (!File.Exists(zipPath))
                    {
                        ZipFile.CreateFromDirectory(root, zipPath);
                        //ZipFile.ExtractToDirectory(zipPath, extractPath);
                    }
                }
                catch (Exception ex)
                {
                    Lib.Exception.Log("Form1", "Form1_Load ZIP", root + "_" + ex.ToString());
                }
                #endregion ZIP

                #region DELETE
                try
                {
                    DeleteDirectory(root);
                }
                catch (Exception ex)
                {
                    Lib.Exception.Log("Form1", "Form1_Load DeleteDirectory", root + "_" + ex.ToString());
                }
                #endregion DELETE
            }
            catch (Exception ex)
            {
                Lib.Exception.Log("Form1", "Form1_Load", ex.ToString());
            }

            this.Close();
        }

        private void DeleteDirectory(string path)
        {
            try
            {
                if (Directory.Exists(path))
                {
                    //Delete all files from the Directory
                    foreach (string file in Directory.GetFiles(path))
                    {
                        File.Delete(file);
                    }
                    //Delete all child Directories
                    foreach (string directory in Directory.GetDirectories(path))
                    {
                        DeleteDirectory(directory);
                    }
                    //Delete a Directory
                    Directory.Delete(path);
                }
            }
            catch (Exception ex)
            {
                Lib.Exception.Log("Form1", "DeleteDirectory", ex.ToString());
            }
        }
    }
}

Comments

Popular posts from this blog

SALARY CALCULATOR WINDOWS APPLICATION USING NETBEANS

ELECTRON PROJECT LOGIN & SIGNUP FORM