Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 12 additions & 52 deletions FtpBackupRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,12 @@ public async Task RunJobAsync(
return;
}

var currentRoot = Path.Combine(
job.LocalPath,
options.CurrentSubdirectoryName);
var historyRoot = Path.Combine(
job.LocalPath,
options.HistorySubdirectoryName);

Directory.CreateDirectory(currentRoot);
Directory.CreateDirectory(historyRoot);
var tempDir = Path.Combine(job.LocalPath, "temp", job.Host);
Directory.CreateDirectory(tempDir);

var archiveDir = Path.Combine(job.LocalPath, job.Host);
Directory.CreateDirectory(archiveDir);


using var client = new FtpClient(job.Host, job.Username, job.Password, job.Port);

Expand Down Expand Up @@ -101,71 +98,34 @@ public async Task RunJobAsync(
remotePath);

var results = client.DownloadDirectory(
currentRoot,
tempDir,
remotePath,
FtpFolderSyncMode.Mirror,
FtpLocalExists.Overwrite,
FtpVerify.None);

LogResults(job.Name, results);

var snapshotName = DateTimeOffset.Now
.ToString("yyyyMMdd_HHmmss", CultureInfo.InvariantCulture);
var snapshotPath = Path.Combine(historyRoot, snapshotName);

logger.LogInformation(
"Creating history snapshot for backup '{name}' at {path}.",
job.Name,
snapshotPath);


CopyDirectory(currentRoot, snapshotPath, completionToken);
CleanupHistory(historyRoot, job, options);

string currentDir = Path.Combine(job.LocalPath, "current");
Directory.CreateDirectory(currentDir);

CopyDirectory(snapshotPath, currentDir, completionToken);

string archiveDir = Path.Combine(job.LocalPath, "archives", job.Name);
Directory.CreateDirectory(archiveDir);

string zipPath = Path.Combine(archiveDir, DateTime.Now.ToString("yyyy-MM-dd") + ".zip");

if (File.Exists(zipPath))
{
File.Delete(zipPath);
}

ZipFile.CreateFromDirectory(currentDir, zipPath, CompressionLevel.Optimal, includeBaseDirectory: false);
ZipFile.CreateFromDirectory(tempDir, zipPath, CompressionLevel.Optimal, includeBaseDirectory: false);

foreach (var file in Directory.GetFiles(archiveDir, "*.zip"))
{
var creationDate = File.GetCreationTime(file);
if ((DateTime.Now - creationDate).TotalDays > job.RetentionDays)
File.Delete(file);
}

foreach (var file in Directory.GetFiles(currentDir))
File.Delete(file);

foreach (var dir in Directory.GetDirectories(currentDir))
Directory.Delete(dir, recursive: true);

using var operationCts = new CancellationTokenSource(TimeSpan.FromMinutes(job.OperationTimeoutMinutes));
using var opLinkedCts = CancellationTokenSource.CreateLinkedTokenSource(completionToken, operationCts.Token);

string host = job.Host;
string username = job.Username;
string password = job.Password;
var ftpClient = new FtpClient();
ftpClient.Host = host;
ftpClient.Credentials = new NetworkCredential(username, password);
ftpClient.Connect();

string localPath = Path.Combine(job.LocalPath, "current");
ftpClient.DownloadDirectory(remotePath, localPath);
ftpClient.Disconnect();
if (Directory.Exists(tempDir))
{
Directory.Delete(tempDir, recursive: true);
}
}
catch (OperationCanceledException)
{
Expand Down
2 changes: 1 addition & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
Console.WriteLine("I'm starting a test of downloading files from FTP");

await ftpRunner.RunJobAsync(backupJob, backupOptions, CancellationToken.None);
Console.WriteLine("FTP test completed! Files should be in /");
Console.WriteLine("FTP test completed!" );
}
catch (Exception ex)
{
Expand Down
5 changes: 2 additions & 3 deletions appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
"HistoryCopies": 5,
"DefaultTimeoutMinutes": 60,
"RetentionDays": 7,
"CurrentSubdirectoryName": "current",
"HistorySubdirectoryName": "_history",
"Backups": [
{
"Name": "EXAMPLE_NAME",
Expand All @@ -32,7 +30,8 @@
"Encryption": "Explicit",
"Passive": true,
"AllowInvalidCertificate": true,
"TimeoutMinutes": 60,
"OperationTimeoutMinutes": 10,
"CompletionTimeoutMinutes": 180,
"HistoryCopies": 5
}
]
Expand Down