void btnTestSettings_Click(object sender, EventArgs e) { if (gui == null) { MessageBox.Show("Strange... GUI could not be found..."); return; } string tempFile = null; try { FTPConfig conf = gui.Save(); if (MessageBox.Show(@"A small text file will be uploaded to the specified server, to the specified directory. If the upload succeeds, the file will not be deleted by this test. Any directories specified in the remote path will be created. Are you sure you want to continue?", "Continue?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes) { return; } FTPTarget t = new FTPTarget { config = conf }; DirectoryInfo temp = new DirectoryInfo(Configuration.DataDirectory.FullName); tempFile = temp.FullName + "\\FTPUploadTest.txt"; File.AppendAllText(tempFile, "Testfile for BUMmer FTP Upload"); t.Store(new FileInfo(tempFile), ""); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (tempFile != null && File.Exists(tempFile)) { File.Delete(tempFile); } } }
public static void SftpFileUpload(FTPConfig config) { ConnectionInfo connectionInfo = new ConnectionInfo(config.IP, config.Port, config.User, new PasswordAuthenticationMethod(config.User, config.Password)); using (SftpClient sftp = new SftpClient(connectionInfo)) { sftp.Connect(); sftp.ChangeDirectory(config.Directory); FileInfo fi = new FileInfo(config.FilePath); using (var uplfileStream = File.OpenRead(config.FilePath)) { if (config.DeleteIfExists) { try { Renci.SshNet.Sftp.SftpFile file = sftp.Get(fi.Name); if (file.HasValue()) { sftp.DeleteFile(file.FullName); } } catch { } } sftp.UploadFile(uplfileStream, fi.Name, true); } sftp.Disconnect(); } }
public void InitiateConfiguration(Control container, string configuration) { config = FTPConfig.Load(configuration); gui = new FTPConfigSelector(config); gui.Dock = DockStyle.Fill; gui.btnTestSettings.Click += btnTestSettings_Click; container.Controls.Add(gui); }
/// <summary> /// 一键还原 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOneKeyRestore_Click(object sender, RoutedEventArgs e) { try { textBox_Log.Clear(); this.btnExcute_Sql.IsEnabled = false; ThreadPool.SetMaxThreads(50, 50); //Thread thread = new Thread(new ThreadStart(UpdataLogHandler)); //thread.Start(); FTPConfig fTPConfig = new FTPConfig() { DataBaseFtpIP = textBox_IP.Text, RCFtpIP = textBox_FtpIP.Text, RCFtpUserName = textBox_FtpUserName.Text, RCFtpPassWord = textBox_FtpPassWord.Password, EndTime = DateTimePicker_EndTime.SelectedDate }; //创建目录 FileHelper.CreateDicIfNoExist(OneKeyConst.LocalBakFilePath); FileHelper.CreateDicIfNoExist(OneKeyConst.LocalDiffFilePath); FileHelper.CreateDicIfNoExist(OneKeyConst.LocalTrnFilePath); //创建客户端 FtpClient dataBaseClient, rcClient; CreateClient(fTPConfig, out dataBaseClient, out rcClient); var RcFileList = rcClient.GetListing("/").OrderByDescending(d => d.Modified); string bakFileName = DownAndUpload(dataBaseClient, rcClient, "/BakFile", "RC_full_", RcFileList, fTPConfig)?.FirstOrDefault(); string diffFileName = DownAndUpload(dataBaseClient, rcClient, "/DiffFile", "RC_diff_", RcFileList, fTPConfig)?.FirstOrDefault(); var trnFileList = DownAndUpload(dataBaseClient, rcClient, "/TrnFile", "RC_log_", RcFileList, fTPConfig); //用于检查线程池中的线程是否全部同步完成 rhw = ThreadPool.RegisterWaitForSingleObject(new AutoResetEvent(false), CheckThreadPool, null, 1000, false); //生成sql语句 string sql = GenerateSql(bakFileName, diffFileName, trnFileList); textBox_Sql.Text = sql; } catch (Exception ex) { this.ShowMessageAsync("错误信息", ex.Message); } }
private void LoadFtpConfig() { FTPConfig ftpConfig = GetFtpConfig(); bool flag = (ftpConfig != null) ? true : false; if (!flag) { ftpConfig = new FTPConfig() { FTPHost = "127.0.0.1", UserName = "******", Password = "******", FTPDefaultPath = @"/", SyncsTime = 24 * 60, LocalPath = @"C:\", ServerTimeZone = 7, LocalTimeZone = 7, ConnectionMode = 0, DeleteAfterSync = false, SyncOrderOnly = true, DeleteExpiredMonths = 4, SSLEncryptionMode = 0 }; } txtFTPHost.Text = ftpConfig.FTPHost; txtUserName.Text = ftpConfig.UserName; txtPassword.Text = ftpConfig.Password; txtFTPDefaultPath.Text = ftpConfig.FTPDefaultPath; txtPort.Text = ftpConfig.Port.ToString(); txtSyncsTime.Text = string.Format("{0}", ftpConfig.SyncsTime); txtLocalPath.Text = ftpConfig.LocalPath; // ssl cSSL.SelectedIndex = ftpConfig.SSLEncryptionMode; cConnectionmode.SelectedIndex = ftpConfig.ConnectionMode; cDeleteAfterSync.Checked = ftpConfig.DeleteAfterSync; cSyncCompleted.Checked = ftpConfig.SyncOrderOnly; nExpirationMonths.Value = ftpConfig.DeleteExpiredMonths; string message = flag ? string.Format("{0}", "Load configuration successful.") : string.Format("{0}", "Load configuration failure. Default configuration loaded."); UpdateInfo(string.Format("Message: {0}", message)); Application.DoEvents(); }
public static List <string> FtpListFiles(FTPConfig config) { FtpWebRequest request; List <string> items = new List <string>(); try { config.IP = config.IP.EndsWith(CT_Slach) ? config.IP : string.Concat(config.IP, CT_Slach); string uri = string.Concat(config.IP.StartsWith(CT_FtpPrefix) ? string.Empty : CT_FtpPrefix, config.IP); if (config.Directory.HasText()) { uri = string.Concat(uri, config.Directory); } request = WebRequest.Create(new Uri(uri)) as FtpWebRequest; request.Method = WebRequestMethods.Ftp.ListDirectory; request.UseBinary = true; request.UsePassive = true; request.KeepAlive = true; request.Credentials = new NetworkCredential(config.User, config.Password); request.EnableSsl = config.EnableSsl; request.UsePassive = config.UsePassive; using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) { using (StreamReader streamReader = new StreamReader(response.GetResponseStream())) { string line = streamReader.ReadLine(); while (!string.IsNullOrEmpty(line)) { items.Add(string.Concat(config.IP, line)); line = streamReader.ReadLine(); } } } } catch { } return(items); }
public static void ORMCreateTableOnInit(IDbConnection dbConn, string user, bool gainPermission = false) { dbConn.CreateTableIfNotExists <ABUserAuth>(); dbConn.CreateTableIfNotExists <FTPConfig>(); dbConn.CreateTableIfNotExists <FTPFileCheck>(); #region default config if (dbConn.Count <FTPConfig>() == 0) { FTPConfig c = new FTPConfig() { FTPDefaultPath = "/digilabs", FTPHost = "127.0.0.1", LocalPath = @"E:\ftpsync", LocalTimeZone = 7, ServerTimeZone = 8, Port = 0, Password = "******", UserName = "******", SyncsTime = 24, SSLEncryptionMode = 0, ConnectionMode = 0, DeleteAfterSync = false, DeleteExpiredMonths = 4, SyncOrderOnly = false }; dbConn.Insert <FTPConfig>(c); } #endregion }
private void CreateClient(FTPConfig fTPConfig, out FtpClient dataBaseClient, out FtpClient rcClient) { //10.1.6.175 // create an FTP client rcClient = new FtpClient(fTPConfig.RCFtpIP); // if you don't specify login credentials, we use the "anonymous" user account rcClient.Credentials = new NetworkCredential(fTPConfig.RCFtpUserName, fTPConfig.RCFtpPassWord); // begin connecting to the server rcClient.Connect(); //10.10.24.15 D:\bak的文件 // create an FTP client dataBaseClient = new FtpClient(fTPConfig.DataBaseFtpIP); // if you don't specify login credentials, we use the "anonymous" user account dataBaseClient.Credentials = new NetworkCredential(fTPConfig.FtpUserNameFor15, fTPConfig.FtpPassWordFor15); // begin connecting to the server dataBaseClient.Connect(); }
/// <summary> /// /// </summary> /// <param name="configuration"></param> /// <returns></returns> public static FTPConfig Load(string configuration) { if (string.IsNullOrEmpty(configuration)) { return(new FTPConfig()); } MemoryStream ms = null; FTPConfig ps = null; try { ms = new MemoryStream(configuration.ToByteArray()); ms.Seek(0, SeekOrigin.Begin); ps = (FTPConfig)Serializer.Deserialize(ms); } catch { } finally { if (ms != null) { ms.Close(); ms.Dispose(); } } return(ps ?? new FTPConfig()); }
public static void FtpFileDownLoad(FTPConfig config, string outpath) { if (!Directory.Exists(outpath)) { try { Directory.CreateDirectory(outpath); } catch { return; } } string[] parts = null; string fileName; using (WebClient client = new WebClient()) { client.Credentials = new NetworkCredential(config.User, config.Password); foreach (string item in config.FilesPath) { parts = item.Split(CT_Slach[0]); fileName = string.Concat(outpath, outpath.EndsWith(CT_BackSlach) ? string.Empty : CT_BackSlach, parts[parts.Length - 1]); if (File.Exists(fileName)) { continue; } client.DownloadFile(item.StartsWith(CT_FtpPrefix) ? item : string.Concat(CT_FtpPrefix, item), fileName); } } }
public static void FtpFileUpload(FTPConfig config) { string uri = string.Concat(config.IP.Contains(CT_FtpPrefix) ? string.Empty : CT_FtpPrefix, config.IP, config.IP.EndsWith(CT_Slach) ? string.Empty : CT_Slach); if (config.Directory.HasText()) { uri = string.Concat(uri, string.Concat(config.Directory, config.Directory.EndsWith(CT_Slach) ? string.Empty : CT_Slach)); } string fileFtpPath = string.Empty; FtpWebRequest req = null; Action <string> setreq = (method) => { req = (FtpWebRequest)WebRequest.Create(fileFtpPath); req.Credentials = new NetworkCredential(config.User, config.Password); req.Method = method; req.EnableSsl = config.EnableSsl; req.UsePassive = config.UsePassive; }; Action <string> upload = (path) => { if (!File.Exists(path)) { return; } FileInfo fi = new FileInfo(path); fileFtpPath = string.Concat(uri, fi.Name); setreq(WebRequestMethods.Ftp.DeleteFile); try { FtpWebResponse response = (FtpWebResponse)req.GetResponse(); response.Close(); } catch { } setreq(WebRequestMethods.Ftp.UploadFile); try { using (Stream fileStream = File.OpenRead(path)) { using (Stream ftpStream = req.GetRequestStream()) { byte[] buffer = new byte[10240]; int read; while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0) { ftpStream.Write(buffer, 0, read); } } } } catch (Exception ex) { throw ex; } }; if (config.FilesPath.HasItems()) { foreach (string item in config.FilesPath) { upload(item); } } if (!config.FilePath.IsNullOrEmpty()) { try { upload(config.FilePath); } catch (Exception ex) { SftpFileUpload(config); } } }
Task <ServiceOperationResponse> IServiceOperationsProvider.InvokeOperation(string operationId, InsensitiveDictionary <JToken> connectionParameters, ServiceOperationRequest serviceOperationRequest) { string path = string.Empty; string outputParam = string.Empty; string serverName = string.Empty; string userName = string.Empty; string password = string.Empty; bool useSSL = false; bool implicitMode = false; bool activeMode = false; bool useSelfSignedCert = false; bool useBinaryMode = false; ServiceOperationResponse resp = null; var opResp = Task.Run(() => { try { //serverName = GetBindingConnectionInformation(operationId, connectionParameters); var folderToken = serviceOperationRequest.Parameters.GetValueOrDefault("inputParam"); if (folderToken != null) { path = folderToken.ToString(); } if (folderToken == null || string.IsNullOrEmpty(path)) { throw new ApplicationException("Unable to obtain path"); } serverName = GetBindingConnectionParameter(operationId, connectionParameters, "servername"); userName = GetBindingConnectionParameter(operationId, connectionParameters, "username"); password = GetBindingConnectionParameter(operationId, connectionParameters, "password"); useSSL = System.Convert.ToBoolean(GetBindingConnectionParameter(operationId, connectionParameters, "usessl")); implicitMode = System.Convert.ToBoolean(GetBindingConnectionParameter(operationId, connectionParameters, "implicitmode")); activeMode = System.Convert.ToBoolean(GetBindingConnectionParameter(operationId, connectionParameters, "activemode")); useSelfSignedCert = System.Convert.ToBoolean(GetBindingConnectionParameter(operationId, connectionParameters, "useSelfsignedcert")); useBinaryMode = System.Convert.ToBoolean(GetBindingConnectionParameter(operationId, connectionParameters, "usebinarymode")); FTPConfig ftpConfig = new FTPConfig(serverName, userName, password, useSSL, implicitMode, useSelfSignedCert, activeMode, useBinaryMode); Connectors.FTPCore.FTP FTP = new Connectors.FTPCore.FTP(ftpConfig); switch (operationId) { case ServiceOperationGetFile: var file = FTP.FluentGetFile(path); JProperty fileProp = new JProperty("body", file.Result); resp = new ServiceOperationResponse(fileProp.Value, System.Net.HttpStatusCode.OK); break; case ServiceOperationUploadFile: string content; var con = serviceOperationRequest.Parameters.GetValueOrDefault("content"); if (con == null) { throw new ApplicationException("Unable to read content"); } content = con.ToString(); var output = FTP.FluentUploadFile(path, content); JProperty outProp = new JProperty("body", output.Result); resp = new ServiceOperationResponse(outProp.Value, System.Net.HttpStatusCode.OK); break; case ServiceOperationDeleteFile: var deleteOutput = FTP.FluentDeleteFile(path); JProperty deleteProp = new JProperty("body", deleteOutput.Result); resp = new ServiceOperationResponse(deleteProp.Value, System.Net.HttpStatusCode.OK); break; case ServiceOperationCopyFileToBlob: string storageConnectionString; var storageConn = serviceOperationRequest.Parameters.GetValueOrDefault("storageconnectionstring"); if (storageConn == null) { throw new ApplicationException("Unable to read Storage Connection String"); } storageConnectionString = storageConn.ToString(); string targetContainer; var targetContainerParam = serviceOperationRequest.Parameters.GetValueOrDefault("targetcontainer"); if (targetContainerParam == null) { throw new ApplicationException("Unable to read Target Container"); } targetContainer = targetContainerParam.ToString(); var copyFileOutput = FTP.FluentCopyFileToBlob(path, storageConnectionString, targetContainer); JProperty copyProp = new JProperty("body", copyFileOutput.Result); resp = new ServiceOperationResponse(copyProp.Value, System.Net.HttpStatusCode.OK); break; case ServiceOperationListFile: var fileList = FTP.FluentListFiles(path); var obj = fileList.Result; List <JObject> jobjects = new List <JObject>(); foreach (var doc in fileList.Result) { jobjects.Add((JObject)doc.ToJToken()); } JProperty prop = new JProperty("body", jobjects); resp = new ServiceOperationResponse(prop.Value, System.Net.HttpStatusCode.OK); break; default: throw new NotImplementedException(); } } catch (FtpCommandException ex) { HttpStatusCode code = GetStatusCode(ex.CompletionCode); throw new ServiceOperationsProviderException( httpStatus: code, errorCode: ServiceOperationsErrorResponseCode.ServiceOperationFailed, errorMessage: ex.Message + ", completion code: " + ex.CompletionCode, null); } catch (Exception ex) { throw new ServiceOperationsProviderException( httpStatus: HttpStatusCode.InternalServerError, errorCode: ServiceOperationsErrorResponseCode.ServiceOperationFailed, errorMessage: ex.Message, innerException: ex.InnerException ); } return(resp); }); return(opResp); }
/// <summary> /// 基础方法 文件下载到本地,并从本地上传到15服务器上 /// </summary> /// <param name="dataBaseClient"></param> /// <param name="rcClient"></param> /// <param name="dataBaseDicPath"></param> /// <param name="rcFullFile"></param> private void BaseDownToLocalAndUploadTo15(string localDicPath, string dataBaseDicPath, FtpListItem rcFullFile, FTPConfig fTPConfig) { string localFileFullName = localDicPath + "\\" + rcFullFile.Name; string dataBaseFileFullName = dataBaseDicPath + rcFullFile.FullName; //下载到本地 ThreadPool.QueueUserWorkItem(sender => { //每次都创建客户端,防止timeout FtpClient dataBaseClient, rcClient; CreateClient(fTPConfig, out dataBaseClient, out rcClient); //下载进度 ListenDownProcessHandler(dataBaseDicPath, rcFullFile, localFileFullName); ShowLog($"开始下载=》远程文件【{rcFullFile.FullName}】至本地【{localFileFullName}】..."); rcClient.DownloadFile(localFileFullName, rcFullFile.FullName); ShowLog($"下载成功=》远程文件【{rcFullFile.FullName}】至本地【{localFileFullName}】..."); //修改本地文件的 ModifiedTime var localFile = new FileInfo(localFileFullName); localFile.LastWriteTime = rcFullFile.Modified; ShowLog($"开始上传=》本地文件【{localFileFullName}】至服务器【{dataBaseFileFullName}】..."); dataBaseClient.UploadFile(localFileFullName, dataBaseFileFullName); ShowLog($"上传成功=》本地文件【{localFileFullName}】至服务器【{dataBaseFileFullName}】..."); dataBaseClient.SetModifiedTime(dataBaseFileFullName, rcFullFile.Modified, FtpDate.Local); }); }
/// <summary> /// 文件的下载和上传 /// </summary> /// <param name="dataBaseClient"></param> /// <param name="rcClient"></param> /// <param name="dataBaseDicPath"></param> /// <param name="matchFileName"></param> /// <param name="RcFileList"></param> private List <string> DownAndUpload(FtpClient dataBaseClient, FtpClient rcClient, string dataBaseDicPath, string matchFileName, IOrderedEnumerable <FtpListItem> RcFileList, FTPConfig fTPConfig) { var FileNameList = new List <string>(); //get a list of files and directories in the "/htdocs" folder if (!dataBaseClient.DirectoryExists(dataBaseDicPath)) { dataBaseClient.CreateDirectory(dataBaseDicPath); } var DataBaseFileList = dataBaseClient.GetListing(dataBaseDicPath); //不存在,直接去RcFileList匹配最新的一条,下载到本地然后上传到服务器. //存在,判断是否与最新的一条时间相等,如果相等,则不需要重新上传,否则需要重新上传。 FtpListItem rcFullFile = RcFileList.Where(d => d.FullName.Contains(matchFileName) && d.Modified <= fTPConfig.EndTime).FirstOrDefault(); if (rcFullFile == null && dataBaseDicPath != "/TrnFile") { throw new Exception($"未能找到对应的{dataBaseDicPath.TrimStart('/')}文件!"); } string localDicPath = string.Empty; switch (dataBaseDicPath) { case "/BakFile": localDicPath = OneKeyConst.LocalBakFilePath; break; case "/DiffFile": localDicPath = OneKeyConst.LocalDiffFilePath; break; case "/TrnFile": localDicPath = OneKeyConst.LocalTrnFilePath; break; default: break; } if (dataBaseDicPath == "/TrnFile") { DateTime fileModifiedTime = DateTime.Now; var rcDiffFile = RcFileList.Where(d => d.FullName.Contains("RC_diff_") && d.Modified <= fTPConfig.EndTime).FirstOrDefault(); if (rcDiffFile == null) { fileModifiedTime = rcFullFile.Modified; } else { fileModifiedTime = rcDiffFile.Modified; } //批量获取最近的一批数据,循环遍历下载上传 var trnList = RcFileList.Where(d => d.Modified >= fileModifiedTime && d.FullName.Contains("RC_log_") && d.Modified <= fTPConfig.EndTime).ToList(); foreach (var item in trnList) { var fisrtFile = DataBaseFileList.Where(d => d.Name == item.Name).FirstOrDefault(); if (fisrtFile == null) { BaseDownToLocalAndUploadTo15(localDicPath, dataBaseDicPath, item, fTPConfig); } if (fisrtFile != null) { ShowLog($"无需还原,{fisrtFile.Name}已存在"); } FileNameList.Add(item.Name); } } else { var fisrtFile = DataBaseFileList.Where(d => d.Name == rcFullFile.Name).FirstOrDefault(); if (DataBaseFileList == null || DataBaseFileList.Count() == 0 || fisrtFile == null) { BaseDownToLocalAndUploadTo15(localDicPath, dataBaseDicPath, rcFullFile, fTPConfig); } if (fisrtFile != null) { ShowLog($"无需还原,{fisrtFile.Name}已存在"); } FileNameList.Add(rcFullFile.Name); } return(FileNameList); }
/// <summary> /// Load FTP Config /// </summary> /// <returns></returns> bool LoadConfig() { config = Db.Select <FTPConfig>(x => x.Limit(1)).FirstOrDefault(); return(config != null); }
public IBackupTarget Prepare(string configuration) { return(new FTPTarget { config = FTPConfig.Load(configuration) }); }