-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
221 lines (218 loc) · 9.53 KB
/
Program.cs
File metadata and controls
221 lines (218 loc) · 9.53 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using Chloe.SqlServer;
using System;
using System.Collections.Generic;
using System.IO;
namespace bak
{
class Program
{
static void Main(string[] args)
{
//发布说明,
Console.WriteLine("还原/备份(restore/backup):");
var rep = Console.ReadLine();
while (!(rep.ToLower() == "restore" || rep.ToLower() == "backup"))
{
Console.WriteLine("还原/备份(restore/backup)输入restore或backup继续:");
rep = Console.ReadLine();
}
if (rep == "restore")
{
restore();
}
else if (rep == "backup")
{
backup();
}
else
{
Console.WriteLine("结束");
}
Console.ReadKey();
}
static void backup()
{
string connStr = "data source=.;user id=sa;password=ld123456a*;initial catalog=master";
Console.WriteLine(string.Concat("默认连接字符串:", connStr));
Console.Write("请输入连接字符串(按Enter选择默认连接字符串):");
var newconstr = Console.ReadLine();
if (string.IsNullOrEmpty(newconstr))
{
Console.WriteLine(string.Concat("当前所选择的连接字符串为:", connStr));
}
else
{
connStr = newconstr;
Console.WriteLine(string.Concat("当前所选择的连接字符串为:", connStr));
}
Console.Write("请输入备份路径:");
var dir = Console.ReadLine();
while (string.IsNullOrEmpty(dir) || !Directory.Exists(dir))
{
Console.Write("输入的路径有错误,请输入备份路径:");
dir = Console.ReadLine();
}
Console.Write("请输入要备份的数据库,以【 , 】分割,默认全部库,按回车确认:");
var databaseStr = Console.ReadLine().Replace(",",",");
var databases = databaseStr.Split(',');
var willbackup = new List<string>();
var uselessdatabase = new List<string>();
if (!string.IsNullOrEmpty(databaseStr))
{
//备份全部数据库
//查询每个看看数据库是否都存在
foreach (var item in databases)
{
if (!string.IsNullOrEmpty(item))
{
using (MsSqlContext context = new MsSqlContext(connStr))
{
var obj = context.SqlQuery<dynamic>($"select top 1 * from sys.databases where name = '{item.Trim()}'");
if (obj.Count > 0)
{
willbackup.Add(item.Trim());
}
else
{
uselessdatabase.Add(item.Trim());
}
}
}
}
}
else
{
using (MsSqlContext context = new MsSqlContext(connStr))
{
willbackup = context.SqlQuery<string>($"select name from sys.databases where database_id > 4");
}
}
Console.WriteLine(string.Concat("将备份的数据库为:", string.Join(',', willbackup)));
if (uselessdatabase.Count > 0)
{
Console.WriteLine(string.Concat("以下数据库不存在将跳过:", string.Join(',', uselessdatabase)));
}
Console.WriteLine("是否继续(y/n):");
var contiue = false;
var cont = Console.ReadLine();
while (!(cont.ToLower() == "y" || cont.ToLower() == "n"))
{
Console.WriteLine("是否继续(y/n):");
cont = Console.ReadLine();
}
contiue = cont == "y" ? true : false;
if (contiue)
{
foreach (var item in willbackup)
{
string fileName = Path.Combine(dir,string.Concat(DateTime.Now.ToString("yyyy-MM-dd-hhmmssss_"), item, ".bak"));
try
{
using (MsSqlContext context = new MsSqlContext(connStr))
{
context.Session.CommandTimeout = int.MaxValue;
context.SqlQuery<dynamic>(@$"
BACKUP DATABASE [{item}] TO DISK = '{fileName}'
WITH NOFORMAT, NOINIT, NAME = N'{item}-完整 数据库 备份', SKIP, NOREWIND, NOUNLOAD");
}
Console.WriteLine($"数据库{item}备份成功");
}
catch (Exception ex)
{
Console.WriteLine($"数据库{item}备份失败---{ex.Message}");
}
}
Console.WriteLine($"备份执行结束");
}
else
{
Console.WriteLine("结束");
}
}
static void restore()
{
string connStr = "data source=.;user id=sa;password=ld123456a*;initial catalog=master";
Console.WriteLine(string.Concat("默认连接字符串:", connStr));
Console.Write("请输入连接字符串(按Enter选择默认连接字符串):");
var newconstr = Console.ReadLine();
if (string.IsNullOrEmpty(newconstr))
{
Console.WriteLine(string.Concat("当前所选择的连接字符串为:", connStr));
}
else
{
connStr = newconstr;
Console.WriteLine(string.Concat("当前所选择的连接字符串为:", connStr));
}
Console.Write("请输入bak所在路径:");
var dir = Console.ReadLine();
while (string.IsNullOrEmpty(dir) || !Directory.Exists(dir))
{
Console.Write("输入的路径有错误,请输入bak所在路径:");
dir = Console.ReadLine();
}
Console.Write("请输入还原后的路径:");
var recoverDir = Console.ReadLine();
while (string.IsNullOrEmpty(recoverDir) || !Directory.Exists(recoverDir))
{
Console.Write("输入的路径有错误,请输入还原后的路径:");
recoverDir = Console.ReadLine();
}
Console.WriteLine("是否覆盖(y/n):");
var replace = false;
var rep = Console.ReadLine();
while (!(rep.ToLower() == "y" || rep.ToLower() == "n"))
{
Console.WriteLine("是否覆盖(y/n):");
rep = Console.ReadLine();
}
replace = rep == "y" ? true : false;
string[] files = Directory.GetFiles(dir);
foreach (var item in files)
{
try
{
using (MsSqlContext context = new MsSqlContext(connStr))
{
context.Session.CommandTimeout = int.MaxValue;
var headInfo = context.SqlQuery<dynamic>($"RESTORE HEADERONLY FROM DISK = '{item}'");
var fileInfo = context.SqlQuery<dynamic>($"RESTORE FILELISTONLY from disk= N'{item}'");
if (headInfo.Count < 1)
{
Console.WriteLine($"文件,{item},还原失败");
continue;
}
else
{
var databaseName = headInfo[0].DatabaseName;
var dataName = fileInfo[0].LogicalName;
var logName = fileInfo[1].LogicalName;
//判断数据库是否存在
var hasDataBaseSql = $"select count(1) From master.dbo.sysdatabases where name='{databaseName}'";
var hasDataBase= context.SqlQuery<int>(hasDataBaseSql).First();
if (hasDataBase == 1)
{
var stopConnect = $"ALTER DATABASE {databaseName} SET OFFLINE WITH ROLLBACK IMMEDIATE";
context.SqlQuery<dynamic>(stopConnect);
}
string restorSql = $@"RESTORE DATABASE {databaseName} from disk= N'{item}'
WITH NOUNLOAD,
{(replace ? "REPLACE," : "")}
MOVE '{dataName}' TO '{Path.Combine(recoverDir, string.Concat(databaseName, ".mdf"))}',
MOVE '{logName}' TO '{Path.Combine(recoverDir, string.Concat(databaseName, ".ldf"))}';";
Console.WriteLine($"正在关闭数据{databaseName}的当前连接");
Console.WriteLine($"正在还原{databaseName}");
context.SqlQuery<dynamic>(restorSql);
Console.WriteLine($"还原{databaseName}成功");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
Console.WriteLine($"还原执行结束");
}
}
}