@echo off
rem =====================================================
rem 获取昨日日期
rem =====================================================
set YE=%date:~0,4%
set MO=%date:~5,2%
set DA=%date:~8,2%
set DG=1
set/a vY1=%YE% %% 400
set/a vY2=%YE% %% 4
set/a vY3=%YE% %% 100
if %vY1%==0 (set var=true) else (if %vY2%==0 (if %vY3%==0 (set var=false) else (set var=true)) else (set var=false))
set LY=%YE%
set LM=%MO%
if %MO:~0,1%==0 (set MO=%MO:~1,1%)
if %DA:~0,1%==0 (set DA=%DA:~1,1%)
if %DA% GTR %DG% (set/a LD=%DA%-%DG%) else (
if %MO%==1 (set/a LY=%YE%-1) & (set/a LM=12) & (set/a LD=31+%DA%-%DG%) else (
set/a LM=%MO%-1
if %MO%==3 (if %var%==false (set/a LD=28+%DA%-%DG%) else (set/a LD=29+%DA%-%DG%))
for %%a in (2 4 6 8 9 11) do (if "%MO%"=="%%a" (set/a LD=31+%DA%-%DG%))
for %%b in (5 7 10 12) do (if "%MO%"=="%%b" (set/a LD=30+%DA%-%DG%))))
if %LM% LSS 10 set LM=0%LM:~-1%
if %LD% LSS 10 set LD=0%LD:~-1%
set strLstDt=%LY%%LM%%LD%
rem =====================================================
rem 以下信息需要手动配置修改
rem =====================================================
rem 数据库的IP地址
set DB_HOST=127.0.0.1
rem 数据库的端口
set DB_PORT=27017
rem 数据库名
set DB_NAME=test
rem 用户名
set DB_USER=root
rem 用户密码
set DB_PWD=root
rem mongodump命令路径
set DUMP=D:\迅雷下载\mongodb-database-tools-windows-x86_64-100.5.1\bin
rem 备份文件保存的路径
set BACK_UP_DIR=D:\工作文档\杂项\mongodb定时备份\Mongodb\databaseBack
rem =====================================================
rem 以下为批处理逻辑部分,不需要修改
rem =====================================================
set BAK_FILE_NAME=yapi_bak_%date:~0,4%%date:~5,2%%date:~8,2%
%DUMP:~0,2%
cd %DUMP%
mongodump --authenticationDatabase admin -h %DB_HOST% -p %DB_PORT% -d %DB_NAME% -u %DB_USER% -p %DB_PWD% -o %BACK_UP_DIR%\%BAK_FILE_NAME%
set BEFORE_BACK_DIR=%BACK_UP_DIR%\yapi_bak_%strLstDt%
rd /s/q %BEFORE_BACK_DIR%