How do I configure mongodb to remove old log files? - mongodb

I've read the mongodb docs and am rotating my log files on Windows 10. However, the number of logs grow until I manually delete them. I'd like to keep 30 days worth of logs.
I was hoping there was a config setting similar to NLog where I can specify how many files to keep before they are re-written. Maybe I am misunderstanding something in the docs. What is the "best practice" for automatically removing old log files?

Looks like the easiest way to do this without 3rd party utilities is to create a batch file that runs each night via Task Scheduler.

years later, but still helpful might be this cmd-script. It's written to remove old stuff in my profile, but really simple to extend:
#echo off
#setlocal
REM Remove everything older then "/D" days (ten/10 days in our examples below)
REM BUT we can not remove directories with files in it, so we do it in two-steps ways:
REM 1st remove all files by using the command: del
REM 2nd remove all files by using the command: rd
REM cleanup %TMP%
forfiles -p "%TMP%" -m * /D -10 /C "cmd /c del /s/q #path > nul 2> nul"
forfiles -p "%TMP%" -m * /D -10 /C "cmd /c rd /s/q #path > nul 2> nul"
REM cleanup %TEMP%
forfiles -p "%TEMP%" -m * /D -10 /C "cmd /c del /s/q #path > nul 2> nul"
forfiles -p "%TEMP%" -m * /D -10 /C "cmd /c rd /s/q #path > nul 2> nul"
REM cleanup the Downloads folder in the current profile
forfiles -p "%USERPROFILE%\Downloads" -m * /D -10 /C "cmd /c del /s/q #path > nul 2> nul"
forfiles -p "%USERPROFILE%\Downloads" -m * /D -10 /C "cmd /c rd /s/q #path > nul 2> nul"
#endlocal
#echo on

Related

Running Commands in Batch FIle

I am trying to create a batch file to run start some micro services and database
1 FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :1000') DO #ECHO TaskKill.exe /PID %%P
2 FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :1001') DO #ECHO TaskKill.exe /PID %%P
3 FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :5432') DO #ECHO TaskKill.exe /PID %%P
4 start cd "C:\Program Files\PostgreSQL\10\bin\" & pg_ctl.exe -D "c:\Program Files\PostgreSQL\10\data" start
#REM to start service
5 start javaw -jar -Dserver.port=1000 text-annotation-tool-1.0-SNAPSHOT.jar
Line 1 to 3 and Line 5 execute correctly when executed by commenting line 4.
Line 4 is to start a Postgres server in a new prompt (beacuse of the Dir change). I think the problem is with the way I have used quotes. The 'start' at the beginning and ending of line 4 serve different purpose.
Also, if I execute line 4 in different prompt, How do I close the prompt after execution (nohup equivalent)
There are two errors: You can't "pass" cd to the start command. And start has the quirk to interpret the first quoted parameter as the new window's title. So start "C:\Program Files\PostgreSQL\10\bin\" ... wouldn't work, you need to supply a dummy window title. The & also seems wrong
So you need:
start "Postgres Server" "C:\Program Files\PostgreSQL\10\bin\pg_ctl.exe" -D "c:\Program Files\PostgreSQL\10\data" start
As the full path to pg_ctl.exe is supplied, there is no need for a cd.
But if you want to define the default directory for the new process, you have to use the /D parameter:
start "Postgres Server" /D "C:\Program Files\PostgreSQL\10\bin" pg_ctl.exe -D "c:\Program Files\PostgreSQL\10\data" start
Unrelated, but: putting the Postgres data directory into c:\Program Files\ is a very bad idea. That directory has special permissions for a purpose. You should use %ProgramData% or %AppData% instead

powershell $lastexitcode does not work when running batch files

