From 61a7287932a97804a0c27cc9579014ed89891c88 Mon Sep 17 00:00:00 2001 From: zhangzhanhong <468073364@qq.com> Date: Thu, 13 Jan 2022 11:06:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++ TsFTP/TsFTPBackup.tsf | 128 ++++++++++++++++++++++++++++++++++++ TsFTP/TsFTPBackupConfig.tsf | 11 ++++ examples/example1.tsl | 13 ++++ 4 files changed, 159 insertions(+) create mode 100644 README.md create mode 100644 TsFTP/TsFTPBackup.tsf create mode 100644 TsFTP/TsFTPBackupConfig.tsf create mode 100644 examples/example1.tsl diff --git a/README.md b/README.md new file mode 100644 index 0000000..15edc8b --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +FTP数据备份工具 + +使用步骤: +1、TsFTP目录放入解释器所在目录的funcext中; +2、(可选)修改默认配置信息:TsFTP\TsFTPBackupConfig.tsf; +3、参考examples\example1.tsl编写备份脚本; +4、(可选)添加备份脚本的定时调度。 \ No newline at end of file diff --git a/TsFTP/TsFTPBackup.tsf b/TsFTP/TsFTPBackup.tsf new file mode 100644 index 0000000..3547c18 --- /dev/null +++ b/TsFTP/TsFTPBackup.tsf @@ -0,0 +1,128 @@ +Type TsFTPBackup = Class + public + host; + port; + username; + password; + dataInfo; + ftpObj; + logPath; + defaultConfig; + msg; + Function Create(); + Begin + defaultConfig := TsFTPBackupConfig(); + dataInfo := array(); + End; + + Function Run(); + Begin + if not Host then Host := defaultConfig['Host']; + if not port then port := defaultConfig['Port']; + if not port then port := 21; + if not username then username := defaultConfig['username']; + if not password then password := defaultConfig['password']; + if not initFtpObj() then return 0; + for i,v in dataInfo do begin + src := v['src']; + destPath := v['destPath']; + recursive := v['recursive']; + if ifnil(recursive) then recursive := defaultConfig['recursive']; + if not ifstring(src) or not src or not ifstring(destPath) or not destPath then begin + msg := 'src='+tostn(src)+' destPath='+tostn(destPath); + log(msg,'error'); + continue; + end; + destPath := ReplaceStr(destPath,'/','\\'); + if not IsPathDelimiter(destPath,length(destPath)) then begin + msg := 'destPathӦΪĿ¼'+src+'ıݣdestPath='+destPath; + log(msg,'error'); + continue; + end; + destPath := ReplaceStr(destPath,'\\','/'); + backupPath(src,destPath,recursive); + end; + return 1; + End; + Function backupPath(src,destPath,recursive); + Begin + path := ExtractFilePath(src); + files := filelist('',src); + for j,f in files do begin + if f['FileName']='.' or f['FileName']='..' then continue; + if pos('D',f['Attr']) then begin + if recursive then begin + try + ftpObj.MakeDir(destPath+f['FileName']+'/'); + except + end; + backupPath(path+'\\'+f['FileName']+"\\*",destPath+f['FileName']+'/',recursive); + end; + end else if f['Attr']='A' then begin + backup(path+f['FileName'],destPath+f['FileName']); + end; + end; + End; + Function initFtpObj(); + Begin + ftpObj := CreateObject('FTP'); + if not ftpObj then begin + msg := "FTP"; + log(msg,'error'); + return 0; + end; + if not username or not password then begin + msg := "ûΪ"; + log(msg,'error'); + return 0; + end; + ftpObj.Host := Host; + ftpObj.Username := username; + ftpObj.Password := password; + ftpObj.UseTLS := true; + ftpObj.Port := port; + try + ftpObj.connect(); + except + msg := "ӳַ˿ʺŵϢǷȷFileMgr.dll"; + log(msg,'error'); + return 0; + end; + return 1; + End; + Function backup(srcfile,destfile); + Begin + size_s := filesize('',srcfile); + size_d := ftpObj.Size(destfile); + if size_s=size_d then begin + msg := 'Դļ('+srcfile+')Ŀļ('+destfile+')Сһ£'; + log(msg); + return 2; + end; + st_put:=CreateObject("TFileStream","",srcfile,0); + ret := ftpObj.Put(st_put,destfile,0); + size_d := ftpObj.Size(destfile); + if size_s=size_d then begin + msg := 'Դļ('+srcfile+')->Ŀļ('+destfile+')ϴɹ'; + log(msg); + return 1; + end else begin + msg := 'Դļ('+srcfile+')->Ŀļ('+destfile+')ϴ'; + log(msg,'error'); + return 0; + end; + End; + Function log(context,tag); + Begin + if not tag or not ifstring(tag) then tag := 'info'; + context := Datetimetostr(now())+' ['+tag+'] '+context+"\r\n"; + write(context); + if not logPath then logPath := defaultConfig['logPath']; + if not logPath then logPath := ExtractFilePath(SysExecName())+"log\\TsFTPBackup"; + logfile := IncludeTrailingPathDelimiter(logPath)+datetostr(today())+".log"; + pos := Filesize('',logfile); + if pos<0 then pos := 0; + ret := writefile(rwraw(),'',logfile,pos,length(context),context); + if ret<>true then writeln('д־logfile='+logfile+',err='+ret); + End; +End; diff --git a/TsFTP/TsFTPBackupConfig.tsf b/TsFTP/TsFTPBackupConfig.tsf new file mode 100644 index 0000000..971f85e --- /dev/null +++ b/TsFTP/TsFTPBackupConfig.tsf @@ -0,0 +1,11 @@ +Function TsFTPBackupConfig(); +Begin + return array( + 'Host':'192.168.0.1', + 'Port':21, + 'username':'', + 'password':'', + 'logPath':'', + 'recursive':1,//ݹ飬Ŀ¼Ҳ + ); +End; diff --git a/examples/example1.tsl b/examples/example1.tsl new file mode 100644 index 0000000..dd8b175 --- /dev/null +++ b/examples/example1.tsl @@ -0,0 +1,13 @@ +tsftpbackup := new TsFTPBackup(); +tsftpbackup.username := 'username'; +tsftpbackup.password := 'password'; +tsftpbackup.dataInfo := array( +//说明: +//1、destPath分隔符可以是/或\(引号中为\\) +//2、destPath必须以分隔符结束,文件名与源一致 +//3、recursive为是否递归备份子目录 + ('src':%% D:\temp\filename.txt%%,'destPath':'/temp/','recursive':1), + ('src':%% D:\temp\*%%,'destPath':'/','recursive':1), + ('src':%% D:\temp\aaa\*%%,'destPath':'/bbb/','recursive':0), +); +return tsftpbackup.Run();