首页 > 解决方案 > 使用 7-zip 归档文件,仅将当前日期文件上传到 FTP 服务器,然后删除

问题描述

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

我的脚本:

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"

标签: windowsbatch-filecmdftparchiving

解决方案


@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

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

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


推荐阅读