private void DeleteDirectory(string relativeDirectory) { //Get files var files = SftpManager.GetList(host, relativeDirectory, Username, Password); //Loop through files foreach (var fileStruct in files) { //If directory, need to delete sub directories if (fileStruct.IsDirectory) { DeleteDirectory(relativeDirectory + "/" + fileStruct.Name); } else { SftpManager.DeleteRemoteFile(host, fileStruct.Name, Username, Password); } } SftpManager.DeleteRemoteDirectory(host, relativeDirectory, Username, Password); }
private void CleanUpBackups() { //Get files in folder var files = SftpManager.GetList(host, _backupFolder, Username, Password); //Filename structure var exp = new Regex(@"^build_\d\d\d\d_\d\d_\d\d_\d\d$"); //Loop through directory foreach (var fileStruct in files) { //If a directory and matches filename structure if (fileStruct.IsDirectory && exp.IsMatch(fileStruct.Name)) { //Get year var year = int.Parse(fileStruct.Name.Substring(6, 4)); //Get month var month = int.Parse(fileStruct.Name.Substring(11, 2)); //Get day var day = int.Parse(fileStruct.Name.Substring(14, 2)); //Get version var version = int.Parse(fileStruct.Name.Substring(17, 2)); //Get date from file name var date = new DateTime(year, month, day); //If past expiration date if (date < _deleteBeforeDate) { //Remove file from server DeleteDirectory(_backupFolder + fileStruct.Name); } } } }
public static void BuildBackupFile() { string localPath = Path.GetTempPath() + @"\BackupFolders.txt"; StreamWriter sw; sw = File.CreateText(localPath); // //Filename structure var exp = new Regex(@"^build_\d\d\d\d_\d\d_\d\d_\d\d$"); // //Daily Builds var files = SftpManager.GetList(host, "files.flatredball.com/content/FrbXnaTemplates/DailyBackups/", Username, Password); var folderNames = (from fileStruct in files where fileStruct.IsDirectory && exp.IsMatch(fileStruct.Name) select fileStruct.Name).ToList(); folderNames.Reverse(); foreach (var folderName in folderNames) { //Get year var year = int.Parse(folderName.Substring(6, 4)); //Get month var month = int.Parse(folderName.Substring(11, 2)); //Get day var day = int.Parse(folderName.Substring(14, 2)); //Get version var version = int.Parse(folderName.Substring(17, 2)); sw.WriteLine("Daily Build - " + month + @"/" + day + @"/" + year + " " + version + ",DailyBackups/" + folderName + "/"); } //Weekly Builds files = SftpManager.GetList(host, "files.flatredball.com/content/FrbXnaTemplates/WeeklyBackups/", Username, Password); folderNames = (from fileStruct in files where fileStruct.IsDirectory && exp.IsMatch(fileStruct.Name) select fileStruct.Name).ToList(); folderNames.Reverse(); foreach (var folderName in folderNames) { //Get year var year = int.Parse(folderName.Substring(6, 4)); //Get month var month = int.Parse(folderName.Substring(11, 2)); //Get day var day = int.Parse(folderName.Substring(14, 2)); //Get version var version = int.Parse(folderName.Substring(17, 2)); sw.WriteLine("Weekly Build - " + month + @"/" + day + @"/" + year + " " + version + ",WeeklyBackups/" + folderName + "/"); } //Monthly Builds files = SftpManager.GetList( host, "files.flatredball.com/content/FrbXnaTemplates/MonthlyBackups/", Username, Password); folderNames = (from fileStruct in files where fileStruct.IsDirectory && exp.IsMatch(fileStruct.Name) select fileStruct.Name).ToList(); folderNames.Reverse(); foreach (var folderName in folderNames) { //Get year var year = int.Parse(folderName.Substring(6, 4)); //Get month var month = int.Parse(folderName.Substring(11, 2)); //Get day var day = int.Parse(folderName.Substring(14, 2)); //Get version var version = int.Parse(folderName.Substring(17, 2)); sw.WriteLine("Monthly Build - " + month + @"/" + day + @"/" + year + " " + version + ",MonthlyBackups/" + folderName + "/"); } sw.Close(); //SftpManager.UploadFile(localPath, host, _backupFile, Username, Password); }
private bool FolderExists(string fileName) { var files = SftpManager.GetList(host, _backupFolder, Username, Password); return(files.Any(fileStruct => "sftp://files.flatredball.com/" + _backupFolder + fileStruct.Name == fileName)); }