diff --git a/FtpBackupRunner.cs b/FtpBackupRunner.cs index f6072bf..303598d 100644 --- a/FtpBackupRunner.cs +++ b/FtpBackupRunner.cs @@ -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); @@ -101,7 +98,7 @@ public async Task RunJobAsync( remotePath); var results = client.DownloadDirectory( - currentRoot, + tempDir, remotePath, FtpFolderSyncMode.Mirror, FtpLocalExists.Overwrite, @@ -109,27 +106,6 @@ public async Task RunJobAsync( 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)) @@ -137,7 +113,7 @@ public async Task RunJobAsync( 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")) { @@ -145,27 +121,11 @@ public async Task RunJobAsync( 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) { diff --git a/Program.cs b/Program.cs index ad772c0..5659607 100644 --- a/Program.cs +++ b/Program.cs @@ -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) { diff --git a/appsettings.json b/appsettings.json index 93b4ddb..da7d60b 100644 --- a/appsettings.json +++ b/appsettings.json @@ -18,8 +18,6 @@ "HistoryCopies": 5, "DefaultTimeoutMinutes": 60, "RetentionDays": 7, - "CurrentSubdirectoryName": "current", - "HistorySubdirectoryName": "_history", "Backups": [ { "Name": "EXAMPLE_NAME", @@ -32,7 +30,8 @@ "Encryption": "Explicit", "Passive": true, "AllowInvalidCertificate": true, - "TimeoutMinutes": 60, + "OperationTimeoutMinutes": 10, + "CompletionTimeoutMinutes": 180, "HistoryCopies": 5 } ]