最近公司要求每日备份文件并压缩,tomcat应用需要每日定时启动和停止,记录一下。
@echo off
REM ###########################################################
REM # Tomcat自动备份批处理脚本
REM ###########################################################
REM 取当前系统时间,可能因操作系统不同而取值不一样
set CURDATE=%date:~0,4%%date:~5,2%%date:~8,2%
REM Tomcat目录
set tomcat_dir=D:\apache-tomcat-9.0.46-windows
REM Tomcat备份目录
set tomcat_backdir=D:\back
REM 若目标路径不存在则创建
if not exist "%tomcat_backdir%" mkdir %tomcat_backdir%
if not exist "%tomcat_backdir%\back" mkdir %tomcat_backdir%\back
if not exist "%tomcat_backdir%\tmp" mkdir %tomcat_backdir%\tmp
if exist "%tomcat_backdir%\Tomcat.zip" del %tomcat_backdir%\Tomcat.zip
REM 备份tomcat目录
xcopy %tomcat_dir% %tomcat_backdir%\tmp /s/y/c/k/e
REM 备份文件压缩
REM tar -czvf %tomcat_backdir%\Tomcat.zip %tomcat_backdir%\tmp\*
7z a %tomcat_backdir%\Tomcat.zip %tomcat_backdir%\tmp\* -r -mx=5
copy %tomcat_backdir%\Tomcat.zip %tomcat_backdir%\back\Tomcat_%CURDATE%.zip
rd %tomcat_backdir%\tmp /s/q
rem 正在删除N天前的备份版本库 下面N=5
forfiles /p %tomcat_backdir%\back\ /s /m * /d -5 /c "cmd /c del /s/q @path"
REM pause
exit
备份需要下载7z
@echo off
REM ###########################################################
REM # Tomcat自动停止和启动脚本
REM ###########################################################
REM 取当前系统时间,可能因操作系统不同而取值不一样
set CURDATE=%date:~0,4%%date:~5,2%%date:~8,2%
REM Tomcat目录
set tomcat_dir=D:\apache-tomcat-9.0.65\bin
REM 移动到Tomcat所在目录
D:
cd %tomcat_dir%
SET PW=%1
IF "%PW%" == "START" (
startup.bat >> D:\back\logs.log
echo %CURDATE% : tomcat已经开启 >> D:\back\logs.log
) ELSE (
TASKKILL /f /im java.exe >> D:\back\logs.log
echo %CURDATE% : tomcat已经关闭 >> D:\back\logs.log
)
REM pause
exit