windows - 使用 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 Files\7-Zip\7z.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 Software\NcFTPncftpput.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 Files\7-Zip\7z.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 文件外的所有文件都将单独压缩并用于 .7z 文件ftp
。
zipped
变量存储压缩文件的计数,因此ftp
命令仅在 的值zipped
大于时运行0
。
transport.txt
将多次插入内容,而不是一次全部插入。这样做是为了可以分别处理每个目录和文件。
循环现在可以将for
目录列表迭代到 zip*.7z
文件。这些ftp
put
命令将transport.txt
与每个压缩文件一起回显。
简化写入%file_transport%
文件。根据需要插入mkdir
以确保在cd
发生之前创建远程目录。
forfiles
现在用于变量指定的每个目录dirs
。
该变量dirs
包含要迭代的目录名称列表。
是当前目录,即路径.
的根目录。source
powershell
将logfile
使用 cmdlet Send-MailMessage通过电子邮件发送文本内容。logfile
如果变量路径不存在,它将不会发送。
如果日志文件的扩展名是,如果不存在.tmp
则以扩展名重命名.txt
,否则将合并到
.txt
文件中并.tmp
删除文件。这是在脚本末尾完成的。使用扩展名.tmp
允许powershell
通过电子邮件发送文件中的内容.tmp
,而不是较大的.txt
文件。
推荐阅读
- extjs - Extjs 3.0.2 升级到 4.0.2 压缩输入失败
- c - c中的printf中出现意外的换行符
- flutter - 颤振:日期选择器中的 api 日期显示
- html - 基于 src HTML 的 if 语句
- excel - 将 XLSM 保存到 XLSX
- vb.net - 将 TextBox 的名称输入到 TextBox 中,然后能够检索命名 TextBox 的内容
- python - Python/Pandas/Excel 从 3 列创建二维数组
- node.js - 我想在用户创建帐户后立即为其创建自定义令牌
- javascript - Suitescript 将 .0 添加到数组中的数字
- python - 使用 BeautifulSoup/Selenium 从站点获取页码