// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace Spludlow { public class Restore { // dir restore public static void DirectoryFromFTP(string ftpKey, string ftpDirectory, string archivePassword, string backupName, string targetDirectory) { Spludlow.Net.Ftp ftp = new Net.Ftp(ftpKey); if (archivePassword != null && archivePassword.StartsWith("@") == true) archivePassword = Spludlow.Credentials.GetPassword(archivePassword.Substring(1)); int endLength = backupName.Length + "_2018-01-10-18-29-26-809.7z".Length; string[] ftpFilenames = ListFTP(ftp, endLength, ftpDirectory, backupName); string filename = ftpDirectory + @"/" + ftpFilenames[ftpFilenames.Length - 1]; using (Spludlow.TempDirectory tempDir = new TempDirectory()) { string archiveFilename = tempDir.Path + @"\ARCHIVE.7z"; ftp.DownloadFile(filename, archiveFilename); string outDirectory = tempDir.Path + @"\OUT"; Directory.CreateDirectory(outDirectory); Archive.Extract(archiveFilename, outDirectory, archivePassword); // Shuffle down direcories if they are single folders with no files string[] childDirs; while (((childDirs = Directory.GetDirectories(outDirectory)).Length == 1) && (Directory.GetFiles(outDirectory).Length == 0)) { outDirectory = childDirs[0]; } string backupDirectory = targetDirectory + "_" + Spludlow.Text.TimeStamp(); Directory.Move(targetDirectory, backupDirectory); Spludlow.Io.Dirs.Copy(outDirectory, targetDirectory); Spludlow.Log.Finish("Restore DirectoryFromFTP: " + filename + " -> " + targetDirectory); } } // has to be ".BAK" for length public static void DatabaseFromFTP(string ftpKey, string ftpDirectory, string archivePassword, string backupName, string connectionString, string targetDatabaseName, string databaseTempDir, string ownerUser) { Spludlow.Net.Ftp ftp = new Net.Ftp(ftpKey); if (archivePassword != null && archivePassword.StartsWith("@") == true) archivePassword = Spludlow.Credentials.GetPassword(archivePassword.Substring(1)); if (ownerUser != null && ownerUser == "@") ownerUser = Environment.UserDomainName + @"\" + Environment.UserName; int endLength = backupName.Length + "_2018-01-10-18-29-26-809.BAK.7z".Length; string[] ftpFilenames = ListFTP(ftp, endLength, ftpDirectory, backupName); string filename = ftpDirectory + @"/" + ftpFilenames[ftpFilenames.Length - 1]; string backupFilename = null; try { using (Spludlow.TempDirectory tempDir = new TempDirectory()) { string archiveFilename = tempDir.Path + @"\ARCHIVE.7z"; ftp.DownloadFile(filename, archiveFilename); string outDirectory = tempDir.Path + @"\OUT"; Directory.CreateDirectory(outDirectory); Archive.Extract(archiveFilename, outDirectory, archivePassword); string[] filenames = Directory.GetFiles(outDirectory); if (filenames.Length != 1) throw new ApplicationException("Restore DatabaseFromFTP; Did not extract 1 file"); backupFilename = databaseTempDir + @"\" + Path.GetFileName(filenames[0]); File.Copy(filenames[0], backupFilename); } Spludlow.Data.Database.Restore(connectionString, targetDatabaseName, backupFilename, ownerUser); } finally { if (backupFilename != null && File.Exists(backupFilename) == true) File.Delete(backupFilename); } } private static string[] ListFTP(Spludlow.Net.Ftp ftp, int endLength, string ftpDirectory, string backupName) { List matches = new List(); foreach (string ftpFilename in ftp.ListDirectory(ftpDirectory)) { if (ftpFilename.Length == endLength && ftpFilename.StartsWith(backupName) == true) matches.Add(ftpFilename); } if (matches.Count == 0) throw new ApplicationException("Restore DatabaseFromFTP; Nothing found on FTP"); matches.Sort(); return matches.ToArray(); } } }