Skip to content

Commit 2acab8a

Browse files
committed
Allow ignore files by regex
1 parent 255b798 commit 2acab8a

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

AutoSSHApp.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ private class HostEntry
2828
public string Host { get; set; }
2929
public string Name { get; set; }
3030
public bool IsWindows { get; set; }
31+
public Regex IgnoreRegex { get; set; }
3132

3233
public override string ToString()
3334
{
@@ -320,7 +321,7 @@ private static long BackupFile(string root, string remotePath, SftpClient client
320321
return 0;
321322
}
322323

323-
private static long BackupFolder(string root, string path, SftpClient client)
324+
private static long BackupFolder(HostEntry host, string root, string path, SftpClient client)
324325
{
325326
long size = 0;
326327
foreach (string fileOrFolder in path.Split('|').Select(s => s.Trim()).Where(s => s.Length != 0))
@@ -341,13 +342,13 @@ private static long BackupFolder(string root, string path, SftpClient client)
341342
else
342343
{
343344
SftpFile[] files = client.ListDirectory(fileOrFolder).Where(f => f.IsRegularFile || (f.IsDirectory && !f.Name.StartsWith("."))).ToArray();
344-
Parallel.ForEach(files.Where(f => f.IsRegularFile), parallelOptions, (_file) =>
345+
Parallel.ForEach(files.Where(f => f.IsRegularFile && (host.IgnoreRegex == null || !host.IgnoreRegex.IsMatch(f.FullName))), parallelOptions, (_file) =>
345346
{
346347
Interlocked.Add(ref size, BackupFile(root, _file.FullName, client));
347348
});
348349
Parallel.ForEach(files.Where(f => f.IsDirectory), parallelOptions2, (folder) =>
349350
{
350-
Interlocked.Add(ref size, BackupFolder(root, folder.FullName, client));
351+
Interlocked.Add(ref size, BackupFolder(host, root, folder.FullName, client));
351352
});
352353
}
353354
}
@@ -400,7 +401,11 @@ private static void ClientLoop(string root, HostEntry host, List<string> command
400401
{
401402
if (command.StartsWith("$backup ", StringComparison.OrdinalIgnoreCase))
402403
{
403-
backupSize += BackupFolder(backupPath, command.Substring(8), sftpClient);
404+
backupSize += BackupFolder(host, backupPath, command.Substring(8), sftpClient);
405+
}
406+
else if (command.StartsWith("$ignore ", StringComparison.OrdinalIgnoreCase))
407+
{
408+
host.IgnoreRegex = new Regex(command.Substring(8), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
404409
}
405410
}
406411
else

Readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ Command file format is as follows:
2424
$host * *
2525
# update packages
2626
apt-get -q -y update
27+
28+
# ignore files by case insensitive regex, in this case any file called big_files[0-9]+\.bin
29+
$ignore big_file[0-9]+\.bin
30+
2731
# backup files and folders, always recursive, separate multiple with |
2832
$backup /var/www|/etc/apache2/apache2.conf|/etc/apache2/sites-enabled
2933

0 commit comments

Comments
 (0)