使用7-zip存档文件,只上传到FTP服务器的当前日期文件,然后删除



在一个脚本中,我有几个步骤:

  • 归档后,通过7-zip归档文件,压缩、加密、删除源文件

  • 然后我从步骤1传输到FTP服务器文件。

  • 然后我删除步骤1中的旧文件。

我的脚本:

REM 7-zip archiving, compress, encryption, delete source file
@echo off
set source="D:test"
set destination="D:test"
set passwd="Qwerty"
set dd=%DATE:~0,2%
set mm=%DATE:~3,2%
set yyyy=%DATE:~6%
set hh=%TIME:~0,2%
set mm=%TIME:~3,2%
set ss=%TIME:~6,2%
set curdate=%dd%-%mm%-%yyyy%-%hh%-%mm%-%ss%
"C:Program Files7-Zip7z.exe" a -ssw -mx9 -xr!*.7z -p%passwd% %destination%backup_%curdate%.7z %source% -sdel
REM Send to FTP server by cmd ftp
set ftp_host=127.0.0.1
set ftp_username=test
set ftp_pass=test
set file_transport=transport.txt
set file_name="*.7z"
set dir_from=D:test
set dir_to=/
echo open %ftp_host%>%file_transport%
echo user %ftp_username% %ftp_pass%>>%file_transport%
echo cd %dir_to%>>%file_transport%
echo lcd %dir_from%>>%file_transport%
echo put %file_name%>>%file_transport%
echo bye>>%file_transport%
ftp -v -n -s:%file_transport%
del %file_transport%
REM delete old files older than N days
forfiles /p "D:test" /d -30 /m *.7z /c "cmd /c del @file"

所以问题是:

当我尝试发送到ftp服务器时,我不知道如何只发送当天的文件%.7z%。即使我使用file_name="*.7z",它也只适用于目录dir_from=D:test中的第一个归档,但如果我有这个值,它必须从D:test发送所有内容。我试图使用ncFTP执行此任务,但我无法理解它是如何工作的,这不起作用:

"C:Program Files (x86)NcFTP SoftwareNcFTPncftpput.exe" -u "test" -p "test" "127.0.0.1"  "/" "D:test"
@echo off
setlocal
set "source=D:test"
set "destination=D:test"
set "dirs="." "fullbackup" "diffbackup""
set "passwd=Qwerty"
set "dd=%DATE:~0,2%"
set "mm=%DATE:~3,2%"
set "yyyy=%DATE:~6%"
set "hh=%TIME:~0,2%"
set "mn=%TIME:~3,2%"
set "ss=%TIME:~6,2%"
set "curdate=%dd%-%mm%-%yyyy%-%hh%-%mn%-%ss%"
set "logfile=%cd%log.tmp"
REM Make the logfile if not exist.
if not exist "%logfile%" 1> nul 2> "%logfile%" echo.
REM Make ftp file.
set "ftp_host=127.0.0.1"
set "ftp_username=test"
set "ftp_pass=test"
set "file_transport=transport.txt"
set "ftp_root=/"
(
echo open %ftp_host%
echo user %ftp_username% %ftp_pass%
echo binary
) > "%file_transport%"
REM Zip files.
set "zipped=0"
for %%A in (%dirs%) do if exist "%source%%%~A" (
set "setdir="
for %%B in ("%source%%%~A*") do if /i not "%%~xB" == ".7z" (
"C:Program Files7-Zip7z.exe" a -ssw -mx9 -p"%passwd%" "%destination%%%~A%%~nxB_%curdate%.7z" "%%~fB" -sdel
if not errorlevel 1 (
set /a "zipped+=1"
>> "%logfile%" echo zip "%%~A%%~nxB_%curdate%.7z".
if not defined setdir (
set "setdir=1"
if not "%%~A" == "." echo mkdir "%ftp_root%/%%~A"
echo cd "%ftp_root%/%%~A"
echo lcd "%destination%%%~A"
)
echo put "%%~nxB_%curdate%.7z"
) >> "%file_transport%"
)
)
>> "%file_transport%" echo bye
REM Send to FTP server by cmd ftp.
if %zipped% equ 0 (
>> "%logfile%" echo zip no files for "%curdate%".
) else (
ftp -v -n -i -s:"%file_transport%"
if errorlevel 1 (
>> "%logfile%" echo ftp error %errorlevel%.
) else >> "%logfile%" echo ftp success.
)
del "%file_transport%"
REM Delete old files older than N days.
for %%A in (%dirs%) do if exist "%destination%%%~A*.7z" (
pushd "%destination%%%~A" && (
echo CD: "%destination%%%~A"
forfiles /d -30 /m *.7z /c "cmd /c del @file"
popd
)
)
REM Email the log content.
if not exist "%logfile%" exit /b 0
set command=^&{$usr= 'mylogon';^
$pwd= ConvertTo-SecureString -String 'mypassword' -AsPlainText -Force;^
$cred= New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $usr, $pwd;^
$body= Get-Content -LiteralPath '%logfile%' -Raw;^
Send-MailMessage -To 'mymail' -From 'mymail' -Subject 'Log' -SmtpServer 'smtp.mail.ru' -Port 587 -Credential $cred -UseSSL -Body $body}
Powershell -Command "%command%"
REM Merge or rename .tmp file to .txt file.
for %%A in ("%logfile%") do (
if "%%~xA" == ".tmp" (
if exist "%%~nA.txt" (
type "%%~A" >> "%%~nA.txt"
del "%%~A"
) else ren "%%~A" "%%~nA.txt"
)
)
exit /b 0

发布日期和时间,其中使用mm表示月份,使用mm表示分钟。现在使用mn分钟来分隔值。

除.7z文件外的所有文件都将单独压缩并可用对于CCD_ 8。

zipped变量存储压缩的文件数,因此ftp命令仅当CCD_ 11的值大于CCD_。

CCD_ 13将具有多次插入的内容,而不是一次完成。这样每个目录文件可以单独处理。

for循环现在可以迭代目录列表来压缩*.7z文件。ftpput命令将与每个文件一起回显到transport.txt拉链。

简化了对%file_transport%文件的写入。根据需要插入mkdir以确保在CCD_ 21发生之前创建远程目录。

现在在由变量CCD_ 23指定的每个目录上使用的CCD_。

变量dirs包含要迭代的目录名的列表。.是当前目录,即source路径的根目录。

powershell将通过电子邮件发送logfile文本内容cmdlet Send-MailMessage。如果变量logfile路径不存在,它将不会发送。

如果日志文件的扩展名为.tmp,它将被重命名如果扩展名为.txt,则else将合并到将删除.txt文件和.tmp文件。这是在脚本的末尾完成的。使用扩展.tmp允许powershell通过电子邮件发送.tmp文件而不是较大的.txt文件。

最新更新