🤖 System Automation & Documentation#

1. Bash Scripting: Log Cleaner#

This script checks the system log, archives old files, and removes everything older than 30 days. Perfect for keeping your server clean.

#!/bin/bash

LOG_DIR="/var/log/myapp"
ARCHIVE_DIR="/var/log/myapp/archive"

# Create archive directory if it doesn't exist
mkdir -p "$ARCHIVE_DIR"

echo "Starting log maintenance..."

# Move logs older than 7 days to archive
find "$LOG_DIR" -name "*.log" -mtime +7 -exec mv {} "$ARCHIVE_DIR" \;

# Delete archived logs older than 30 days
find "$ARCHIVE_DIR" -name "*.log" -mtime +30 -delete

echo "Maintenance complete: $(date)"

2. Installation Procedure#

Follow these steps to deploy the script locally:

  • Step 1: Create the file: touch cleanup.sh
  • Step 2: Grant execution permissions: chmod +x cleanup.sh
  • Step 3: Add to Crontab for automation:
    • 0 0 * * * /path/to/cleanup.sh

3. Environment Variables#

VariableDescriptionDefault Value
LOG_DIRWhere the application writes logs/var/log/myapp
MAX_AGEDays before deletion30

4. Technical Note#

Note: Keep in mind that the find -delete operation is irreversible. Always test with -print first to see which files are targeted by the search.


Summary of Syntax Used:#

  • Bash Code Block: Uses triple backticks with bash for syntax highlighting.
  • Bullet Points: Simple overview of progress steps.
  • Tables: For parameter definitions.
  • Blockquotes: For important warnings.