private void SyncFTPBackups() { var ftpSettings = backupSettings.FTPSettings; log.Info("Starting FTP: {0}", ftpSettings.FTPServerAddress); var ftpPassword = SecurityHelpers.DecodeSecret(ftpSettings.FTPPassword); var ftpClient = new FTPClient(ftpSettings.FTPServerAddress, ftpSettings.FTPUser, ftpPassword, port: ftpSettings.FTPPort, useSSL: ftpSettings.FTPUseSSL, attempts: ftpSettings.RetryAttempts, attemptWaitSeconds: ftpSettings.RetryDelaySeconds); var localBackupDirectory = new DirectoryInfo(backupSettings.LocalBackupFolder); var localBackupFiles = new List <FileInfo>(); localBackupFiles.AddRange(localBackupDirectory.GetFiles("I-*.7z")); localBackupFiles.AddRange(localBackupDirectory.GetFiles("F-*.7z")); var list = ftpClient.GetList(); if (list == null) { throw new ApplicationException("Could not retrieve list from FTP site."); } var remoteBackupFileNames = list.Where(rbf => rbf.EndsWith(".7z", StringComparison.OrdinalIgnoreCase) && (rbf.StartsWith("I-", StringComparison.OrdinalIgnoreCase) || rbf.StartsWith("F-", StringComparison.OrdinalIgnoreCase)) ).ToList(); foreach (string remoteBackupFileName in remoteBackupFileNames) { bool existsLocal = localBackupFiles.Exists(lbf => string.Equals(lbf.Name, remoteBackupFileName, StringComparison.OrdinalIgnoreCase)); if (!existsLocal) { log.Info("Deleting remote: {0}", remoteBackupFileName); if (!ftpClient.DeleteFile(remoteBackupFileName)) { throw new ApplicationException("Delete failed. " + ftpClient.LastStatusDescription); } } } foreach (var localBackupFile in localBackupFiles) { string remoteFileName = remoteBackupFileNames.Find(rbfn => string.Equals(rbfn, localBackupFile.Name, StringComparison.OrdinalIgnoreCase)); bool shouldUpload = false; if (string.IsNullOrEmpty(remoteFileName)) { shouldUpload = true; log.Info("File: {0} ({1}) does not exist on FTP site. Starting upload.", localBackupFile.Name, StaticHelpers.FormatFileSize(localBackupFile.Length)); } else { //Check the sizes if (ftpSettings.FTPVerifySizes) { long?ftpFileSize = ftpClient.GetFileSize(remoteFileName); shouldUpload = localBackupFile.Length != ftpFileSize; if (shouldUpload) { log.Warn("File: {0} ({1}) size DOES NOT match file size on FTP site ({2}).", localBackupFile.Name, StaticHelpers.FormatFileSize(localBackupFile.Length), StaticHelpers.FormatFileSize(ftpFileSize)); } else { log.Info("File: {0} ({1}) size matches file size on FTP site ({2}).", localBackupFile.Name, StaticHelpers.FormatFileSize(localBackupFile.Length), StaticHelpers.FormatFileSize(ftpFileSize)); } } } if (shouldUpload) { Stopwatch sw = Stopwatch.StartNew(); if (!ftpClient.UploadFile(localBackupFile)) { throw new ApplicationException("Upload failed. " + ftpClient.LastStatusDescription); } sw.Stop(); double bytesSec = localBackupFile.Length / sw.Elapsed.TotalSeconds; log.Info("Upload of:{0} ({1}) completed in:{2} ({3} / second)", localBackupFile.Name, StaticHelpers.FormatFileSize(localBackupFile.Length), sw.Elapsed, StaticHelpers.FormatFileSize(bytesSec)); } } }
private void SyncFTPBackups() { var ftpSettings = backupSettings.FTPSettings; log.Info("Starting FTP: {0}", ftpSettings.FTPServerAddress); var ftpPassword = SecurityHelpers.DecodeSecret(ftpSettings.FTPPassword); var ftpClient = new FTPClient(ftpSettings.FTPServerAddress, ftpSettings.FTPUser, ftpPassword, port: ftpSettings.FTPPort, useSSL: ftpSettings.FTPUseSSL, attempts: ftpSettings.RetryAttempts, attemptWaitSeconds: ftpSettings.RetryDelaySeconds); var localBackupDirectory = new DirectoryInfo(backupSettings.LocalBackupFolder); var localBackupFiles = new List<FileInfo>(); localBackupFiles.AddRange(localBackupDirectory.GetFiles("I-*.7z")); localBackupFiles.AddRange(localBackupDirectory.GetFiles("F-*.7z")); var list = ftpClient.GetList(); if(list == null) throw new ApplicationException("Could not retrieve list from FTP site."); var remoteBackupFileNames = list.Where(rbf => rbf.EndsWith(".7z", StringComparison.OrdinalIgnoreCase) && (rbf.StartsWith("I-", StringComparison.OrdinalIgnoreCase) || rbf.StartsWith("F-", StringComparison.OrdinalIgnoreCase)) ).ToList(); foreach(string remoteBackupFileName in remoteBackupFileNames) { bool existsLocal = localBackupFiles.Exists(lbf => string.Equals(lbf.Name, remoteBackupFileName, StringComparison.OrdinalIgnoreCase)); if(!existsLocal) { log.Info("Deleting remote: {0}", remoteBackupFileName); if (!ftpClient.DeleteFile(remoteBackupFileName)) { throw new ApplicationException("Delete failed. " + ftpClient.LastStatusDescription); } } } foreach(var localBackupFile in localBackupFiles) { string remoteFileName = remoteBackupFileNames.Find(rbfn => string.Equals(rbfn, localBackupFile.Name, StringComparison.OrdinalIgnoreCase)); bool shouldUpload = false; if (string.IsNullOrEmpty(remoteFileName)) { shouldUpload = true; log.Info("File: {0} ({1}) does not exist on FTP site. Starting upload.", localBackupFile.Name, StaticHelpers.FormatFileSize(localBackupFile.Length)); } else { //Check the sizes if (ftpSettings.FTPVerifySizes) { long? ftpFileSize = ftpClient.GetFileSize(remoteFileName); shouldUpload = localBackupFile.Length != ftpFileSize; if (shouldUpload) { log.Warn("File: {0} ({1}) size DOES NOT match file size on FTP site ({2}).", localBackupFile.Name, StaticHelpers.FormatFileSize(localBackupFile.Length), StaticHelpers.FormatFileSize(ftpFileSize)); }else { log.Info("File: {0} ({1}) size matches file size on FTP site ({2}).", localBackupFile.Name, StaticHelpers.FormatFileSize(localBackupFile.Length), StaticHelpers.FormatFileSize(ftpFileSize)); } } } if (shouldUpload) { Stopwatch sw = Stopwatch.StartNew(); if (!ftpClient.UploadFile(localBackupFile)) { throw new ApplicationException("Upload failed. " + ftpClient.LastStatusDescription); } sw.Stop(); double bytesSec = localBackupFile.Length/sw.Elapsed.TotalSeconds; log.Info("Upload of:{0} ({1}) completed in:{2} ({3} / second)", localBackupFile.Name, StaticHelpers.FormatFileSize(localBackupFile.Length), sw.Elapsed, StaticHelpers.FormatFileSize(bytesSec)); } } }