DEV Community

...horse?
...horse?

Posted on

Archiving Logs For An App That Doesn't Have Built In Archiving

Issue:
The disk on an important server keeps getting low of free space.

Cause:
The important server runs a small application that doesn't include built-in log rotation or archiving for two rather noisy components. I want to keep the local logs as small as possible but maintain an archive of all logs for at least one year.

Solution:
User PowerShell to copy all log files to a blind-write network location on a daily basis then delete all logs not being actively written to from the local location.

The code must be easily understandable for my boss since he handles this task while I am away. No loops, no arrays, etc.

Example Code:

# Ensure the target directory exists
md -f '\\filestore.mydomain.co\shared\it\NoisyApp Logs\Component1\'

# Copy the current log files to the archive location
cp -r -fo '\\noisyapp.mydomain.co\C$\Program Files\NoisyApp\Logs\Component1\*' '\\filestore.mydomain.co\shared\it\NoisyApp Logs\Component1\'

# Delete any logs not being actively written to. 
rm -r -fo '\\noisyapp.mydomain.co\C$\Program Files\NoisyApp\Logs\Component1\*' -ErrorAction SilentlyContinue
Enter fullscreen mode Exit fullscreen mode

I had to include "-ErrorAction SilentlyContinue" to suppress the flurry of red text that appears when rm cannot remove open files. This is expected behavior, but red text can be scary!

Discussion (0)