-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathDefault.aspx.cs
More file actions
169 lines (148 loc) · 5.69 KB
/
Copy pathDefault.aspx.cs
File metadata and controls
169 lines (148 loc) · 5.69 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySqlConnector;
using System.Data.SQLite;
using static System.Net.WebRequestMethods;
using System.Security.Cryptography;
namespace System
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
config.GetMySqlInstancePath(out string mysqld_path, out string mysqladmin_path);
if (string.IsNullOrEmpty(mysqld_path))
{
txtMysqldExePath.Text = @"C:\mysql\bin\mysqld.exe";
}
else
{
txtMysqldExePath.Text = mysqld_path;
}
if (string.IsNullOrEmpty(mysqladmin_path))
{
txtMySqlAdminExePath.Text = @"C:\mysql\bin\mysqladmin.exe";
}
else
{
txtMySqlAdminExePath.Text = mysqladmin_path;
}
if (string.IsNullOrEmpty(config.ConnString))
{
txtConnStr.Text = "server=localhost;user=root;pwd=;convertzerodatetime=true;treattinyasboolean=true;";
}
else
{
txtConnStr.Text = config.ConnString;
}
}
}
protected void btBackup_Click(object sender, EventArgs e)
{
try
{
string folder = Server.MapPath("~/App_Data/temp");
Directory.CreateDirectory(folder);
string databaseName = "";
string filename = "";
string filePath = "";
using (var conn = config.GetNewConnection())
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "select database()";
databaseName = cmd.ExecuteScalar() + "";
}
if (string.IsNullOrEmpty(databaseName))
{
throw new Exception("No database selected");
}
filename = $"{databaseName}_{DateTime.Now:yyyy-MM-dd_hhmmss}.sql";
filePath = Path.Combine(folder, filename);
using (var conn = config.GetNewConnection())
using (var cmd = conn.CreateCommand())
using (var mb = new MySqlBackup(cmd))
{
conn.Open();
mb.ExportToFile(filePath);
}
string result = $@"
<span style='color: darkgreen;'>
Backup Success!<br />
<br />
Download File: <a href='/apiFiles?folder=temp&filename={filename}'>{filename}</a><br />
<br />
File saved at: {filePath}</span>";
phBackup.Controls.Add(new LiteralControl(result));
((masterPage1)this.Master).ShowMessage("Success", "Backup Success", true);
}
catch (Exception ex)
{
phBackup.Controls.Add(new LiteralControl($"<pre style='color: maroon;'>Task Failed: {ex.Message}</pre>"));
((masterPage1)this.Master).ShowMessage("Error", "Backup Failed", false);
}
}
protected void btRestore_Click(object sender, EventArgs e)
{
try
{
if (!fileUploadSql.HasFile)
{
throw new Exception("No file uploaded.");
}
if (fileUploadSql.FileName.ToLower().EndsWith(".zip"))
{
throw new Exception("Zip file is not supported in this page. Please go to other pages for zip file.");
}
string folder = Server.MapPath("~/App_Data/temp");
Directory.CreateDirectory(folder);
string databaseName = "";
string filename = "";
string filePath = "";
using (var conn = config.GetNewConnection())
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "select database()";
databaseName = cmd.ExecuteScalar() + "";
}
if (string.IsNullOrEmpty(databaseName))
{
throw new Exception("No database selected");
}
filename = $"{databaseName}_(restore)_{DateTime.Now:yyyy-MM-dd_hhmmss}.txt";
filePath = Path.Combine(folder, filename);
fileUploadSql.SaveAs(filePath);
using (var conn = config.GetNewConnection())
using (var cmd = conn.CreateCommand())
using (var mb = new MySqlBackup(cmd))
{
conn.Open();
mb.ImportFromFile(filePath);
}
string result = $@"
<span style='color: darkgreen;'>
Restore Success!<br />
<br />
Download File: <a href='/apiFiles?folder=temp&filename={filename}'>{filename}</a><br />
<br />
File saved at: {filePath}</span>";
phRestore.Controls.Add(new LiteralControl(result));
((masterPage1)this.Master).ShowMessage("Restore", "Restore Success", true);
}
catch (Exception ex)
{
phRestore.Controls.Add(new LiteralControl($"<pre style='color: maroon;'>Task Failed: {ex.Message}</pre>"));
((masterPage1)this.Master).ShowMessage("Error", "Restore Failed", false);
}
}
}
}