Automation is a buzzword in today's technology; it makes things faster, effortless, and most of all ease the burden of system administrators.
However, automation should be tested thoroughly to avoid any unforeseen circumstances or else instead of making life easier it might deliver some nightmares.
For example, removing log files to save space or remove any unwanted or old log files to make room for new files; this task can be done manually or using some automation via a script.
In Linux, cron jobs that fires automatically as per schedule is quite ideal to automate removing or deleting log files.
This can also be done manually, by simply logging in to the server and delete files.
There is nothing wrong in deleting manually but would not be ideal if the operation of the server is operating day and night.
There might be a chance that the operation will halt if there is no more room to write the log files. Or simply when there is some issue and wanted to troubleshoot; if log files are not available then it might take a longer time to troubleshoot since there are no reference files for troubleshooting and troubleshooting will be a nightmare.
Clearing the log files and removing old files is a must, in order to save space and make room for new files.
In Linux, rm file_to_be_deleted is quite straight forward to remove or delete a file.
Removing files via script or automation, needs to be done carefully in order not to remove any unwanted files.
Example script:
LOG_DIR="." #or change the location
# Find files older than 30 days and loop through them
find "$LOG_DIR" -type f -mtime +30 -print0 | while IFS= read -r -d '' file; do
echo "Processing: $file"
# Or just move to archive folder
# mv "$file" ./archive/
#show which files will be deleted (check if it shows the correct files)
echo "Removing files: rm -rf -- $file"
# Or delete (uncomment carefully)
#rm -rf -- "$file"
done
rm -rf -- "$file" => is a safety mechanism in order not to delete recursively
The -- forces rm to treat anything beyond -- as a literal path.
Example, if there is a malicious filename like:
file="-rf-badass-filename.txt" # A malicious or weird filename
rm -f $file # Becomes: rm -f -rf-badass-filename.txt (-rf is treated as a parameter not a filename)
But yes, newer operating distro or systems doesn't fall into this trick anymore since they will append the full path when removing the file.
Example for the script above it will be:
rm -rf ./any_old_files.log
rm -rf ./-rf-badass-filename.txt
The full path is appended, averting a recursive deletion.
Even if the operating system has already a mechanism to avoid disaster, it would be better to just use the rm -rf -- $file syntax to have a peace of mind.
Till next time...
Fill your hearts with grateful thoughts and there is no more room for worry and fear.
And turn your eyes and hearts towards the love of God.
Comments
Post a Comment