-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoSSHApp.cs
More file actions
1364 lines (1291 loc) · 60.2 KB
/
Copy pathAutoSSHApp.cs
File metadata and controls
1364 lines (1291 loc) · 60.2 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region Imports
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Channels;
using System.Threading.Tasks;
using System.Threading;
using System.Formats.Tar;
using Renci.SshNet;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
#endregion Imports
namespace AutoSSH
{
public static class AutoSSHApp
{
internal class HostEntry
{
public string Host { get; set; }
public string Name { get; set; }
public bool IsWindows { get; set; }
public Regex IgnoreRegex { get; set; }
public override string ToString()
{
return Name + " : " + Host;
}
}
// Each host owns its clients. Never run concurrent operations on one SFTP session.
private static readonly int hostWorkers = GetWorkerCount("AUTOSSH_HOST_WORKERS", 8, 64);
private static readonly int downloadWorkers = GetWorkerCount("AUTOSSH_DOWNLOAD_WORKERS", 8, 16);
private static readonly TimeSpan connectionTimeout = TimeSpan.FromSeconds(30);
private static readonly TimeSpan operationTimeout = GetTimeout("AUTOSSH_SFTP_TIMEOUT_SECONDS", 60);
private static readonly TimeSpan commandTimeout = GetTimeout("AUTOSSH_COMMAND_TIMEOUT_SECONDS", 1800);
private const int transferBufferSize = 128 * 1024;
private const uint sftpBufferSize = 64 * 1024;
private static readonly Regex windowsDrivePrefixRegex = new(@"^\/[A-Za-z]\:[/\\]", RegexOptions.Compiled);
private static int GetWorkerCount(string name, int defaultWorkers, int maxWorkers)
{
string value = Environment.GetEnvironmentVariable(name);
if (string.IsNullOrWhiteSpace(value)) return defaultWorkers;
if (int.TryParse(value, out int workers) && workers >= 1 && workers <= maxWorkers) return workers;
throw new ArgumentException($"{name} must be a whole number from 1 to {maxWorkers}.");
}
private static TimeSpan GetTimeout(string name, int defaultSeconds)
{
string value = Environment.GetEnvironmentVariable(name);
if (string.IsNullOrWhiteSpace(value))
{
return TimeSpan.FromSeconds(defaultSeconds);
}
if (!int.TryParse(value, out int seconds) || seconds <= 0 || seconds > int.MaxValue / 1000)
{
throw new ArgumentException($"{name} must be a positive number of seconds no greater than {int.MaxValue / 1000}.");
}
return TimeSpan.FromSeconds(seconds);
}
internal static void ConfigureClient(BaseClient client)
{
client.ConnectionInfo.Timeout = connectionTimeout;
client.KeepAliveInterval = TimeSpan.FromSeconds(15);
if (client is SftpClient sftpClient)
{
sftpClient.OperationTimeout = operationTimeout;
sftpClient.BufferSize = sftpBufferSize;
}
}
private static class Diag
{
private static readonly ConcurrentDictionary<string, (string Op, long Start)> inflight = new();
private static readonly object gate = new();
private static StreamWriter writer;
private static Stopwatch clock;
private static long nextId;
internal static string Path { get; private set; }
internal static void Start()
{
string dir = System.IO.Path.GetDirectoryName(Environment.ProcessPath);
if (string.IsNullOrEmpty(dir)) dir = AppContext.BaseDirectory;
Directory.CreateDirectory(dir);
Path = System.IO.Path.Combine(dir, "AutoSSH.log");
clock = Stopwatch.StartNew();
var stream = new FileStream(Path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
lock (gate)
{
writer = new StreamWriter(stream) { AutoFlush = true };
}
Write($"==== AutoSSH {DateTime.Now:yyyy-MM-dd HH:mm:ss} pid={Environment.ProcessId} ====");
Write("exe=" + (Environment.ProcessPath ?? "(unknown)"));
Write("log=" + Path);
Write($"hosts={hostWorkers} downloadWorkers={downloadWorkers} sftpTimeout={operationTimeout.TotalSeconds}s commandTimeout={commandTimeout.TotalSeconds}s");
}
internal static void Stop()
{
Write("==== finished ====");
lock (gate)
{
writer?.Dispose();
writer = null;
}
}
internal static void Write(string message)
{
lock (gate)
{
if (writer == null) return;
writer.WriteLine($"{DateTime.Now:HH:mm:ss.fff} +{clock.Elapsed:hh\\:mm\\:ss\\.fff} t{Environment.CurrentManagedThreadId} {message}");
}
}
internal static string Begin(string op)
{
if (writer == null) return null;
string key = Interlocked.Increment(ref nextId).ToString();
inflight[key] = (op, Stopwatch.GetTimestamp());
Write($"BEGIN [{key}] {op}");
return key;
}
internal static void End(string key, string extra = null)
{
if (key == null) return;
string suffix = extra == null ? "" : " " + extra;
if (inflight.TryRemove(key, out var a))
{
double ms = (Stopwatch.GetTimestamp() - a.Start) * 1000.0 / Stopwatch.Frequency;
Write($"END [{key}] {a.Op} {ms:0}ms{suffix}");
}
else
{
Write($"END [{key}]{suffix}");
}
}
internal static void Fail(string key, Exception ex)
{
Write($"FAIL [{key ?? "?"}] {ex.GetType().Name}: {ex.Message}");
End(key, "FAILED");
}
internal static void Heartbeat()
{
if (writer == null) return;
var ops = inflight.Select(kv =>
{
double sec = (Stopwatch.GetTimestamp() - kv.Value.Start) / (double)Stopwatch.Frequency;
return $"{kv.Value.Op} ({sec:0.0}s)";
}).OrderByDescending(s => s).ToArray();
Write($"HEART down={BytesToString(Interlocked.Read(ref bytesDownloaded))} up={BytesToString(Interlocked.Read(ref bytesUploaded))} skip={BytesToString(Interlocked.Read(ref bytesSkipped))} inflight={ops.Length}");
foreach (string op in ops)
{
Write(" ... " + op);
}
}
}
// Accept trailing terminal color/reset sequences as well as plain prompts.
private const string promptSuffix = @"(?:[ \t]|\x1b\[[0-?]*[ -/]*[@-~])*\r?$";
internal static readonly Regex loginPromptRegex = new Regex(@"(?m)[$#>]" + promptSuffix);
internal static readonly Regex rootPromptRegex = new Regex(@"(?m)#" + promptSuffix);
internal static readonly Regex windowsPromptRegex = new Regex(@"(?m)>" + promptSuffix);
internal static readonly Regex sudoPromptRegex = new Regex(@"(?m)[Pp]assword[^\r\n]*:" + promptSuffix + "|" + rootPromptRegex);
private static SecureString userName;
private static SecureString password;
private static long bytesDownloaded;
private static long bytesUploaded;
private static long bytesSkipped;
private static void WriteSecure(SecureString secureString, ShellStream writer)
{
IntPtr unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secureString);
try
{
byte[] buf = new byte[2];
for (int i = 0; i < secureString.Length * 2; )
{
buf[0] = Marshal.ReadByte(unmanagedString, i++);
buf[1] = Marshal.ReadByte(unmanagedString, i++);
writer.Write(BitConverter.ToChar(buf).ToString());
}
writer.Write("\n");
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
}
}
private static string SecureStringToString(SecureString secureString)
{
StringBuilder b = new StringBuilder();
IntPtr unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secureString);
try
{
byte[] buf = new byte[2];
for (int i = 0; i < secureString.Length * 2;)
{
buf[0] = Marshal.ReadByte(unmanagedString, i++);
buf[1] = Marshal.ReadByte(unmanagedString, i++);
b.Append(BitConverter.ToChar(buf));
}
return b.ToString();
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
}
}
private static void SecureStringFromString(ref SecureString secureString, string text)
{
secureString = new SecureString();
foreach (char c in text)
{
secureString.AppendChar(c);
}
}
private static List<KeyValuePair<HostEntry, List<string>>> LoadCommands(string commandFile)
{
List<KeyValuePair<HostEntry, List<string>>> commands = new List<KeyValuePair<HostEntry, List<string>>>();
List<string> lines = new List<string>();
List<string> inheritedLines = new List<string>();
HostEntry currentEntry = null;
string cleanedLine;
bool isHostLine = false;
int lineIndex = 0;
Dictionary<string, string> replacers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (string line in File.ReadAllLines(commandFile))
{
// clean and trim
cleanedLine = line.Trim();
int pos = cleanedLine.IndexOf('#');
if (pos >= 0)
{
cleanedLine = cleanedLine.Substring(0, pos).Trim();
}
// replace any find and replace directives
foreach (var kv in replacers)
{
cleanedLine = cleanedLine.Replace(kv.Key, kv.Value, StringComparison.OrdinalIgnoreCase);
}
// look for defines ($...$=value)
Match replacer = Regex.Match(cleanedLine, @"(?<name>\$[^\$]+\$)\s*=\s*(?<value>.+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
if (replacer.Success)
{
replacers[replacer.Groups["name"].Value] = replacer.Groups["value"].Value;
continue;
}
// check if this is a host
isHostLine = cleanedLine.StartsWith("$host", StringComparison.OrdinalIgnoreCase);
if (currentEntry != null && (cleanedLine.Length == 0 || isHostLine))
{
if (currentEntry.Name != "*" && currentEntry.Host != "*")
{
lines.AddRange(inheritedLines);
commands.Add(new KeyValuePair<HostEntry, List<string>>(currentEntry, lines));
lines = new List<string>();
currentEntry = null;
}
}
if (isHostLine)
{
// found a host, set the current entry
string[] pieces = cleanedLine.Split(' ');
if (pieces.Length < 3)
{
throw new InvalidOperationException("Host line format is $host name dns_or_address, line: " + lineIndex);
}
if (pieces[1] == "*" && pieces[2] == "*")
{
currentEntry = new HostEntry { Name = "*", Host = "*" };
inheritedLines.Clear();
}
else
{
currentEntry = new HostEntry { Name = pieces[1], Host = pieces[2],
IsWindows = (pieces.Length < 4 ? false : pieces[3].Equals("windows", StringComparison.OrdinalIgnoreCase)) };
}
}
else if (cleanedLine.Length != 0)
{
if (currentEntry == null)
{
throw new InvalidOperationException("Must define a $host before commands, line: " + lineIndex);
}
else if (currentEntry.Name == "*" && currentEntry.Host == "*")
{
inheritedLines.Add(cleanedLine);
}
else
{
lines.Add(cleanedLine);
}
}
lineIndex++;
}
lines.AddRange(inheritedLines);
if (currentEntry != null && lines.Count != 0)
{
// add commands for this host
commands.Add(new KeyValuePair<HostEntry, List<string>>(currentEntry, lines));
}
return commands;
}
private static List<KeyValuePair<HostEntry, List<string>>> Initialize(string commandFile, string backupRoot)
{
string loginPath = Path.Combine(backupRoot, "login.key");
if (!File.Exists(loginPath))
{
Console.Write("Enter user name: ");
string userName = Console.ReadLine();
Console.Write("Enter password: ");
string password = string.Empty;
while (true)
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
else
{
password += key.KeyChar;
}
}
byte[] bytes = ProtectedData.Protect(Encoding.UTF8.GetBytes(userName + "|" + password), null,
DataProtectionScope.CurrentUser);
File.WriteAllBytes(loginPath, bytes);
}
if (!File.Exists(loginPath))
{
throw new FileNotFoundException("Missing login.key file");
}
{
byte[] protectedBytes = File.ReadAllBytes(loginPath);
string unprotectedBytes = Encoding.UTF8.GetString(ProtectedData.Unprotect(protectedBytes, null,
DataProtectionScope.CurrentUser));
int pos = unprotectedBytes.IndexOf('|');
if (pos < 0)
{
throw new ArgumentException("Corrupted login.key file, delete and restart");
}
SecureStringFromString(ref userName, unprotectedBytes.Substring(0, pos));
SecureStringFromString(ref password, unprotectedBytes.Substring(++pos));
protectedBytes = null;
unprotectedBytes = null;
GC.Collect();
}
return LoadCommands(commandFile);
}
private static async Task<BaseClient> ConnectAsync(string root, HostEntry host, bool ssh)
{
Console.WriteLine("Connecting to {0} with type {1}", host, ssh ? "SSH" : "SFTP");
string op = Diag.Begin($"{host} connect {(ssh ? "SSH" : "SFTP")}");
root = Path.Combine(root, host.Name);
Directory.CreateDirectory(root);
string fingerFile = Path.Combine(root, "finger.key");
byte[] fingerprint = File.Exists(fingerFile) ? File.ReadAllBytes(fingerFile) : null;
var insecureUserName = SecureStringToString(userName);
var insecurePassword = SecureStringToString(password);
BaseClient client = ssh ? new SshClient(host.Host, insecureUserName, insecurePassword) : new SftpClient(host.Host, insecureUserName, insecurePassword);
ConfigureClient(client);
bool fingerMatch = true;
client.HostKeyReceived += (sender, e) =>
{
if (fingerprint != null)
{
if (!e.FingerPrint.SequenceEqual(fingerprint))
{
e.CanTrust = false;
fingerMatch = false;
}
}
else
{
File.WriteAllBytes(fingerFile, e.FingerPrint);
fingerprint = e.FingerPrint.ToArray();
}
};
try
{
await client.ConnectAsync(CancellationToken.None);
if (!client.IsConnected || !client.ConnectionInfo.IsAuthenticated)
{
throw new SshConnectionException($"Failed to connect to {host}, finger match: {fingerMatch}");
}
Diag.End(op, fingerMatch ? "ok" : "fingerprint-mismatch");
return client;
}
catch (Exception ex)
{
Diag.Fail(op, ex);
client.Dispose();
throw;
}
}
private static string BytesToString(long byteCount)
{
string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB
if (byteCount == 0)
return "0" + suf[0];
long bytes = Math.Abs(byteCount);
int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
double num = Math.Round(bytes / Math.Pow(1024, place), 2);
return (Math.Sign(byteCount) * num).ToString() + suf[place];
}
// SSH.NET queues progress callbacks on the thread pool, so delivery can be out of order.
internal sealed class TransferProgress : IProgress<DownloadFileProgressReport>, IProgress<UploadFileProgressReport>
{
private long reported;
private readonly Action<long> addBytes;
internal TransferProgress(Action<long> addBytes) => this.addBytes = addBytes;
void IProgress<DownloadFileProgressReport>.Report(DownloadFileProgressReport value) =>
Report(value.TotalBytesDownloaded);
void IProgress<UploadFileProgressReport>.Report(UploadFileProgressReport value) =>
Report(value.TotalBytesUploaded);
internal void Report(ulong value)
{
long progress = checked((long)value);
long previous;
do
{
previous = Interlocked.Read(ref reported);
if (progress <= previous)
{
return;
}
}
while (Interlocked.CompareExchange(ref reported, progress, previous) != previous);
addBytes(progress - previous);
}
}
private static FileStream OpenLocalTransferStream(string path, FileMode mode, FileAccess access, FileShare share) =>
new FileStream(path, mode, access, share, transferBufferSize,
FileOptions.Asynchronous | FileOptions.SequentialScan);
private static async Task WhenAllPreferSessionErrors(params Task[] tasks)
{
try
{
await Task.WhenAll(tasks);
}
catch
{
Exception[] errors = tasks.Where(t => t.IsFaulted)
.SelectMany(t => t.Exception!.Flatten().InnerExceptions)
.ToArray();
Exception error = Array.Find(errors, static e => e is not OperationCanceledException)
?? errors.FirstOrDefault();
if (error != null)
{
ExceptionDispatchInfo.Capture(error).Throw();
}
throw;
}
}
private static string BackupFileName(string root, string remotePath)
{
// trim rooted paths, including drive letters
string localPath = windowsDrivePrefixRegex.Replace(remotePath, string.Empty).Trim('/', '\\');
return Path.Combine(root, localPath);
}
private static async Task<long> BackupFileAsync(string root, BackupEntry file, ISftpClient client,
CancellationToken cancellation)
{
string fileName = BackupFileName(root, file.FullName);
long transferred = 0;
long downloaded = 0;
bool committed = false;
string op = Diag.Begin($"download {file.FullName} {BytesToString(file.Length)}");
try
{
string tempFile = fileName + "." + Guid.NewGuid().ToString("N") + ".__TEMP__";
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
try
{
await using (FileStream stream = OpenLocalTransferStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
var progress = new TransferProgress(delta =>
{
Interlocked.Add(ref transferred, delta);
Interlocked.Add(ref bytesDownloaded, delta);
});
await client.DownloadFileAsync(file.FullName, stream, progress, cancellation);
await stream.FlushAsync(cancellation);
progress.Report((ulong)stream.Length);
downloaded = stream.Length;
}
// Listing/find size is a snapshot. Live json/sqlite/log files grow and shrink.
// SFTP reads until EOF, so a finished transfer is complete even if the size moved.
if (downloaded != file.Length)
{
Diag.Write($"size changed during download {file.FullName} listed={file.Length} got={downloaded}");
}
File.SetLastWriteTimeUtc(tempFile, file.LastWriteTimeUtc);
File.Move(tempFile, fileName, overwrite: true);
committed = true;
Diag.End(op, BytesToString(downloaded));
return downloaded;
}
finally
{
try
{
File.Delete(tempFile);
}
catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException)
{
Console.WriteLine("Unable to remove temporary file {0}: {1}", tempFile, ex.Message);
}
}
}
catch (SftpPathNotFoundException)
{
Diag.End(op, "missing");
return 0;
}
catch (SftpPermissionDeniedException)
{
Diag.End(op, "denied");
return 0;
}
catch (Exception ex) when (ex is not SshException && ex is not TimeoutException && ex is not OperationCanceledException)
{
Console.WriteLine("Error: {0} ", ex.Message);
Diag.Fail(op, ex);
return 0;
}
catch (Exception ex)
{
Diag.Fail(op, ex);
throw;
}
finally
{
if (!committed && transferred != 0)
{
Interlocked.Add(ref bytesDownloaded, -transferred);
}
}
}
internal sealed record BackupEntry(string FullName, string Name, bool IsRegularFile,
bool IsDirectory, long Length, DateTime LastWriteTimeUtc);
private static async Task<BackupEntry> ReadBackupRootAsync(ISftpClient client, string path,
TextWriter log, CancellationToken cancellation)
{
try
{
var attributes = await client.GetAttributesAsync(path, cancellation);
return new BackupEntry(path, Path.GetFileName(path.TrimEnd('/', '\\')),
attributes.IsRegularFile, attributes.IsDirectory, attributes.Size, attributes.LastWriteTimeUtc);
}
catch (SftpPathNotFoundException)
{
Diag.Write("stat missing " + path);
}
catch (SftpPermissionDeniedException ex)
{
log.WriteLine("Error backing up {0}: {1}", path, ex.Message);
}
catch (Exception ex) when (ex is not SshException && ex is not TimeoutException && ex is not OperationCanceledException)
{
log.WriteLine("Error backing up {0}: {1}", path, ex);
}
return null;
}
internal static async Task<long> BackupFileAsync(string root, string remotePath, ISftpClient client,
CancellationToken cancellation = default)
{
var file = await ReadBackupRootAsync(client, remotePath, TextWriter.Null, cancellation);
if (file == null || !file.IsRegularFile) return 0;
string localFile = BackupFileName(root, remotePath);
if (File.Exists(localFile) && file.LastWriteTimeUtc <= File.GetLastWriteTimeUtc(localFile))
{
Interlocked.Add(ref bytesSkipped, file.Length);
return file.Length;
}
return await BackupFileAsync(root, file, client, cancellation);
}
private static async Task<List<BackupEntry>> ReadBackupEntriesAsync(ISftpClient client, string path,
TextWriter log, CancellationToken cancellation)
{
var entries = new List<BackupEntry>();
string op = Diag.Begin("list " + path);
try
{
await foreach (var file in client.ListDirectoryAsync(path, cancellation))
{
entries.Add(new BackupEntry(file.FullName, file.Name, file.IsRegularFile,
file.IsDirectory, file.Length, file.LastWriteTimeUtc));
}
Diag.End(op, entries.Count + " entries");
}
catch (SftpPathNotFoundException)
{
Diag.End(op, "missing");
}
catch (SftpPermissionDeniedException ex)
{
Diag.Fail(op, ex);
log.WriteLine("Error backing up {0}: {1}", path, ex.Message);
}
catch (Exception ex) when (ex is not SshException && ex is not TimeoutException && ex is not OperationCanceledException)
{
Diag.Fail(op, ex);
log.WriteLine("Error backing up {0}: {1}", path, ex);
}
catch (Exception ex)
{
Diag.Fail(op, ex);
throw;
}
return entries;
}
private static string QuoteUnix(string value) => "'" + value.Replace("'", "'\\''") + "'";
private static readonly byte[] tarNameNul = { 0 };
internal static string NormalizeTarEntryName(string name)
{
if (string.IsNullOrEmpty(name)) return "/";
name = name.Replace('\\', '/');
while (name.StartsWith("./", StringComparison.Ordinal)) name = name[2..];
if (name.Length > 1) name = name.TrimEnd('/');
if (name.Length == 0) return "/";
if (name[0] != '/') name = "/" + name;
return name;
}
internal static async Task<long> ExtractTarBackupAsync(string root,
IReadOnlyDictionary<string, BackupEntry> files, HashSet<string> remaining, Stream tarStream,
CancellationToken cancellation = default)
{
long extracted = 0;
int count = 0;
await using TarReader reader = new TarReader(tarStream, leaveOpen: true);
while (await reader.GetNextEntryAsync(copyData: false, cancellation) is TarEntry entry)
{
bool wanted = entry.EntryType is TarEntryType.RegularFile or TarEntryType.V7RegularFile
or TarEntryType.ContiguousFile;
string remotePath = wanted ? NormalizeTarEntryName(entry.Name) : null;
if (!wanted || remotePath == null || !files.TryGetValue(remotePath, out BackupEntry file) ||
!remaining.Contains(file.FullName))
{
if (entry.DataStream != null)
{
await entry.DataStream.CopyToAsync(Stream.Null, cancellation);
}
continue;
}
string fileName = BackupFileName(root, file.FullName);
long transferred = 0;
long downloaded = 0;
bool committed = false;
string tempFile = fileName + "." + Guid.NewGuid().ToString("N") + ".__TEMP__";
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
try
{
await using (FileStream stream = OpenLocalTransferStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
var progress = new TransferProgress(delta =>
{
Interlocked.Add(ref transferred, delta);
Interlocked.Add(ref bytesDownloaded, delta);
});
if (entry.DataStream != null)
{
await entry.DataStream.CopyToAsync(stream, transferBufferSize, cancellation);
}
await stream.FlushAsync(cancellation);
progress.Report((ulong)stream.Length);
downloaded = stream.Length;
}
if (downloaded != file.Length)
{
Diag.Write($"size changed during download {file.FullName} listed={file.Length} got={downloaded}");
}
File.SetLastWriteTimeUtc(tempFile, file.LastWriteTimeUtc);
File.Move(tempFile, fileName, overwrite: true);
committed = true;
remaining.Remove(file.FullName);
extracted += downloaded;
count++;
if (count == 1 || count % 50 == 0)
{
Diag.Write($"tar extracted {count} files (latest {file.FullName} {BytesToString(downloaded)})");
}
}
finally
{
if (!committed && transferred != 0)
{
Interlocked.Add(ref bytesDownloaded, -transferred);
}
if (!committed)
{
try
{
File.Delete(tempFile);
}
catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException)
{
Console.WriteLine("Unable to remove temporary file {0}: {1}", tempFile, ex.Message);
}
}
}
}
return extracted;
}
private static async Task WriteTarFileListAsync(SshCommand cmd, IReadOnlyList<BackupEntry> files,
CancellationToken cancellation)
{
await using Stream input = cmd.CreateInputStream();
foreach (BackupEntry file in files)
{
cancellation.ThrowIfCancellationRequested();
await input.WriteAsync(Encoding.UTF8.GetBytes(file.FullName), cancellation);
await input.WriteAsync(tarNameNul, cancellation);
}
}
private static async Task<string> ReadCommandErrorAsync(SshCommand cmd, CancellationToken cancellation)
{
using StreamReader reader = new StreamReader(cmd.ExtendedOutputStream, Encoding.UTF8,
detectEncodingFromByteOrderMarks: false, bufferSize: 4096, leaveOpen: true);
return await reader.ReadToEndAsync(cancellation);
}
private static async Task<(List<BackupEntry> leftover, long extracted)> TryDownloadChangedFilesViaTarAsync(
HostEntry host, string root, IReadOnlyList<BackupEntry> files, SshClient ssh, CancellationToken cancellation)
{
if (files.Count == 0) return (new List<BackupEntry>(), 0);
var byName = new Dictionary<string, BackupEntry>(files.Count, StringComparer.Ordinal);
foreach (BackupEntry file in files) byName.TryAdd(file.FullName, file);
var remaining = new HashSet<string>(byName.Keys, StringComparer.Ordinal);
long listed = 0;
foreach (BackupEntry file in files) listed += file.Length;
string op = Diag.Begin($"{host} tar {files.Count} files {BytesToString(listed)}");
try
{
using SshCommand cmd = ssh.CreateCommand(
"tar --null --absolute-names --ignore-failed-read -T - -cf -");
cmd.CommandTimeout = commandTimeout;
Task executeTask = cmd.ExecuteAsync(cancellation);
Task stdinTask = WriteTarFileListAsync(cmd, files, cancellation);
Task<string> stderrTask = ReadCommandErrorAsync(cmd, cancellation);
long extracted = await ExtractTarBackupAsync(root, byName, remaining, cmd.OutputStream, cancellation);
try
{
await stdinTask;
}
catch (Exception ex)
{
Diag.Write($"{host} tar stdin {ex.GetType().Name}: {ex.Message}");
}
try
{
await executeTask;
}
catch (Exception ex) when (extracted != 0)
{
Diag.Write($"{host} tar command {ex.GetType().Name}: {ex.Message}");
}
string stderr = string.Empty;
try
{
stderr = await stderrTask;
}
catch (Exception ex)
{
Diag.Write($"{host} tar stderr {ex.GetType().Name}: {ex.Message}");
}
if (stderr.Length > 2048) stderr = stderr[..2048] + "...";
var leftover = files.Where(file => remaining.Contains(file.FullName)).ToList();
Diag.End(op, $"exit={cmd.ExitStatus} extracted={files.Count - leftover.Count} left={leftover.Count}" +
(stderr.Length == 0 ? "" : " " + stderr.Trim()));
return (leftover, extracted);
}
catch (Exception ex)
{
Diag.Fail(op, ex);
long extracted = 0;
foreach (BackupEntry file in files)
{
if (!remaining.Contains(file.FullName)) extracted += file.Length;
}
return (files.Where(file => remaining.Contains(file.FullName)).ToList(), extracted);
}
}
private static async Task<List<BackupEntry>> TryFindBackupFilesAsync(HostEntry host, string path,
SshClient ssh, CancellationToken cancellation)
{
string[] roots = path.Split('|').Select(s => s.Trim()).Where(s => s.Length != 0).ToArray();
if (roots.Length == 0) return new List<BackupEntry>();
string command = "find " + string.Join(" ", roots.Select(QuoteUnix)) +
" -name '.*' -type d -prune -o -type f -printf '%T@\\t%s\\t%p\\n'";
string op = Diag.Begin($"{host} find {path}");
try
{
using SshCommand cmd = ssh.CreateCommand(command);
cmd.CommandTimeout = commandTimeout;
await cmd.ExecuteAsync(cancellation);
string error = cmd.Error ?? string.Empty;
if (error.Contains("unknown predicate", StringComparison.OrdinalIgnoreCase) ||
error.Contains("illegal option", StringComparison.OrdinalIgnoreCase) ||
error.Contains("unrecognized", StringComparison.OrdinalIgnoreCase))
{
Diag.End(op, "unsupported find, fallback to SFTP");
return null;
}
var files = new List<BackupEntry>();
using StringReader reader = new StringReader(cmd.Result ?? string.Empty);
for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
{
int tab1 = line.IndexOf('\t');
int tab2 = tab1 < 0 ? -1 : line.IndexOf('\t', tab1 + 1);
if (tab2 < 0) continue;
if (!double.TryParse(line.AsSpan(0, tab1), NumberStyles.Float, CultureInfo.InvariantCulture, out double unix)) continue;
if (!long.TryParse(line.AsSpan(tab1 + 1, tab2 - tab1 - 1), NumberStyles.Integer, CultureInfo.InvariantCulture, out long size)) continue;
string fullName = line[(tab2 + 1)..];
if (fullName.Length == 0) continue;
if (host.IgnoreRegex != null && host.IgnoreRegex.IsMatch(fullName)) continue;
string name = fullName[(fullName.LastIndexOf('/') + 1)..];
files.Add(new BackupEntry(fullName, name, true, false, size, DateTime.UnixEpoch.AddSeconds(unix)));
}
Diag.End(op, $"{files.Count} files exit={cmd.ExitStatus}");
return files;
}
catch (Exception ex)
{
Diag.Fail(op, ex);
return null;
}
}
private static async IAsyncEnumerable<BackupEntry> EnumerateBackupFilesAsync(HostEntry host, string path,
ISftpClient client, TextWriter log, [EnumeratorCancellation] CancellationToken cancellation)
{
foreach (string root in path.Split('|').Select(s => s.Trim()).Where(s => s.Length != 0))
{
cancellation.ThrowIfCancellationRequested();
var rootEntry = await ReadBackupRootAsync(client, root, log, cancellation);
if (rootEntry == null) continue;
var pending = new Stack<BackupEntry>();
pending.Push(rootEntry);
while (pending.Count != 0)
{
cancellation.ThrowIfCancellationRequested();
var entry = pending.Pop();
if (entry.IsRegularFile)
{
yield return entry;
}
else if (entry.IsDirectory)
{
foreach (var child in await ReadBackupEntriesAsync(client, entry.FullName, log, cancellation))
{
if ((child.IsRegularFile || (child.IsDirectory && !child.Name.StartsWith("."))) &&
(host.IgnoreRegex == null || !host.IgnoreRegex.IsMatch(child.FullName)))
{
pending.Push(child);
}
}
}
}
}
}
internal static async Task<long> BackupFolderAsync(HostEntry host, string root, string path,
ISftpClient client, TextWriter log, Func<Task<ISftpClient>> createDownloadClient = null, int workers = 4,
SshClient ssh = null)
{
if (workers < 1 || workers > 16) throw new ArgumentOutOfRangeException(nameof(workers));
long size = 0;
int queued = 0;
int skipped = 0;
using var cancellation = new CancellationTokenSource();
var seen = new HashSet<string>(StringComparer.Ordinal);
string scanOp = Diag.Begin($"{host} backup {path} workers={workers}");
List<BackupEntry> foundList = null;
if (ssh != null && !host.IsWindows)
{
foundList = await TryFindBackupFilesAsync(host, path, ssh, cancellation.Token);
if (foundList == null)
{
Diag.Write($"{host} falling back to SFTP directory walking");
}
}
async IAsyncEnumerable<BackupEntry> EnumerateAsync()
{
if (foundList != null)
{
foreach (BackupEntry file in foundList) yield return file;
yield break;
}
await foreach (BackupEntry file in EnumerateBackupFilesAsync(host, path, client, log, cancellation.Token))
{
yield return file;
}
}
async IAsyncEnumerable<BackupEntry> ChangedFilesAsync()
{
await foreach (var file in EnumerateAsync())
{
if (!seen.Add(file.FullName)) continue;
string localFile = BackupFileName(root, file.FullName);
if (File.Exists(localFile) && file.LastWriteTimeUtc <= File.GetLastWriteTimeUtc(localFile))
{
Interlocked.Add(ref bytesSkipped, file.Length);
Interlocked.Add(ref size, file.Length);
int n = Interlocked.Increment(ref skipped);
if (n == 1 || n % 200 == 0)
{
Diag.Write($"{host} skipped {n} files (latest {file.FullName})");
}
}
else
{
yield return file;
}
}
}
async IAsyncEnumerable<BackupEntry> RemainingAsync(List<BackupEntry> leftover)
{
foreach (BackupEntry file in leftover) yield return file;
}
async Task DownloadViaSftpAsync(IAsyncEnumerable<BackupEntry> source, bool countFiles)
{
if (createDownloadClient == null || workers == 1)
{
await foreach (var file in source)
{
if (countFiles)
{
int n = Interlocked.Increment(ref queued);
if (n == 1 || n % 50 == 0)
{
Diag.Write($"{host} queued {n} downloads (latest {file.FullName} {BytesToString(file.Length)})");
}
}
size += await BackupFileAsync(root, file, client, cancellation.Token);
}
return;
}