I try to get the exitcode/errorlevel code from a bat file into the powershell script calling the bat file.
Althought the %ErrorLevel% is 1:
BAT file
call aCONFIGURATION
for %%f in (.\createData\*.g.sql) do sqlcmd -b -U %UserName% -P %Password% -S %sqlClonedServer% -d %sqlClonedDatabaseName% -i %%f -r1 1> NUL
echo %ERRORLEVEL%
When I do in the powershellscript
$lastexitcode its ALWAYS 0 but the %ErrorLevel% says 1
$build = "c:\my.bat"
$state = & $build
Write-Host $LASTEXITCODE
I am pulling my hairs of this crap powershell full with bugs.
I have read these links but they did not help as the result is different:
http://blogs.technet.com/b/heyscriptingguy/archive/2011/06/06/get-legacy-exit-codes-in-powershell.aspx
https://social.technet.microsoft.com/Forums/scriptcenter/en-US/05e37b8d-761b-4fd7-881e-977f114ad8d7/obtaining-errorlevel-return-codes-in-powershell-is-it-possible
How can I fix the $lastexitcode
echo %ERRORLEVEL% just writes the error code to stdout, after which the batch file exits normally.
If you want the batch script to actually return an error on exit, use exit /b [exitcode]:
call aCONFIGURATION
for %%f in (.\createData\*.g.sql) do sqlcmd -b -U %UserName% -P %Password% -S %sqlClonedServer% -d %sqlClonedDatabaseName% -i %%f -r1 1> NUL
exit /b %ERRORLEVEL%

%%G from For Loop changing from index to %G using

I am trying to write a batch file to automate some routine Matlab processes. The batch file loops through from 0 to a set value (usually between 50 and 75) using the For /L structure. The script copies the main Matlab script to the subfolder and runs it. The batch would normally continue onward so I put a :loop to wait until the Matlab ends.
echo off
setlocal EnableDelayedExpansion
REM The format is matlab_auto.in (max value).
For /L %%G in (0,1,%1) do (
REM Sanity check
echo 1 %%G
REM Create Outputs folder if non-existent
if not exist Outputs md Outputs
REM Copy .m file into deg folder and cd to folder
copy values_calc.m %%Gsort\values_calc.m
cd %%Gsort
echo Got to folder
REM Running .m script and sanity check
echo 2 %%G
matlab -nosplash -nodesktop -noFigureWindows -logfile output.log -r "run('values_calc.m');"
echo 3 %%G
REM Waiting for matlab to finish
:loop
tasklist /fi "imagename eq MATLAB.exe" |find ":" > nul
echo 4 %%G
if errorlevel 1 goto loop
echo Finished Matlab
echo 5 %%G
REM Copy .m outputs into outputs folder, ignoring confirmation
copy Output_*.* ..\Outputs /Y
echo Copied outputs
REM Sanity check and return home
echo 6 %%G
cd %~dp0
echo Home again
)
The problem I'm having is that when it ends after the first iteration of the For loop. Echo 1, 2, 3 are 0. Echo 4 shows 0 the first time through :loop but then it shows %G for the remainder of the loops and at Echos 5 and 6. It also does not continue into further iterations of the For loop. I'm assuming this is because %%G is no longer a number (or in the range specified).
I have tried implementing a call subroutine to use the goto outside the loop but then it opens the Matlab dozens of times, crashing the computer.
Any insight or advice is appreciated. Thank you.
EDIT: Changed the :: for commenting to REM. It did not resolve this issue but looks better.
EDIT 2: I have a test case that demonstrates the problem. Its something with the :loop or goto.
echo off
setlocal EnableDelayedExpansion
for /l %%G in (0,1,5) do (
:loop
echo %%G
pause
if %%G==0 goto loop
)
Yes. The execution of a GOTO command cancel any active (pending) FOR or IF commands that may be nested inside parentheses at any level. This way, the commands placed below the :loop label are executed inside the FOR context the first time, but after the goto command they are executed as if they were placed outside the FOR loop! The way to solve this problem is extracting the code below the label into a subroutine and then call :loop in the FOR.
echo off
setlocal EnableDelayedExpansion
REM The format is matlab_auto.in (max value).
For /L %%G in (0,1,%1) do (
REM Sanity check
echo 1 %%G
REM Create Outputs folder if non-existent
if not exist Outputs md Outputs
REM Copy .m file into deg folder and cd to folder
copy values_calc.m %%Gsort\values_calc.m
cd %%Gsort
echo Got to folder
REM Running .m script and sanity check
echo 2 %%G
matlab -nosplash -nodesktop -noFigureWindows -logfile output.log -r "run('values_calc.m');"
echo 3 %%G
REM Waiting for matlab to finish
call :loop
echo Finished Matlab
echo 5 %%G
REM Copy .m outputs into outputs folder, ignoring confirmation
copy Output_*.* ..\Outputs /Y
echo Copied outputs
REM Sanity check and return home
echo 6 %%G
cd %~dp0
echo Home again
)
goto :EOF
:loop
tasklist /fi "imagename eq MATLAB.exe" |find ":" > nul
REM echo 4 %%G
if errorlevel 1 goto loop
exit /B

