Skip to content

Commit 5a2adef

Browse files
committed
2 parents e1775fb + 902a4df commit 5a2adef

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

README.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,173 @@
11
# MySqlBackup_All_DB
22
A tool to backup & restore all databases in one click
33

4+
## Download
5+
https://github.com/MySqlBackupNET/MySqlBackup_All_DB/releases
6+
47
Language: C#<br />
58
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+
A typical code block to backup one specific pre-defined database will be something like this:
14+
15+
```C#
16+
string connstr = "server=localhost;user=root;pwd=1234;database=db1;sslmode=none;convertdatetime=true;";
17+
string backupfile = "C:\\backup.sql";
18+
19+
using (MySqlConnection conn = new MySqlConnection(connstr))
20+
{
21+
using (MySqlCommand cmd = new MySqlCommand())
22+
{
23+
using (MySqlBackup mb = new MySqlBackup(cmd))
24+
{
25+
conn.Open();
26+
cmd.Connection = conn;
27+
28+
mb.ExportToFile(backupfile);
29+
30+
conn.Close();
31+
}
32+
}
33+
}
34+
```
35+
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.
36+
37+
This SQL command will get the list:
38+
39+
```
40+
show databases;
41+
```
42+
Example of code block in C#:
43+
```c#
44+
string connstr = "server=localhost;user=root;pwd=1234;sslmode=none;convertdatetime=true;";
45+
46+
using (MySqlConnection conn = new MySqlConnection(connstr))
47+
{
48+
using (MySqlCommand cmd = new MySqlCommand())
49+
{
50+
using (MySqlBackup mb = new MySqlBackup(cmd))
51+
{
52+
conn.Open();
53+
cmd.Connection = conn;
54+
55+
cmd.CommandText = "show databases;";
56+
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
57+
DataTable dtDbList = new DataTable();
58+
da.Fill(dtDbList);
59+
60+
conn.Close();
61+
}
62+
}
63+
}
64+
```
65+
Next, we loop through the databases one by one to perform the backup work:
66+
67+
```c#
68+
cmd.CommandText = "show databases;";
69+
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
70+
DataTable dtDbList = new DataTable();
71+
da.Fill(dtDbList);
72+
73+
string defaultFolder = "C:\\backup_folder";
74+
75+
foreach(DataRow dr in dtDbList.Rows)
76+
{
77+
string dbname = dr[0] + "";
78+
79+
// skip mysql default system tables
80+
switch (dbname)
81+
{
82+
case "sys":
83+
case "performance_schema":
84+
case "mysql":
85+
case "information_schema":
86+
continue;
87+
}
88+
89+
string dumpFile = System.IO.Path.Combine(defaultFolder, dbname) + ".sql";
90+
91+
cmd.CommandText = "use `" + dbname + "`;";
92+
cmd.ExecuteNonQuery();
93+
94+
mb.ExportToFile(dumpFile);
95+
}
96+
```
97+
## Restore
98+
For the restore task, loop through all the backup files and perform the restore one by one.
99+
100+
Code for getting a list of backup files (dump files):
101+
102+
```c#
103+
string defaultBackupFolder = "C:\\backup_folder";
104+
string[] files = System.IO.Directory.GetFiles(defaultBackupFolder);
105+
106+
foreach (string file in files)
107+
{
108+
if (file.ToLower().EndsWith(".sql"))
109+
{
110+
// Perform backup
111+
}
112+
}
113+
```
114+
Begin of restoration of database:
115+
116+
```c#
117+
foreach (string file in files)
118+
{
119+
if (file.ToLower().EndsWith(".sql"))
120+
{
121+
string dbName = System.IO.Path.GetFileNameWithoutExtension(file);
122+
123+
cmd.CommandText = "create database if not exists `" + dbName + "`";
124+
cmd.ExecuteNonQuery();
125+
126+
cmd.CommandText = "use `" + dbName + "`";
127+
cmd.ExecuteNonQuery();
128+
129+
mb.ImportFromFile(file);
130+
}
131+
}
132+
```
133+
The full code block will be something like this:
134+
135+
```c#
136+
string connstr = "server=localhost;user=root;pwd=1234;sslmode=none;convertdatetime=true;";
137+
138+
using (MySqlConnection conn = new MySqlConnection(connstr))
139+
{
140+
using (MySqlCommand cmd = new MySqlCommand())
141+
{
142+
using (MySqlBackup mb = new MySqlBackup(cmd))
143+
{
144+
conn.Open();
145+
cmd.Connection = conn;
146+
147+
string defaultBackupFolder = "C:\\backup_folder";
148+
string[] files = System.IO.Directory.GetFiles(defaultBackupFolder);
149+
150+
foreach (string file in files)
151+
{
152+
if (file.ToLower().EndsWith(".sql"))
153+
{
154+
string dbName = System.IO.Path.GetFileNameWithoutExtension(file);
155+
156+
cmd.CommandText = "create database if not exists `" + dbName + "`";
157+
cmd.ExecuteNonQuery();
158+
159+
cmd.CommandText = "use `" + dbName + "`";
160+
cmd.ExecuteNonQuery();
161+
162+
mb.ImportFromFile(file);
163+
}
164+
}
165+
166+
conn.Close();
167+
}
168+
}
169+
}
170+
```
171+
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.
172+
173+
One of the possible solution is by running the process using BackgroundWorker so that the process will be executed on separate thread.

0 commit comments

Comments
 (0)