Repository navigation
Expand file tree
/
Copy pathBackupEngine.cs
More file actions
274 lines (246 loc) · 11.4 KB
/
Copy pathBackupEngine.cs
File metadata and controls
274 lines (246 loc) · 11.4 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System.Diagnostics;
using System.Management;
namespace GamingStackGUI
{
/// <summary>
/// One drive GamingStack could point a full image backup at - built by
/// <see cref="BackupEngine.FindEligibleDrives"/>, a pure read that changes
/// nothing. Deliberately shows both free space and the rough estimate needed
/// (see <see cref="EstimatedNeededBytes"/>) rather than silently filtering
/// drives out - the wizard's selection screen discloses both and lets the user
/// judge, same principle as the tweaks consent screen.
/// </summary>
public readonly record struct DriveOption(string RootPath, string Label, long FreeBytes);
public enum BackupTiming { Skip, Before, After }
/// <summary>
/// The "always always happen" System Restore point, plus an optional full
/// disk-image backup via `wbadmin` (the engine behind the old "Backup and
/// Restore (Windows 7)" control panel item - still present and working on
/// Windows 11; there's no newer scriptable equivalent). Deliberately its own
/// class, decoupled from InstallerEngine via events/return values the same way
/// InstallerEngine is decoupled from MainForm - this isn't app installation,
/// it's a separate safety-net concern with its own log file.
/// </summary>
public class BackupEngine
{
public event Action<string>? OnLog;
private string? _logFilePath;
public string? LogFilePath => _logFilePath;
public bool RestorePointCreated { get; private set; }
public string? RestorePointError { get; private set; }
public bool ImageBackupSucceeded { get; private set; }
public string? ImageBackupError { get; private set; }
public string? ImageBackupDestination { get; private set; }
private void Log(string text)
{
OnLog?.Invoke(text);
if (_logFilePath != null)
{
try { File.AppendAllText(_logFilePath, text + Environment.NewLine); }
catch { /* logging shouldn't itself be fatal */ }
}
}
private void EnsureLogFile()
{
if (_logFilePath != null) return;
try
{
var logsDir = InstallerEngine.LogsFolder; // same Documents\GamingStack\logs folder as the install log
Directory.CreateDirectory(logsDir);
_logFilePath = Path.Combine(logsDir, $"backup_{DateTime.Now:yyyyMMdd_HHmmss}.log");
File.WriteAllText(_logFilePath,
$"GamingStack backup log - {DateTime.Now:yyyy-MM-dd HH:mm:ss}{Environment.NewLine}{Environment.NewLine}");
}
catch { /* if this fails, Log() just no-ops the file side */ }
}
private static string SystemDriveRoot =>
Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.Windows)) ?? @"C:\";
/// <summary>
/// Rough estimate of what a full image backup needs: the used space on the
/// system drive. Real backups usually compress somewhat, so this is
/// deliberately a conservative (slightly high) estimate rather than an
/// optimistic one - shown next to each drive's free space so the user can
/// judge for themselves instead of GamingStack silently deciding for them.
/// </summary>
public static long EstimatedNeededBytes()
{
try
{
var sys = new DriveInfo(SystemDriveRoot);
return sys.TotalSize - sys.AvailableFreeSpace;
}
catch
{
return 0;
}
}
public static string FormatBytes(long bytes)
{
double gb = bytes / 1024.0 / 1024.0 / 1024.0;
return $"{gb:0.#} GB";
}
/// <summary>
/// Any drive `wbadmin` could plausibly target: ready, fixed or removable,
/// NTFS (wbadmin refuses anything else), and not the system drive itself
/// (Windows enforces that regardless). A pure read - nothing here changes
/// anything on the machine.
/// </summary>
public static List<DriveOption> FindEligibleDrives()
{
var systemRoot = SystemDriveRoot;
var options = new List<DriveOption>();
try
{
foreach (var drive in DriveInfo.GetDrives())
{
try
{
if (!drive.IsReady) continue;
if (drive.DriveType is not (DriveType.Fixed or DriveType.Removable)) continue;
if (string.Equals(drive.RootDirectory.FullName, systemRoot, StringComparison.OrdinalIgnoreCase)) continue;
if (!string.Equals(drive.DriveFormat, "NTFS", StringComparison.OrdinalIgnoreCase)) continue;
var label = string.IsNullOrWhiteSpace(drive.VolumeLabel)
? drive.Name
: $"{drive.Name} ({drive.VolumeLabel})";
options.Add(new DriveOption(drive.RootDirectory.FullName, label, drive.TotalFreeSpace));
}
catch
{
// One drive being unreadable (e.g. a card reader with nothing
// in it) shouldn't hide every other eligible drive.
}
}
}
catch
{
// Fall through with whatever was found (possibly nothing) - the
// wizard already treats an empty list as "skip cleanly".
}
return options;
}
/// <summary>
/// The mandatory safety net. Tries the direct WMI call first (works if
/// System Restore is already on for the system drive); if that fails,
/// tries enabling System Restore for the system drive first and retries
/// once - Windows ships with System Protection off by default on most
/// consumer machines, and the user's own experience is that restore points
/// silently failing is a real, repeated problem, so this gets one real
/// retry rather than a single silent attempt. Never throws - failure is
/// recorded in <see cref="RestorePointError"/> and disclosed, not hidden,
/// but doesn't block the rest of the run either.
/// </summary>
public void CreateRestorePoint()
{
EnsureLogFile();
Log("[backup] Creating a System Restore point...");
if (TryCreateRestorePointOnce(out var error))
{
RestorePointCreated = true;
Log("[backup] System Restore point created.");
return;
}
Log($"[backup] First attempt failed ({error}) - System Restore may be off for this drive. Enabling it and retrying...");
try
{
var psi = new ProcessStartInfo("powershell",
$"-NoProfile -NonInteractive -Command \"Enable-ComputerRestore -Drive '{SystemDriveRoot}'\"")
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using var p = Process.Start(psi);
p?.WaitForExit(15000);
}
catch (Exception ex)
{
Log($"[backup] Couldn't enable System Restore: {ex.Message}");
}
if (TryCreateRestorePointOnce(out var secondError))
{
RestorePointCreated = true;
Log("[backup] System Restore point created (after enabling System Restore).");
}
else
{
RestorePointError = secondError;
Log($"[backup] Restore point still failed: {secondError}. Continuing without one - " +
"check System Protection in Windows' own System settings if this keeps happening.");
}
}
private static bool TryCreateRestorePointOnce(out string? error)
{
try
{
using var mc = new ManagementClass(@"root\default:SystemRestore");
using var inParams = mc.GetMethodParameters("CreateRestorePoint");
inParams["Description"] = "GamingStack setup";
inParams["RestorePointType"] = 0; // APPLICATION_INSTALL
inParams["EventType"] = 100; // BEGIN_SYSTEM_CHANGE
using var outParams = mc.InvokeMethod("CreateRestorePoint", inParams, null);
var ret = outParams == null ? 1u : Convert.ToUInt32(outParams["ReturnValue"]);
if (ret == 0)
{
error = null;
return true;
}
error = $"CreateRestorePoint returned {ret}";
return false;
}
catch (Exception ex)
{
error = ex.Message;
return false;
}
}
/// <summary>
/// A full system-image backup via `wbadmin`, blocking until it finishes -
/// deliberately not fire-and-forget, since a backup racing against
/// installs/tweaks in the background would capture a half-changed system
/// rather than a clean before/after snapshot (the whole point of asking
/// "before or after" in the first place).
/// </summary>
public async Task<bool> RunImageBackupAsync(string destinationRoot)
{
EnsureLogFile();
ImageBackupDestination = destinationRoot;
Log($"[backup] Starting full disk-image backup to {destinationRoot} - this can take a while...");
try
{
var systemDrive = SystemDriveRoot.TrimEnd('\\');
var target = destinationRoot.TrimEnd('\\');
var psi = new ProcessStartInfo("wbadmin",
$"start backup -backupTarget:{target} -include:{systemDrive} -allCritical -quiet")
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using var p = Process.Start(psi);
if (p == null) throw new InvalidOperationException("wbadmin failed to start");
p.OutputDataReceived += (_, e) => { if (!string.IsNullOrWhiteSpace(e.Data)) Log($"[backup] {e.Data}"); };
p.ErrorDataReceived += (_, e) => { if (!string.IsNullOrWhiteSpace(e.Data)) Log($"[backup] {e.Data}"); };
p.BeginOutputReadLine();
p.BeginErrorReadLine();
await p.WaitForExitAsync();
if (p.ExitCode == 0)
{
ImageBackupSucceeded = true;
Log("[backup] Full disk-image backup completed.");
return true;
}
ImageBackupError = $"wbadmin exited with code {p.ExitCode}";
Log($"[backup] Full disk-image backup failed: exit code {p.ExitCode}");
return false;
}
catch (Exception ex)
{
ImageBackupError = ex.Message;
Log($"[backup] Full disk-image backup failed: {ex.Message}");
return false;
}
}
}
}