windowns下自动复制文件并采用rsync同步到Linux服务器的bat脚本

艺帆风顺 发布于 2025-04-03 16 次阅读


一、需求背景

    SQL Server数据库,设置了定时备份任务,保存数据备份30天;分为7个库的备份。

    现在每天将D:SQLBackup的包含各个库子目录文件路径,取最新日期的一个备份文件,将其复制到D:SQLServerSYNCYSU8目录【在复制前先清空D:SQLServerSYNCYSU8目录】。

    复制完毕后执行rsync同步命令,并将其同步到云服务器。

二、自动获取多个库备份文件到制定目录的bat脚本

@echo offsetlocal enabledelayedexpansion
set "src_dir=D:SQLBackup"set "dst_dir=D:SQLServerSYNCYSU8"
del "%dst_dir%*" /q
for /d %%i in ("%src_dir%*") do ( set "newest_file=" set "newest_date=" for %%f in ("%%i*.*") do ( set "file_date=%%~tf" if not defined newest_date set "newest_date=!file_date!" if "!file_date!" GTR "!newest_date!" ( set "newest_date=!file_date!" set "newest_file=%%f" ) ) copy "!newest_file!" "%dst_dir%")

三、Rsync自动同步的的bat脚本

@echo offset source_path=YSU8set software_path=D:SQLServerSYNCsync
set RSYNCUSER=XT6lset dest_ip=122.X.x.96set dest_path=ysu8
%software_path:~0,2%cd %software_path%
%software_path%rsync -avzP --suffix=."%date:~0,4%-%date:~5,2%-%date:~8,2%" ../%source_path% %RSYNCUSER%@%dest_ip%::%dest_path% --password-file=./rsync.password --log-file=%software_path%/log/%date:~0,4%-%date:~5,2%-%date:~8,2%.log --log-format='%t %f %b'
C:WindowsSystem32forfiles /p %software_path%log /s /d -180 /m *.log /c "cmd /c Del @path"