Port3101.org : Your BES Connection

Port3101.org : Your BES Connection (http://www.port3101.org/index.php)
-   Scripts & Tools / Hints & Tricks (http://www.port3101.org/forumdisplay.php?f=12)
-   -   Script to Zip Log Files (http://www.port3101.org/showthread.php?t=717)

Sith_Apprentice 03-16-2009 10:16 AM

Script to Zip Log Files
 
1 Attachment(s)
Originally Posted by hdawg on BBF.

RIM has the ability to automatically purge old log files as a part of BES. You can go to BlackBerry Server Configuration and change how long the server will keep the logs before purging them (as seen in the picture attached), but you can't do the same for SMS/PIN/Phone logs.

Regardless, if there's one thing you probably know about me, it's that I'm a log junkie. Much like a scale doesn't lie; the logs don't lie ... and I hate to get rid of them; so much that I really have no problem keeping them for a year or more. There is so much value in them, and they really don't take up all the much space if you zip them up ... even in very busy environment. But, if you leave logs as uncompressed text, you could easily see a volume fill up and you'll be out of space wondering what the heck is going on.

That said, to address a few of my desires:

1) To have logs for as long as possible
2) To not waste disk space unnecessarily
3) To make troubleshooting easier
4) To make planning easier (the next time you need an upgrade you can use the data in these logs to backup your claims / requests)
5) To deliver a script that I promised to a customer that has been patiently waiting

I've put together this mega-simple script which can really be modified to just about anything ... just be gentle with it because if you change things around you could easily be deleting entire file systems instead of just zipping up some aging log files.

Attached is a zip file with 3 files:

zip.exe (duh)
forfiles.exe (command to iterate the zip command)
go.cmd (script that you execute)

... and here is the contents of go.cmd:

-----

@ECHO OFF
REM -- Script to individually zip BES Log files ...
REM -- Place this file along with forfiles.exe and zip.exe in the root directory of your BlackBerry Logs folder
REM -- By Default: C:\Program Files\Research in Motion\BlackBerry Enterprise Server\Logs
REM -- This will zip all TXT files in the current and all sub-directories 10 days old or older
REM -- 4/23/2008 - Updated to delete old files / directories
REM -- Change "-d-90" if you want to delete files at an age different than 90 days
REM -- Uncomment the REM -- lines at the bottom if you actually want it to do deletion

forfiles.exe -p. -s -m*.txt -d-10 -c"zip -m @FILE.zip @FILE"

REM -- forfiles.exe -p. -s -d-90 -m*.txt -c"cmd /c if @ISDIR==FALSE del /q /f @FILE"
REM -- forfiles.exe -p. -s -d-90 -m*.csv -c"cmd /c if @ISDIR==FALSE del /q /f @FILE"
REM -- forfiles.exe -p. -s -d-90 -m*.zip -c"cmd /c if @ISDIR==FALSE del /q /f @FILE"
REM -- forfiles.exe -p. -s -d-90 -m*.* -c"cmd /c if @ISDIR==TRUE rd /q @FILE"
-----

Here's a breakdown of what the command is doing:

forfiles.exe (The forfiles command, to iterate through all the log files)
-p. (The starting point for the filelist. "." means the current working directory that forfiles.exe is located in)
-s (Recurse subdirectories)
-m*.txt (Process all .TXT files)
-d-10 (10 days old or older)
-c"zip -m @FILE.zip @FILE" (The command to execute on each file. @FILE represents the current file being processed. zip -m tells it to zip the file and move the file into the zip, therefore removing the original .TXT file. Then the next time this gets run it won't have to zip the file again.)

Put these 3 files in the root of your BES Logs directory ... by default it is: "C:\Program Files\Research in Motion\BlackBerry Enterprise Server\Logs" and schedule go.cmd as a task to run nightly; you'll keep the last 10 days of logs easily accessible and older logs on the file system, just compressed.

I had thought about having each day zip into a single file, but then the structure of what is there gets modified ... I wanted to keep it simple, keep the directory / file structure as it was, but just compress the files.

Alternatively, you could enable file compression on the file system and ignore this script totally ... but I like this better

hdawg 03-31-2009 09:55 PM

w00t :)

jdizzle 07-15-2009 02:49 PM

Very good stuff, but one comment, one question on this:

Comment:
If you want to have it delete files older then 90 days, you need to move the line
forfiles.exe -p. -s -m*.txt -d-10 -c"zip -m @FILE.zip @FILE"
to the end (after the 4 lines to delete files); otherwise, it zips everything older then 10 days, then when it gets to the lines to find files older then 90 days, it doesn't find anything because zipping the files changed the modified date on all of them.

Question:
Should the lines to delete files also delete the folders? Once I figured out that the zip portion needed to come after the delete portion, it still doesn't appear to be removing the older folders--not sure if I did something to cause this, or if it doesn't delete them by design.

hdawg 07-15-2009 04:06 PM

Quote:

Originally Posted by jdizzle (Post 8311)
Very good stuff, but one comment, one question on this:

Comment:
If you want to have it delete files older then 90 days, you need to move the line
forfiles.exe -p. -s -m*.txt -d-10 -c"zip -m @FILE.zip @FILE"
to the end (after the 4 lines to delete files); otherwise, it zips everything older then 10 days, then when it gets to the lines to find files older then 90 days, it doesn't find anything because zipping the files changed the modified date on all of them.

the del for *.txt really I guess is irrelevant since the files are getting zipped anyway. But yes, it'd need to be moved to be effective for that.

Quote:

Question:
Should the lines to delete files also delete the folders? Once I figured out that the zip portion needed to come after the delete portion, it still doesn't appear to be removing the older folders--not sure if I did something to cause this, or if it doesn't delete them by design.
forfiles.exe -p. -s -d-90 -m*.* -c"cmd /c if @ISDIR==TRUE rd /q @FILE"


^^ should delete the folders.


All times are GMT -4. The time now is 02:04 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2019, vBulletin Solutions, Inc.


SEO by vBSEO 3.3.2 PL2