-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackupKit.cs
260 lines (181 loc) · 7.98 KB
/
BackupKit.cs
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FirebirdSql.Data.FirebirdClient;
using FirebirdSql.Data.Services;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.ComponentModel;
using System.Windows.Forms;
using GedeminBasesManager;
namespace FirebirdBackupKit
{
public class BackupKit
{
public TextBox tb;
public string Username { get; set; }
public string Password { get; set; }
public string Server{ get; set; }
public int Port { get; set; }
public BackupKit(string username, string password, string server, int port)
{
if (string.IsNullOrEmpty(username))
throw new ArgumentNullException("username");
if (string.IsNullOrEmpty(password))
throw new ArgumentNullException("password");
if (string.IsNullOrEmpty(server)) server = "localhost";
if (port == 0) Port = 3050;
Username = username;
Password = password;
Server = server;
Port = port;
}
public void Backup(string source, string dest)
{
if (!File.Exists(source))
throw new FileNotFoundException("Source file not found", source);
if (File.Exists(dest))
throw new IOException("Destination file already exists");
//frmBackup.addText("Backing up database");
Debug.WriteLine("Backing up database");
Debug.WriteLine("Source: {0} ({1})", source, GetFileSize(source));
Debug.WriteLine("Destination: {0}", dest, null);
string backupTemp = GetTempName(dest);
Debug.WriteLine("Backing up to temp file - {0}", backupTemp, null);
FbBackup backup = new FbBackup
{
ConnectionString = CreateConnectionString(source),
Verbose = true,
};
backup.BackupFiles.Add(new FbBackupFile(backupTemp, null));
backup.ServiceOutput += ServiceOutput;
backup.Execute();
Debug.WriteLine("Backup complete - {0} ({1})", backupTemp, GetFileSize(backupTemp));
Debug.WriteLine("Renaming temp backup file to - {0}", dest, null);
File.Move(backupTemp, dest);
Debug.WriteLine("Backup complete");
}
public void Restore(string source, string dest, TextBox tbLog)
{
tb = tbLog;
if (!File.Exists(source))
throw new FileNotFoundException("Файл архива базы данных не найден", source);
if (File.Exists(dest))
throw new IOException("Файл базы данных уже существует");
tb.Text = String.Format("Начат процесс восстановления базы данных: {0}" + Environment.NewLine, DateTime.Now);
//tbLog.AppendText("Source: {0} ({1})", source, GetFileSize(source));
//tbLog.AppendText("Destination: {0}", dest, null);
/*
Debug.WriteLine("Restoring database");
Debug.WriteLine("Source: {0} ({1})", source, GetFileSize(source));
Debug.WriteLine("Destination: {0}", dest, null);
*/
string restoreTemp = GetTempName(dest);
tb.AppendText(String.Format("Временный файл: {0}" + Environment.NewLine + Environment.NewLine, restoreTemp));
//Debug.WriteLine("Restoring to temp file - {0}", restoreTemp, null);
FbRestore restore = new FbRestore()
{
ConnectionString = CreateConnectionString(restoreTemp),
Verbose = true,
Options = FbRestoreFlags.Create,
};
restore.BackupFiles.Add(new FbBackupFile(source, null));
restore.ServiceOutput += ServiceOutput;
//Program.
try
{
restore.Execute();
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message);
throw;
// Удалить временный файл
}
//Debug.WriteLine("Restore complete - {0} ({1})", restoreTemp, GetFileSize(restoreTemp));
tb.AppendText(Environment.NewLine + "Переименование временного файла" + Environment.NewLine + Environment.NewLine);
//Debug.WriteLine("Renaming temp restore file to - {0}", dest, null);
File.Move(restoreTemp, dest);
tb.AppendText(String.Format("Закончен процесс восстановления базы данных: {0}" + Environment.NewLine, DateTime.Now));
}
public void Copy(string source, string dest)
{
if (!File.Exists(source))
throw new FileNotFoundException("Source file not found", source);
if (File.Exists(dest))
throw new IOException("Destination file already exists");
Debug.WriteLine("Copying database");
Debug.WriteLine("Source: {0} ({1})", source, GetFileSize(source));
Debug.WriteLine("Destination: {0}", dest, null);
string backupTemp = GetTempName(source);
Debug.WriteLine("Backing up to temp file - {0}", backupTemp, null);
FbBackup backup = new FbBackup
{
ConnectionString = CreateConnectionString(source),
Verbose = true,
Options = FbBackupFlags.NoGarbageCollect,
};
backup.BackupFiles.Add(new FbBackupFile(backupTemp, null));
backup.ServiceOutput += ServiceOutput;
backup.Execute();
Debug.WriteLine("Backup complete - {0} ({1})", backupTemp, GetFileSize(backupTemp));
string restoreTemp = GetTempName(source);
Debug.WriteLine("Restoring to temp file - {0}", restoreTemp, null);
FbRestore restore = new FbRestore()
{
ConnectionString = CreateConnectionString(restoreTemp),
Verbose = true,
Options = FbRestoreFlags.Create,
};
restore.BackupFiles.Add(new FbBackupFile(backupTemp, null));
restore.ServiceOutput += ServiceOutput;
restore.Execute();
Debug.WriteLine("Restore complete - {0} ({1})", restoreTemp, GetFileSize(restoreTemp));
Debug.WriteLine("Renaming temp restore file to - {0}", dest, null);
File.Move(restoreTemp, dest);
Debug.WriteLine("Deleting temp backup file - {0}", backupTemp, null);
File.Delete(backupTemp);
Debug.WriteLine("Copy complete");
}
void ServiceOutput(object sender, ServiceOutputEventArgs e)
{
tb.AppendText(String.Format(e.Message + Environment.NewLine));
Debug.WriteLine(e.Message);
}
string GetTempName(string fileName)
{
return Path.Combine(Path.GetDirectoryName(fileName), Path.GetRandomFileName());
}
string GetFileSize(string fileName)
{
FileInfo fi = new FileInfo(fileName);
return FormatSize(fi.Length);
}
string FormatSize(double size)
{
string[] units = { "kb", "mb", "gb" };
int unit = 0;
size = size / 1024;
while (size >= 1024)
{
size = size / 1024;
unit++;
}
return String.Format("{0:f1}{1}", size, units[unit]);
}
string CreateConnectionString(string database)
{
FbConnectionStringBuilder cs = new FbConnectionStringBuilder
{
UserID = Username,
Password = Password,
Database = database,
DataSource = Server,
Port = Port
};
return cs.ToString();
}
}
}