askrobots / dbbasic-logs
Public
Structured logging to TSV files with JSON context: grep-able, compressible and searchable.
rotate-logs.sh · 2.6 KB · 94 lines
| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Log rotation script for dbbasic-logs |
| 4 | # |
| 5 | # Install to cron for automatic rotation: |
| 6 | # sudo cp rotate-logs.sh /etc/cron.daily/dbbasic-logs-rotate |
| 7 | # sudo chmod +x /etc/cron.daily/dbbasic-logs-rotate |
| 8 | # |
| 9 | # Or run manually: |
| 10 | # ./rotate-logs.sh |
| 11 | # |
| 12 | |
| 13 | set -e |
| 14 | |
| 15 | # Configuration |
| 16 | LOG_DIR="${LOG_DIR:-data/logs}" |
| 17 | APP_RETENTION_DAYS="${APP_RETENTION_DAYS:-30}" |
| 18 | ACCESS_RETENTION_DAYS="${ACCESS_RETENTION_DAYS:-30}" |
| 19 | ERROR_RETENTION_DAYS="${ERROR_RETENTION_DAYS:-90}" |
| 20 | |
| 21 | # Get yesterday's date |
| 22 | YESTERDAY=$(date -d yesterday +%Y-%m-%d 2>/dev/null || date -v-1d +%Y-%m-%d 2>/dev/null) |
| 23 | |
| 24 | echo "DBBasic Logs Rotation - $(date)" |
| 25 | echo "Log directory: $LOG_DIR" |
| 26 | echo "Yesterday: $YESTERDAY" |
| 27 | echo "" |
| 28 | |
| 29 | # Function to compress logs |
| 30 | compress_logs() { |
| 31 | local log_type=$1 |
| 32 | local log_file="${LOG_DIR}/${log_type}/${YESTERDAY}.tsv" |
| 33 | |
| 34 | if [ -f "$log_file" ]; then |
| 35 | echo "Compressing ${log_type}/${YESTERDAY}.tsv..." |
| 36 | gzip "$log_file" |
| 37 | echo " ✓ Compressed to ${log_file}.gz" |
| 38 | else |
| 39 | echo " ℹ No log file found: ${log_type}/${YESTERDAY}.tsv" |
| 40 | fi |
| 41 | } |
| 42 | |
| 43 | # Function to delete old logs |
| 44 | delete_old_logs() { |
| 45 | local log_type=$1 |
| 46 | local retention_days=$2 |
| 47 | local count=0 |
| 48 | |
| 49 | echo "Deleting ${log_type} logs older than ${retention_days} days..." |
| 50 | |
| 51 | if [ -d "${LOG_DIR}/${log_type}" ]; then |
| 52 | # Find and delete old compressed logs |
| 53 | count=$(find "${LOG_DIR}/${log_type}" -name "*.tsv.gz" -mtime +${retention_days} -type f | wc -l) |
| 54 | find "${LOG_DIR}/${log_type}" -name "*.tsv.gz" -mtime +${retention_days} -type f -delete |
| 55 | |
| 56 | if [ $count -gt 0 ]; then |
| 57 | echo " ✓ Deleted $count old ${log_type} log files" |
| 58 | else |
| 59 | echo " ℹ No old ${log_type} logs to delete" |
| 60 | fi |
| 61 | fi |
| 62 | } |
| 63 | |
| 64 | # Compress yesterday's logs |
| 65 | echo "=== Compressing Yesterday's Logs ===" |
| 66 | compress_logs "app" |
| 67 | compress_logs "errors" |
| 68 | compress_logs "access" |
| 69 | echo "" |
| 70 | |
| 71 | # Delete old logs based on retention policy |
| 72 | echo "=== Cleaning Old Logs ===" |
| 73 | delete_old_logs "app" "$APP_RETENTION_DAYS" |
| 74 | delete_old_logs "access" "$ACCESS_RETENTION_DAYS" |
| 75 | delete_old_logs "errors" "$ERROR_RETENTION_DAYS" |
| 76 | echo "" |
| 77 | |
| 78 | # Show disk usage |
| 79 | if [ -d "$LOG_DIR" ]; then |
| 80 | echo "=== Log Directory Size ===" |
| 81 | du -sh "$LOG_DIR" |
| 82 | echo "" |
| 83 | echo "Breakdown by type:" |
| 84 | for log_type in app errors access; do |
| 85 | if [ -d "${LOG_DIR}/${log_type}" ]; then |
| 86 | size=$(du -sh "${LOG_DIR}/${log_type}" | cut -f1) |
| 87 | files=$(find "${LOG_DIR}/${log_type}" -type f | wc -l) |
| 88 | echo " ${log_type}: ${size} (${files} files)" |
| 89 | fi |
| 90 | done |
| 91 | fi |
| 92 | |
| 93 | echo "" |
| 94 | echo "Rotation complete: $(date)" |