├── screenshot.png
├── source code
├── MySqlBackupAll
│ ├── logo.ico
│ ├── MySqlBackupAll.csproj.user
│ ├── Properties
│ │ ├── Settings.settings
│ │ ├── Settings.Designer.cs
│ │ ├── AssemblyInfo.cs
│ │ ├── Resources.Designer.cs
│ │ └── Resources.resx
│ ├── Program.cs
│ ├── packages.config
│ ├── App.config
│ ├── MySqlBackupAll.csproj
│ ├── Form1.Designer.cs
│ └── Form1.cs
└── MySqlBackupAll.sln
├── LICENSE
├── README.md
└── .gitignore
/screenshot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MySqlBackupNET/MySqlBackup_All_DB/HEAD/screenshot.png
--------------------------------------------------------------------------------
/source code/MySqlBackupAll/logo.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MySqlBackupNET/MySqlBackup_All_DB/HEAD/source code/MySqlBackupAll/logo.ico
--------------------------------------------------------------------------------
/source code/MySqlBackupAll/MySqlBackupAll.csproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ShowAllFiles
5 |
6 |
--------------------------------------------------------------------------------
/source code/MySqlBackupAll/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/source code/MySqlBackupAll/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using System.Windows.Forms;
6 |
7 | namespace MySqlBackupAll
8 | {
9 | static class Program
10 | {
11 | ///
12 | /// The main entry point for the application.
13 | ///
14 | [STAThread]
15 | static void Main()
16 | {
17 | Application.EnableVisualStyles();
18 | Application.SetCompatibleTextRenderingDefault(false);
19 | Application.Run(new Form1());
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/source code/MySqlBackupAll/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/source code/MySqlBackupAll/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace MySqlBackupAll.Properties {
12 |
13 |
14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.3.0.0")]
16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
17 |
18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
19 |
20 | public static Settings Default {
21 | get {
22 | return defaultInstance;
23 | }
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | This is free and unencumbered software released into the public domain.
2 |
3 | Anyone is free to copy, modify, publish, use, compile, sell, or
4 | distribute this software, either in source code form or as a compiled
5 | binary, for any purpose, commercial or non-commercial, and by any
6 | means.
7 |
8 | In jurisdictions that recognize copyright laws, the author or authors
9 | of this software dedicate any and all copyright interest in the
10 | software to the public domain. We make this dedication for the benefit
11 | of the public at large and to the detriment of our heirs and
12 | successors. We intend this dedication to be an overt act of
13 | relinquishment in perpetuity of all present and future rights to this
14 | software under copyright law.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | OTHER DEALINGS IN THE SOFTWARE.
23 |
24 | For more information, please refer to
25 |
--------------------------------------------------------------------------------
/source code/MySqlBackupAll/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("MySqlBackupAll")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("MySqlBackupAll")]
13 | [assembly: AssemblyCopyright("Copyright © 2018")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("44c274a2-ee6e-490f-ad83-d452cca485b7")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/source code/MySqlBackupAll/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/source code/MySqlBackupAll.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27703.2035
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MySqlBackupAll", "MySqlBackupAll\MySqlBackupAll.csproj", "{44C274A2-EE6E-490F-AD83-D452CCA485B7}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Debug|x64 = Debug|x64
12 | Debug|x86 = Debug|x86
13 | Release|Any CPU = Release|Any CPU
14 | Release|x64 = Release|x64
15 | Release|x86 = Release|x86
16 | totalRelease|Any CPU = totalRelease|Any CPU
17 | totalRelease|x64 = totalRelease|x64
18 | totalRelease|x86 = totalRelease|x86
19 | EndGlobalSection
20 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
21 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
23 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.Debug|x64.ActiveCfg = Debug|Any CPU
24 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.Debug|x64.Build.0 = Debug|Any CPU
25 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.Debug|x86.ActiveCfg = Debug|Any CPU
26 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.Debug|x86.Build.0 = Debug|Any CPU
27 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
28 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.Release|Any CPU.Build.0 = Release|Any CPU
29 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.Release|x64.ActiveCfg = Release|Any CPU
30 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.Release|x64.Build.0 = Release|Any CPU
31 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.Release|x86.ActiveCfg = Release|Any CPU
32 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.Release|x86.Build.0 = Release|Any CPU
33 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.totalRelease|Any CPU.ActiveCfg = Release|Any CPU
34 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.totalRelease|Any CPU.Build.0 = Release|Any CPU
35 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.totalRelease|x64.ActiveCfg = Release|Any CPU
36 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.totalRelease|x64.Build.0 = Release|Any CPU
37 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.totalRelease|x86.ActiveCfg = Release|Any CPU
38 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}.totalRelease|x86.Build.0 = Release|Any CPU
39 | EndGlobalSection
40 | GlobalSection(SolutionProperties) = preSolution
41 | HideSolutionNode = FALSE
42 | EndGlobalSection
43 | GlobalSection(ExtensibilityGlobals) = postSolution
44 | SolutionGuid = {86FBCF64-E305-4538-AF56-19ED4247E734}
45 | EndGlobalSection
46 | EndGlobal
47 |
--------------------------------------------------------------------------------
/source code/MySqlBackupAll/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace MySqlBackupAll.Properties {
12 | using System;
13 |
14 |
15 | ///
16 | /// A strongly-typed resource class, for looking up localized strings, etc.
17 | ///
18 | // This class was auto-generated by the StronglyTypedResourceBuilder
19 | // class via a tool like ResGen or Visual Studio.
20 | // To add or remove a member, edit your .ResX file then rerun ResGen
21 | // with the /str option, or rebuild your VS project.
22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
25 | internal class Resources {
26 |
27 | private static global::System.Resources.ResourceManager resourceMan;
28 |
29 | private static global::System.Globalization.CultureInfo resourceCulture;
30 |
31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
32 | internal Resources() {
33 | }
34 |
35 | ///
36 | /// Returns the cached ResourceManager instance used by this class.
37 | ///
38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
39 | internal static global::System.Resources.ResourceManager ResourceManager {
40 | get {
41 | if (object.ReferenceEquals(resourceMan, null)) {
42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("MySqlBackupAll.Properties.Resources", typeof(Resources).Assembly);
43 | resourceMan = temp;
44 | }
45 | return resourceMan;
46 | }
47 | }
48 |
49 | ///
50 | /// Overrides the current thread's CurrentUICulture property for all
51 | /// resource lookups using this strongly typed resource class.
52 | ///
53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
54 | internal static global::System.Globalization.CultureInfo Culture {
55 | get {
56 | return resourceCulture;
57 | }
58 | set {
59 | resourceCulture = value;
60 | }
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # MySqlBackup_All_DB
2 | A tool to backup & restore all databases in one click
3 |
4 | ## Download
5 | https://github.com/MySqlBackupNET/MySqlBackup_All_DB/releases
6 |
7 | Language: C#
8 | Platform: WinForm
9 |
10 | ## Backup
11 | This tool is mainly using MySqlBackup.NET (github, codeproject) as core library to perform the backup/restore work.
12 |
13 |
14 |
15 | A typical code block to backup one specific pre-defined database will be something like this:
16 |
17 | ```C#
18 | string connstr = "server=localhost;user=root;pwd=1234;database=db1;sslmode=none;convertdatetime=true;";
19 | string backupfile = "C:\\backup.sql";
20 |
21 | using (MySqlConnection conn = new MySqlConnection(connstr))
22 | {
23 | using (MySqlCommand cmd = new MySqlCommand())
24 | {
25 | using (MySqlBackup mb = new MySqlBackup(cmd))
26 | {
27 | conn.Open();
28 | cmd.Connection = conn;
29 |
30 | mb.ExportToFile(backupfile);
31 |
32 | conn.Close();
33 | }
34 | }
35 | }
36 | ```
37 | In order to backup all databases, we'll need to get a list of databases and loop through them one by one to perform the backup work.
38 |
39 | This SQL command will get the list:
40 |
41 | ```
42 | show databases;
43 | ```
44 | Example of code block in C#:
45 | ```c#
46 | string connstr = "server=localhost;user=root;pwd=1234;sslmode=none;convertdatetime=true;";
47 |
48 | using (MySqlConnection conn = new MySqlConnection(connstr))
49 | {
50 | using (MySqlCommand cmd = new MySqlCommand())
51 | {
52 | using (MySqlBackup mb = new MySqlBackup(cmd))
53 | {
54 | conn.Open();
55 | cmd.Connection = conn;
56 |
57 | cmd.CommandText = "show databases;";
58 | MySqlDataAdapter da = new MySqlDataAdapter(cmd);
59 | DataTable dtDbList = new DataTable();
60 | da.Fill(dtDbList);
61 |
62 | conn.Close();
63 | }
64 | }
65 | }
66 | ```
67 | Next, we loop through the databases one by one to perform the backup work:
68 |
69 | ```c#
70 | cmd.CommandText = "show databases;";
71 | MySqlDataAdapter da = new MySqlDataAdapter(cmd);
72 | DataTable dtDbList = new DataTable();
73 | da.Fill(dtDbList);
74 |
75 | string defaultFolder = "C:\\backup_folder";
76 |
77 | foreach(DataRow dr in dtDbList.Rows)
78 | {
79 | string dbname = dr[0] + "";
80 |
81 | // skip mysql default system tables
82 | switch (dbname)
83 | {
84 | case "sys":
85 | case "performance_schema":
86 | case "mysql":
87 | case "information_schema":
88 | continue;
89 | }
90 |
91 | string dumpFile = System.IO.Path.Combine(defaultFolder, dbname) + ".sql";
92 |
93 | cmd.CommandText = "use `" + dbname + "`;";
94 | cmd.ExecuteNonQuery();
95 |
96 | mb.ExportToFile(dumpFile);
97 | }
98 | ```
99 | ## Restore
100 | For the restore task, loop through all the backup files and perform the restore one by one.
101 |
102 | Code for getting a list of backup files (dump files):
103 |
104 | ```c#
105 | string defaultBackupFolder = "C:\\backup_folder";
106 | string[] files = System.IO.Directory.GetFiles(defaultBackupFolder);
107 |
108 | foreach (string file in files)
109 | {
110 | if (file.ToLower().EndsWith(".sql"))
111 | {
112 | // Perform backup
113 | }
114 | }
115 | ```
116 | Begin of restoration of database:
117 |
118 | ```c#
119 | foreach (string file in files)
120 | {
121 | if (file.ToLower().EndsWith(".sql"))
122 | {
123 | string dbName = System.IO.Path.GetFileNameWithoutExtension(file);
124 |
125 | cmd.CommandText = "create database if not exists `" + dbName + "`";
126 | cmd.ExecuteNonQuery();
127 |
128 | cmd.CommandText = "use `" + dbName + "`";
129 | cmd.ExecuteNonQuery();
130 |
131 | mb.ImportFromFile(file);
132 | }
133 | }
134 | ```
135 | The full code block will be something like this:
136 |
137 | ```c#
138 | string connstr = "server=localhost;user=root;pwd=1234;sslmode=none;convertdatetime=true;";
139 |
140 | using (MySqlConnection conn = new MySqlConnection(connstr))
141 | {
142 | using (MySqlCommand cmd = new MySqlCommand())
143 | {
144 | using (MySqlBackup mb = new MySqlBackup(cmd))
145 | {
146 | conn.Open();
147 | cmd.Connection = conn;
148 |
149 | string defaultBackupFolder = "C:\\backup_folder";
150 | string[] files = System.IO.Directory.GetFiles(defaultBackupFolder);
151 |
152 | foreach (string file in files)
153 | {
154 | if (file.ToLower().EndsWith(".sql"))
155 | {
156 | string dbName = System.IO.Path.GetFileNameWithoutExtension(file);
157 |
158 | cmd.CommandText = "create database if not exists `" + dbName + "`";
159 | cmd.ExecuteNonQuery();
160 |
161 | cmd.CommandText = "use `" + dbName + "`";
162 | cmd.ExecuteNonQuery();
163 |
164 | mb.ImportFromFile(file);
165 | }
166 | }
167 |
168 | conn.Close();
169 | }
170 | }
171 | }
172 | ```
173 | The sample project that I have included in this page is presented in WinForm. If the backup or restore process takes a long time, the program will be frozen and has the high possibility to have the Time Out (more than 60 seconds) error.
174 |
175 | One of the possible solution is by running the process using BackgroundWorker so that the process will be executed on separate thread.
176 |
--------------------------------------------------------------------------------
/source code/MySqlBackupAll/Properties/Resources.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 | text/microsoft-resx
107 |
108 |
109 | 2.0
110 |
111 |
112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
113 |
114 |
115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.suo
8 | *.user
9 | *.userosscache
10 | *.sln.docstates
11 |
12 | # User-specific files (MonoDevelop/Xamarin Studio)
13 | *.userprefs
14 |
15 | # Build results
16 | [Dd]ebug/
17 | [Dd]ebugPublic/
18 | [Rr]elease/
19 | [Rr]eleases/
20 | x64/
21 | x86/
22 | bld/
23 | [Bb]in/
24 | [Oo]bj/
25 | [Ll]og/
26 |
27 | # Visual Studio 2015 cache/options directory
28 | .vs/
29 | # Uncomment if you have tasks that create the project's static files in wwwroot
30 | #wwwroot/
31 |
32 | # MSTest test Results
33 | [Tt]est[Rr]esult*/
34 | [Bb]uild[Ll]og.*
35 |
36 | # NUNIT
37 | *.VisualState.xml
38 | TestResult.xml
39 |
40 | # Build Results of an ATL Project
41 | [Dd]ebugPS/
42 | [Rr]eleasePS/
43 | dlldata.c
44 |
45 | # Benchmark Results
46 | BenchmarkDotNet.Artifacts/
47 |
48 | # .NET Core
49 | project.lock.json
50 | project.fragment.lock.json
51 | artifacts/
52 | **/Properties/launchSettings.json
53 |
54 | *_i.c
55 | *_p.c
56 | *_i.h
57 | *.ilk
58 | *.meta
59 | *.obj
60 | *.pch
61 | *.pdb
62 | *.pgc
63 | *.pgd
64 | *.rsp
65 | *.sbr
66 | *.tlb
67 | *.tli
68 | *.tlh
69 | *.tmp
70 | *.tmp_proj
71 | *.log
72 | *.vspscc
73 | *.vssscc
74 | .builds
75 | *.pidb
76 | *.svclog
77 | *.scc
78 |
79 | # Chutzpah Test files
80 | _Chutzpah*
81 |
82 | # Visual C++ cache files
83 | ipch/
84 | *.aps
85 | *.ncb
86 | *.opendb
87 | *.opensdf
88 | *.sdf
89 | *.cachefile
90 | *.VC.db
91 | *.VC.VC.opendb
92 |
93 | # Visual Studio profiler
94 | *.psess
95 | *.vsp
96 | *.vspx
97 | *.sap
98 |
99 | # TFS 2012 Local Workspace
100 | $tf/
101 |
102 | # Guidance Automation Toolkit
103 | *.gpState
104 |
105 | # ReSharper is a .NET coding add-in
106 | _ReSharper*/
107 | *.[Rr]e[Ss]harper
108 | *.DotSettings.user
109 |
110 | # JustCode is a .NET coding add-in
111 | .JustCode
112 |
113 | # TeamCity is a build add-in
114 | _TeamCity*
115 |
116 | # DotCover is a Code Coverage Tool
117 | *.dotCover
118 |
119 | # AxoCover is a Code Coverage Tool
120 | .axoCover/*
121 | !.axoCover/settings.json
122 |
123 | # Visual Studio code coverage results
124 | *.coverage
125 | *.coveragexml
126 |
127 | # NCrunch
128 | _NCrunch_*
129 | .*crunch*.local.xml
130 | nCrunchTemp_*
131 |
132 | # MightyMoose
133 | *.mm.*
134 | AutoTest.Net/
135 |
136 | # Web workbench (sass)
137 | .sass-cache/
138 |
139 | # Installshield output folder
140 | [Ee]xpress/
141 |
142 | # DocProject is a documentation generator add-in
143 | DocProject/buildhelp/
144 | DocProject/Help/*.HxT
145 | DocProject/Help/*.HxC
146 | DocProject/Help/*.hhc
147 | DocProject/Help/*.hhk
148 | DocProject/Help/*.hhp
149 | DocProject/Help/Html2
150 | DocProject/Help/html
151 |
152 | # Click-Once directory
153 | publish/
154 |
155 | # Publish Web Output
156 | *.[Pp]ublish.xml
157 | *.azurePubxml
158 | # Note: Comment the next line if you want to checkin your web deploy settings,
159 | # but database connection strings (with potential passwords) will be unencrypted
160 | *.pubxml
161 | *.publishproj
162 |
163 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
164 | # checkin your Azure Web App publish settings, but sensitive information contained
165 | # in these scripts will be unencrypted
166 | PublishScripts/
167 |
168 | # NuGet Packages
169 | *.nupkg
170 | # The packages folder can be ignored because of Package Restore
171 | **/packages/*
172 | # except build/, which is used as an MSBuild target.
173 | !**/packages/build/
174 | # Uncomment if necessary however generally it will be regenerated when needed
175 | #!**/packages/repositories.config
176 | # NuGet v3's project.json files produces more ignorable files
177 | *.nuget.props
178 | *.nuget.targets
179 |
180 | # Microsoft Azure Build Output
181 | csx/
182 | *.build.csdef
183 |
184 | # Microsoft Azure Emulator
185 | ecf/
186 | rcf/
187 |
188 | # Windows Store app package directories and files
189 | AppPackages/
190 | BundleArtifacts/
191 | Package.StoreAssociation.xml
192 | _pkginfo.txt
193 | *.appx
194 |
195 | # Visual Studio cache files
196 | # files ending in .cache can be ignored
197 | *.[Cc]ache
198 | # but keep track of directories ending in .cache
199 | !*.[Cc]ache/
200 |
201 | # Others
202 | ClientBin/
203 | ~$*
204 | *~
205 | *.dbmdl
206 | *.dbproj.schemaview
207 | *.jfm
208 | *.pfx
209 | *.publishsettings
210 | orleans.codegen.cs
211 |
212 | # Since there are multiple workflows, uncomment next line to ignore bower_components
213 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
214 | #bower_components/
215 |
216 | # RIA/Silverlight projects
217 | Generated_Code/
218 |
219 | # Backup & report files from converting an old project file
220 | # to a newer Visual Studio version. Backup files are not needed,
221 | # because we have git ;-)
222 | _UpgradeReport_Files/
223 | Backup*/
224 | UpgradeLog*.XML
225 | UpgradeLog*.htm
226 |
227 | # SQL Server files
228 | *.mdf
229 | *.ldf
230 | *.ndf
231 |
232 | # Business Intelligence projects
233 | *.rdl.data
234 | *.bim.layout
235 | *.bim_*.settings
236 |
237 | # Microsoft Fakes
238 | FakesAssemblies/
239 |
240 | # GhostDoc plugin setting file
241 | *.GhostDoc.xml
242 |
243 | # Node.js Tools for Visual Studio
244 | .ntvs_analysis.dat
245 | node_modules/
246 |
247 | # Typescript v1 declaration files
248 | typings/
249 |
250 | # Visual Studio 6 build log
251 | *.plg
252 |
253 | # Visual Studio 6 workspace options file
254 | *.opt
255 |
256 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
257 | *.vbw
258 |
259 | # Visual Studio LightSwitch build output
260 | **/*.HTMLClient/GeneratedArtifacts
261 | **/*.DesktopClient/GeneratedArtifacts
262 | **/*.DesktopClient/ModelManifest.xml
263 | **/*.Server/GeneratedArtifacts
264 | **/*.Server/ModelManifest.xml
265 | _Pvt_Extensions
266 |
267 | # Paket dependency manager
268 | .paket/paket.exe
269 | paket-files/
270 |
271 | # FAKE - F# Make
272 | .fake/
273 |
274 | # JetBrains Rider
275 | .idea/
276 | *.sln.iml
277 |
278 | # CodeRush
279 | .cr/
280 |
281 | # Python Tools for Visual Studio (PTVS)
282 | __pycache__/
283 | *.pyc
284 |
285 | # Cake - Uncomment if you are using it
286 | # tools/**
287 | # !tools/packages.config
288 |
289 | # Tabs Studio
290 | *.tss
291 |
292 | # Telerik's JustMock configuration file
293 | *.jmconfig
294 |
295 | # BizTalk build output
296 | *.btp.cs
297 | *.btm.cs
298 | *.odx.cs
299 | *.xsd.cs
--------------------------------------------------------------------------------
/source code/MySqlBackupAll/MySqlBackupAll.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {44C274A2-EE6E-490F-AD83-D452CCA485B7}
8 | WinExe
9 | MySqlBackupAll
10 | MySqlBackupAll
11 | v4.8
12 | 512
13 | true
14 |
15 |
16 |
17 | AnyCPU
18 | true
19 | full
20 | false
21 | bin\Debug\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 |
26 |
27 | AnyCPU
28 | pdbonly
29 | true
30 | bin\Release\
31 | TRACE
32 | prompt
33 | 4
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 | ..\packages\MySqlBackup.NET.MySqlConnector.2.3.6\lib\net48\MySqlBackupNet.MySqlConnector.dll
42 |
43 |
44 | ..\packages\MySqlConnector.2.1.13\lib\net471\MySqlConnector.dll
45 |
46 |
47 |
48 | ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 | ..\packages\System.Diagnostics.DiagnosticSource.6.0.0\lib\net461\System.Diagnostics.DiagnosticSource.dll
57 |
58 |
59 |
60 |
61 | ..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll
62 |
63 |
64 |
65 | ..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll
66 |
67 |
68 | ..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll
69 |
70 |
71 | ..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 | Form
87 |
88 |
89 | Form1.cs
90 |
91 |
92 |
93 |
94 | Form1.cs
95 |
96 |
97 | ResXFileCodeGenerator
98 | Resources.Designer.cs
99 | Designer
100 |
101 |
102 | True
103 | Resources.resx
104 | True
105 |
106 |
107 |
108 | SettingsSingleFileGenerator
109 | Settings.Designer.cs
110 |
111 |
112 | True
113 | Settings.settings
114 | True
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
--------------------------------------------------------------------------------
/source code/MySqlBackupAll/Form1.Designer.cs:
--------------------------------------------------------------------------------
1 | namespace MySqlBackupAll
2 | {
3 | partial class Form1
4 | {
5 | ///
6 | /// Required designer variable.
7 | ///
8 | private System.ComponentModel.IContainer components = null;
9 |
10 | ///
11 | /// Clean up any resources being used.
12 | ///
13 | /// true if managed resources should be disposed; otherwise, false.
14 | protected override void Dispose(bool disposing)
15 | {
16 | if (disposing && (components != null))
17 | {
18 | components.Dispose();
19 | }
20 | base.Dispose(disposing);
21 | }
22 |
23 | #region Windows Form Designer generated code
24 |
25 | ///
26 | /// Required method for Designer support - do not modify
27 | /// the contents of this method with the code editor.
28 | ///
29 | private void InitializeComponent()
30 | {
31 | System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
32 | this.btSetFolder = new System.Windows.Forms.Button();
33 | this.lbFolder = new System.Windows.Forms.Label();
34 | this.btBackup = new System.Windows.Forms.Button();
35 | this.btRestore = new System.Windows.Forms.Button();
36 | this.txtProgress = new System.Windows.Forms.TextBox();
37 | this.label1 = new System.Windows.Forms.Label();
38 | this.label2 = new System.Windows.Forms.Label();
39 | this.txtConnStr = new System.Windows.Forms.TextBox();
40 | this.cbExportTableStructure = new System.Windows.Forms.CheckBox();
41 | this.cbExportRows = new System.Windows.Forms.CheckBox();
42 | this.SuspendLayout();
43 | //
44 | // btSetFolder
45 | //
46 | this.btSetFolder.Location = new System.Drawing.Point(20, 33);
47 | this.btSetFolder.Margin = new System.Windows.Forms.Padding(4);
48 | this.btSetFolder.Name = "btSetFolder";
49 | this.btSetFolder.Size = new System.Drawing.Size(109, 36);
50 | this.btSetFolder.TabIndex = 0;
51 | this.btSetFolder.Text = "Select Folder";
52 | this.btSetFolder.UseVisualStyleBackColor = true;
53 | this.btSetFolder.Click += new System.EventHandler(this.btSetFolder_Click);
54 | //
55 | // lbFolder
56 | //
57 | this.lbFolder.AutoSize = true;
58 | this.lbFolder.Location = new System.Drawing.Point(136, 41);
59 | this.lbFolder.Name = "lbFolder";
60 | this.lbFolder.Size = new System.Drawing.Size(127, 20);
61 | this.lbFolder.TabIndex = 1;
62 | this.lbFolder.Text = "No Folder Selected";
63 | //
64 | // btBackup
65 | //
66 | this.btBackup.Location = new System.Drawing.Point(20, 109);
67 | this.btBackup.Margin = new System.Windows.Forms.Padding(4);
68 | this.btBackup.Name = "btBackup";
69 | this.btBackup.Size = new System.Drawing.Size(188, 36);
70 | this.btBackup.TabIndex = 2;
71 | this.btBackup.Text = "Backup All Databases";
72 | this.btBackup.UseVisualStyleBackColor = true;
73 | this.btBackup.Click += new System.EventHandler(this.btBackup_Click);
74 | //
75 | // btRestore
76 | //
77 | this.btRestore.Location = new System.Drawing.Point(216, 109);
78 | this.btRestore.Margin = new System.Windows.Forms.Padding(4);
79 | this.btRestore.Name = "btRestore";
80 | this.btRestore.Size = new System.Drawing.Size(189, 36);
81 | this.btRestore.TabIndex = 3;
82 | this.btRestore.Text = "Restore Databases";
83 | this.btRestore.UseVisualStyleBackColor = true;
84 | this.btRestore.Click += new System.EventHandler(this.btRestore_Click);
85 | //
86 | // txtProgress
87 | //
88 | this.txtProgress.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
89 | this.txtProgress.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
90 | this.txtProgress.Location = new System.Drawing.Point(20, 152);
91 | this.txtProgress.Multiline = true;
92 | this.txtProgress.Name = "txtProgress";
93 | this.txtProgress.ReadOnly = true;
94 | this.txtProgress.ScrollBars = System.Windows.Forms.ScrollBars.Both;
95 | this.txtProgress.Size = new System.Drawing.Size(707, 342);
96 | this.txtProgress.TabIndex = 8;
97 | //
98 | // label1
99 | //
100 | this.label1.AutoSize = true;
101 | this.label1.Location = new System.Drawing.Point(16, 9);
102 | this.label1.Name = "label1";
103 | this.label1.Size = new System.Drawing.Size(316, 20);
104 | this.label1.TabIndex = 9;
105 | this.label1.Text = "This program will backup and restore all databases.";
106 | //
107 | // label2
108 | //
109 | this.label2.AutoSize = true;
110 | this.label2.Location = new System.Drawing.Point(16, 76);
111 | this.label2.Name = "label2";
112 | this.label2.Size = new System.Drawing.Size(166, 20);
113 | this.label2.TabIndex = 10;
114 | this.label2.Text = "MySQL Connection String:";
115 | //
116 | // txtConnStr
117 | //
118 | this.txtConnStr.Location = new System.Drawing.Point(188, 73);
119 | this.txtConnStr.Name = "txtConnStr";
120 | this.txtConnStr.Size = new System.Drawing.Size(538, 26);
121 | this.txtConnStr.TabIndex = 11;
122 | //
123 | // cbExportTableStructure
124 | //
125 | this.cbExportTableStructure.AutoSize = true;
126 | this.cbExportTableStructure.Checked = true;
127 | this.cbExportTableStructure.CheckState = System.Windows.Forms.CheckState.Checked;
128 | this.cbExportTableStructure.Location = new System.Drawing.Point(388, 8);
129 | this.cbExportTableStructure.Name = "cbExportTableStructure";
130 | this.cbExportTableStructure.Size = new System.Drawing.Size(170, 24);
131 | this.cbExportTableStructure.TabIndex = 13;
132 | this.cbExportTableStructure.Text = "Export Table\'s Structure";
133 | this.cbExportTableStructure.UseVisualStyleBackColor = true;
134 | //
135 | // cbExportRows
136 | //
137 | this.cbExportRows.AutoSize = true;
138 | this.cbExportRows.Checked = true;
139 | this.cbExportRows.CheckState = System.Windows.Forms.CheckState.Checked;
140 | this.cbExportRows.Location = new System.Drawing.Point(564, 8);
141 | this.cbExportRows.Name = "cbExportRows";
142 | this.cbExportRows.Size = new System.Drawing.Size(104, 24);
143 | this.cbExportRows.TabIndex = 14;
144 | this.cbExportRows.Text = "Export Rows";
145 | this.cbExportRows.UseVisualStyleBackColor = true;
146 | //
147 | // Form1
148 | //
149 | this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
150 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
151 | this.ClientSize = new System.Drawing.Size(738, 507);
152 | this.Controls.Add(this.cbExportRows);
153 | this.Controls.Add(this.cbExportTableStructure);
154 | this.Controls.Add(this.txtConnStr);
155 | this.Controls.Add(this.label2);
156 | this.Controls.Add(this.label1);
157 | this.Controls.Add(this.txtProgress);
158 | this.Controls.Add(this.btRestore);
159 | this.Controls.Add(this.btBackup);
160 | this.Controls.Add(this.lbFolder);
161 | this.Controls.Add(this.btSetFolder);
162 | this.Font = new System.Drawing.Font("Arial Narrow", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
163 | this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
164 | this.Margin = new System.Windows.Forms.Padding(4);
165 | this.Name = "Form1";
166 | this.Text = "MySql Backup Restore All - www.mysqlbackup.net";
167 | this.ResumeLayout(false);
168 | this.PerformLayout();
169 |
170 | }
171 |
172 | #endregion
173 |
174 | private System.Windows.Forms.Button btSetFolder;
175 | private System.Windows.Forms.Label lbFolder;
176 | private System.Windows.Forms.Button btBackup;
177 | private System.Windows.Forms.Button btRestore;
178 | private System.Windows.Forms.TextBox txtProgress;
179 | private System.Windows.Forms.Label label1;
180 | private System.Windows.Forms.Label label2;
181 | private System.Windows.Forms.TextBox txtConnStr;
182 | private System.Windows.Forms.CheckBox cbExportTableStructure;
183 | private System.Windows.Forms.CheckBox cbExportRows;
184 | }
185 | }
186 |
187 |
--------------------------------------------------------------------------------
/source code/MySqlBackupAll/Form1.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.ComponentModel;
4 | using System.Data;
5 | using System.Drawing;
6 | using System.Linq;
7 | using System.Text;
8 | using System.Threading.Tasks;
9 | using System.Windows.Forms;
10 | using MySqlConnector;
11 | using System.IO;
12 |
13 | namespace MySqlBackupAll
14 | {
15 | public partial class Form1 : Form
16 | {
17 | BackgroundWorker bw = new BackgroundWorker();
18 | bool isBackup = true;
19 | int count = 0;
20 | string folder = "";
21 | string server = "";
22 | string user = "";
23 | string pwd = "";
24 | DateTime timeProcessStart = DateTime.Now;
25 | bool hasError = false;
26 | string errmsg = "";
27 |
28 | string constr = "";
29 |
30 | public Form1()
31 | {
32 | InitializeComponent();
33 | bw.WorkerReportsProgress = true;
34 | bw.DoWork += Bw_DoWork;
35 | bw.RunWorkerCompleted += Bw_RunWorkerCompleted;
36 | bw.ProgressChanged += Bw_ProgressChanged;
37 | }
38 |
39 | private void Bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
40 | {
41 | txtProgress.AppendText(e.UserState + "");
42 | txtProgress.Select(txtProgress.Text.Length - 1, 0);
43 | txtProgress.ScrollToCaret();
44 | }
45 |
46 | private void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
47 | {
48 | DateTime timeProcessEnd = DateTime.Now;
49 |
50 | var timeTotal = timeProcessEnd - timeProcessStart;
51 |
52 | this.SuspendLayout();
53 |
54 | txtProgress.Text = txtProgress.Text + "\r\nProcess End at " + timeProcessEnd.ToString("yyyy-MM-dd HH:mm:ss ffff") + "\r\n\r\nTotal time elapsed: " + string.Format("{0} h {1} m {2} s {3} ms", timeTotal.Hours, timeTotal.Minutes, timeTotal.Seconds, timeTotal.Milliseconds);
55 |
56 | if (hasError)
57 | {
58 | txtProgress.AppendText("\r\nError:\r\n\r\n");
59 | txtProgress.AppendText(errmsg);
60 | }
61 |
62 | txtProgress.Select(txtProgress.Text.Length - 1, 0);
63 | txtProgress.ScrollToCaret();
64 |
65 | this.ResumeLayout();
66 | this.PerformLayout();
67 |
68 | if (isBackup)
69 | MessageBox.Show($"{count} databases exported at\r\n\r\n{lbFolder.Text}");
70 | else
71 | MessageBox.Show($"{count} databases imported from\r\n\r\n{lbFolder.Text}");
72 | }
73 |
74 | private void Bw_DoWork(object sender, DoWorkEventArgs e)
75 | {
76 | try
77 | {
78 | if (isBackup)
79 | DoBackup();
80 | else
81 | DoRestore();
82 | }
83 | catch (Exception ex)
84 | {
85 | hasError = true;
86 | errmsg = ex.ToString();
87 | }
88 | }
89 |
90 | void DoRestore()
91 | {
92 | string[] files = Directory.GetFiles(folder);
93 |
94 | DateTime timeProcessStart = DateTime.Now;
95 |
96 | using (MySqlConnection conn = new MySqlConnection(constr))
97 | {
98 | using (MySqlCommand cmd = new MySqlCommand())
99 | {
100 | conn.Open();
101 | cmd.Connection = conn;
102 |
103 | foreach (string file in files)
104 | {
105 | string db = Path.GetFileNameWithoutExtension(file);
106 |
107 | DateTime dateStart = DateTime.Now;
108 |
109 | string appendText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Restore " + db + "....";
110 | bw.ReportProgress(0, appendText);
111 |
112 | cmd.CommandText = "create database if not exists `" + db + "`";
113 | cmd.ExecuteNonQuery();
114 |
115 | cmd.CommandText = $"use `{db}`";
116 | cmd.ExecuteNonQuery();
117 |
118 | using (MySqlBackup mb = new MySqlBackup(cmd))
119 | {
120 | mb.ImportFromFile(file);
121 | }
122 |
123 | DateTime dateEnd = DateTime.Now;
124 |
125 | var timeElapsed = dateEnd - dateStart;
126 |
127 | appendText = $" completed ({timeElapsed.Hours} h {timeElapsed.Minutes} m {timeElapsed.Seconds} s {timeElapsed.Milliseconds} ms)\r\n";
128 | bw.ReportProgress(0, appendText);
129 |
130 | count++;
131 | }
132 |
133 | conn.Close();
134 | }
135 | }
136 | }
137 |
138 | void DoBackup()
139 | {
140 | using (MySqlConnection conn = new MySqlConnection(constr))
141 | {
142 | using (MySqlCommand cmd = new MySqlCommand())
143 | {
144 | conn.Open();
145 | cmd.Connection = conn;
146 |
147 | DataTable dt = GetTable(cmd, "show databases;");
148 |
149 | foreach (DataRow dr in dt.Rows)
150 | {
151 | string db = dr[0] + "";
152 |
153 | switch (db)
154 | {
155 | case "sys":
156 | case "performance_schema":
157 | case "mysql":
158 | case "information_schema":
159 | continue;
160 | }
161 |
162 | DateTime dateStart = DateTime.Now;
163 |
164 | string appendText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Backup " + db + "....";
165 | bw.ReportProgress(0, appendText);
166 |
167 | cmd.CommandText = $"use `{db}`";
168 | cmd.ExecuteNonQuery();
169 |
170 | string file = Path.Combine(lbFolder.Text, $"{db}.sql");
171 |
172 | using (MySqlBackup mb = new MySqlBackup(cmd))
173 | {
174 | mb.ExportInfo.ExportTableStructure = cbExportTableStructure.Checked;
175 | mb.ExportInfo.ExportRows = cbExportRows.Checked;
176 | mb.ExportToFile(file);
177 | }
178 |
179 | DateTime dateEnd = DateTime.Now;
180 |
181 | var timeElapsed = dateEnd - dateStart;
182 |
183 | appendText = $" completed ({timeElapsed.Hours} h {timeElapsed.Minutes} m {timeElapsed.Seconds} s {timeElapsed.Milliseconds} ms)" + "\r\n";
184 | bw.ReportProgress(0, appendText);
185 |
186 | count++;
187 | }
188 |
189 | conn.Close();
190 | }
191 | }
192 | }
193 |
194 | bool LoadData()
195 | {
196 | GC.Collect();
197 | hasError = false;
198 | errmsg = "";
199 | folder = lbFolder.Text;
200 | constr = txtConnStr.Text;
201 | count = 0;
202 | timeProcessStart = DateTime.Now;
203 | txtProgress.Text = "Start at " + timeProcessStart.ToString("yyyy-MM-dd HH:mm:ss ffff") + "\r\n\r\n";
204 | this.Refresh();
205 |
206 | if (constr.Length == 0)
207 | {
208 | MessageBox.Show("Connection string is not set. Cannot continue.");
209 | return false;
210 | }
211 |
212 | return true;
213 | }
214 |
215 | private void btRestore_Click(object sender, EventArgs e)
216 | {
217 | if (lbFolder.Text.Length == 0)
218 | {
219 | MessageBox.Show("Folder is not set");
220 | return;
221 | }
222 |
223 | if (!Directory.Exists(lbFolder.Text))
224 | {
225 | MessageBox.Show("Selected folder is not existed.");
226 | return;
227 | }
228 |
229 | isBackup = false;
230 |
231 | if (!LoadData())
232 | {
233 | return;
234 | }
235 |
236 | bw.RunWorkerAsync();
237 | }
238 |
239 | private void btBackup_Click(object sender, EventArgs e)
240 | {
241 | if (lbFolder.Text.Length == 0)
242 | {
243 | MessageBox.Show("Folder is not set");
244 | return;
245 | }
246 |
247 | if (!Directory.Exists(lbFolder.Text))
248 | {
249 | MessageBox.Show("Selected folder is not existed.");
250 | return;
251 | }
252 |
253 | isBackup = true;
254 |
255 | if (!LoadData())
256 | {
257 | return;
258 | }
259 |
260 | bw.RunWorkerAsync();
261 | }
262 |
263 | private void btSetFolder_Click(object sender, EventArgs e)
264 | {
265 | FolderBrowserDialog fb = new FolderBrowserDialog();
266 | if (fb.ShowDialog() == DialogResult.OK)
267 | {
268 | lbFolder.Text = fb.SelectedPath;
269 | }
270 | }
271 |
272 | DataTable GetTable(MySqlCommand cmd, string sql)
273 | {
274 | cmd.CommandText = sql;
275 | DataTable dt = new DataTable();
276 | MySqlDataAdapter da = new MySqlDataAdapter(cmd);
277 | da.Fill(dt);
278 | return dt;
279 | }
280 |
281 | void Backup()
282 | {
283 | string connstr = "server=localhost;user=root;pwd=1234;sslmode=none;convertdatetime=true;";
284 |
285 | using (MySqlConnection conn = new MySqlConnection(connstr))
286 | {
287 | using (MySqlCommand cmd = new MySqlCommand())
288 | {
289 | using (MySqlBackup mb = new MySqlBackup(cmd))
290 | {
291 | conn.Open();
292 | cmd.Connection = conn;
293 |
294 | cmd.CommandText = "show databases;";
295 | MySqlDataAdapter da = new MySqlDataAdapter(cmd);
296 | DataTable dtDbList = new DataTable();
297 | da.Fill(dtDbList);
298 |
299 | string defaultFolder = "C:\\backup_folder";
300 |
301 |
302 | string defaultBackupFolder = "C:\\backup_folder";
303 | string[] files = System.IO.Directory.GetFiles(defaultBackupFolder);
304 |
305 | foreach (string file in files)
306 | {
307 | if (file.ToLower().EndsWith(".sql"))
308 | {
309 | string dbName = System.IO.Path.GetFileNameWithoutExtension(file);
310 |
311 | cmd.CommandText = "create database if not exists `" + dbName + "`";
312 | cmd.ExecuteNonQuery();
313 |
314 | cmd.CommandText = "use `" + dbName + "`";
315 | cmd.ExecuteNonQuery();
316 |
317 | mb.ImportFromFile(file);
318 | }
319 | }
320 |
321 | conn.Close();
322 | }
323 | }
324 | }
325 |
326 |
327 | }
328 | }
329 | }
--------------------------------------------------------------------------------