Delete all files after date, code is slow

I made a question last week about getting a batch file or code to delete all .txt files in a folder that were created before the last 60 days and I was directed to use the code below.
forfiles -p "J:\Test_Files" -s -m *.txt* -d 60 -c "cmd /c del #path"
This code does the job and works fine but it goes too slow deleting 250 files per minute. I need to delete a total of 2,600,000 files and this would take too long.
The code I used below deletes 200 files a second but deletes all .txt files
cd /BASE_PATH
del /s *.txt
How can I edit this code to delete files created before 60 days? I need it to delete at a faster pace.
Thank you for helping! :D
You can try with
#echo off
setlocal enableextensions disabledelayedexpansion
rem Configure script
set "target=J:\Test_Files"
set "fileMask=*.txt"
set "age=60"
rem We will use a temporary file.
for %%t in ("%temp%\%~nx0.%random%%random%%random%.tmp") do (
rem Send to temp file the list of matching files retrieved by the robocopy command
>"%%~ft" robocopy "%target%." "%target%." "%fileMask%" /minage:%age% /l /nocopy /is /s /njh /njs /ndl /nc /ns
rem Process temporary file deleting the selected files
for /f "usebackq tokens=*" %%f in ("%%~ft") do echo del "%%~ff"
rem Once done, remove the temporary file
) & del /q "%%~ft"
del commands are only echoed to console. If the output is correct, remove the echo command.

looping through files for postgresql give error in windows

i'm trying to read files and load them all csv files, and once done move the files to some other location :
cd E:\data\
for /f %%a in (’dir /b filename*.CSV) do (
psql -U postgre -W password -c "COPY INTO LA from %%a" zipcodes
mv %%a E:\data\bc\
)
but it gives me following error:
E:\data>for /F %a in (ΓÇÖdir /b filename*.CSV) do (
psql -U postgre -W password-c "COPY INTO LA from %a" zipcodes
mv %a E:\data\bc\
)
The system cannot find the file ΓÇÖdir.
thanks for help
#echo off
setlocal enableextensions disabledelayedexpansion
set "psql=c:\wherever\psqlIs\psql.exe"
pushd "e:\data" && (
for %%a in ("filename*.CSV") do (
"%psql%" -U postgre -W password -c "COPY INTO LA from %%~a" zipcodes
move "%%~a" "E:\data\bc\"
)
popd
)
for /f is intended for file/string processing. To iterate over a set of files use a simple for
Anyway, the problem in your code are the opening single quote (as Alex K has pointed), the missing closing quote and the non existing mv command in windows that should be move
edited it seems there are problems with file loading. psql copy command indicates it is better to use full path names (with backslashes doubled), so
#echo off
setlocal enableextensions enabledelayedexpansion
set "psql=c:\wherever\psqlIs\psql.exe"
pushd "e:\data" && (
for %%a in ("filename*.CSV") do (
set "file=%%~fa"
"%psql%" -U postgre -W password -c "COPY INTO LA from E'!file:\=\\!'" zipcodes
move "%%~a" "E:\data\bc\"
)
popd
)