Windows FTP 备份脚本 支持多目录上传

rem 设置参数
set BACKUPSVR=172.16.121.56
set USER=test
set PASSWORD=test
set REMOTE_DIR=\remote
set DATE_FORMAT=%date:~,4%-%date:~5,2%-%date:~8,2%
set LOG=d:\%DATE_FORMAT%.log
set LOCAL_DIR=d:\aaa,d:\bbb,d:\ccc

if "%1"=="" (goto M) else (goto S)

:M
for /f "tokens=1-9 delims=," %%a in ("%LOCAL_DIR%") do (
	set DIR1=%%a
	set DIR2=%%b
	set DIR3=%%c
	set DIR4=%%d
	set DIR5=%%e
	set DIR6=%%f
	set DIR7=%%g
	set DIR8=%%h
	set DIR9=%%i
)

if "%DIR1%" NEQ "" (cmd /c ftp_upload %DIR1%) else (exit)
if "%DIR2%" NEQ "" (cmd /c ftp_upload %DIR2%) else (exit)
if "%DIR3%" NEQ "" (cmd /c ftp_upload %DIR3%) else (exit)
if "%DIR4%" NEQ "" (cmd /c ftp_upload %DIR4%) else (exit)
if "%DIR1%" NEQ "" (cmd /c ftp_upload %DIR5%) else (exit)
if "%DIR1%" NEQ "" (cmd /c ftp_upload %DIR6%) else (exit)
if "%DIR1%" NEQ "" (cmd /c ftp_upload %DIR7%) else (exit)
if "%DIR1%" NEQ "" (cmd /c ftp_upload %DIR8%) else (exit)
if "%DIR1%" NEQ "" (cmd /c ftp_upload %DIR9%) else (exit)	

:S
 set LOCAL_DIR=%1
if EXIST %DIR1% (goto K) else (goto E)

:K
dir /b /ad %LOCAL_DIR% 
if %errorlevel%==1 (set BACKUP_TYPE=file) else (set BACKUP_TYPE=folder)

rem 开始备份
echo 开始备份%LOCAL_DIR% -%date%-----[%time%]---->>%LOG%
rem %LOG%是自己定义的日志文件

rem 压缩备份文件
echo 开始上传....[%time%]>>%LOG%

rem 生成由于ftp上传的命令文件
echo open %BACKUPSVR%>%TEMP%\upload.txt
echo %USER%>>%TEMP%\upload.txt
echo %PASSWORD%>>%TEMP%\upload.txt
echo bin>>%TEMP%\upload.txt
echo prompt off>>%TEMP%\upload.txt

echo  %BACKUP_TYPE%

if %BACKUP_TYPE%==file (
	echo mkdir %REMOTE_DIR%\%DATE_FORMAT%>>%TEMP%\upload.txt
	echo cd %REMOTE_DIR%\%DATE_FORMAT%>>%TEMP%\upload.txt
	echo put %LOCAL_DIR%>>%TEMP%\upload.txt	
)
if %BACKUP_TYPE%==folder (
	rem 生成要备份的目录列表文件
	cd /d %LOCAL_DIR%
	cd >%TEMP%\dir_list.txt
	dir %LOCAL_DIR% /s /b /ad>>%TEMP%\dir_list.txt

	rem 在临时目录生成mkftpcmd.bat 命令
	>%TEMP%\mkftpcmd.bat echo echo mkdir %REMOTE_DIR%\%DATE_FORMAT%%%1 ^>^>%TEMP%\upload.txt 
	>>%TEMP%\mkftpcmd.bat echo echo cd %REMOTE_DIR%\%DATE_FORMAT%%%1 ^>^>%TEMP%\upload.txt 
	>>%TEMP%\mkftpcmd.bat echo echo lcd %%1 ^>^>%TEMP%\upload.txt 
	>>%TEMP%\mkftpcmd.bat echo echo mput * ^>^>%TEMP%\upload.txt 

	for /f %%i in (%TEMP%\dir_list.txt) do cmd /c %TEMP%\mkftpcmd %%~pni
)

echo bye>>%TEMP%\upload.txt

ftp -d -s:%TEMP%\upload.txt >%TEMP%\ftp.log

rem 通过upload.txt中FTP配置信息,进行ftp上传。

findstr \C"230 Logged on" %TEMP%\ftp.log
if %errorlevel%==0 (goto F) else (goto E)

:E
echo 上传失败...[%time%]>>%LOG%
echo -------------------------------------------->>%LOG%
exit

:F
echo 上传成功...[%time%]>>%LOG%
exit



你可能感兴趣的:(Windows FTP 备份脚本 支持多目录上传)