Structured logging to TSV files with JSON context: grep-able, compressible and searchable.

0 downloads

dbbasic-logs/scripts/rotate-logs.sh
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-logs.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-logs.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-logs.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add PyPI badges and links to README cea7e20 1 year, 1 month ago History
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
13set -e
14
15# Configuration
16LOG_DIR="${LOG_DIR:-data/logs}"
17APP_RETENTION_DAYS="${APP_RETENTION_DAYS:-30}"
18ACCESS_RETENTION_DAYS="${ACCESS_RETENTION_DAYS:-30}"
19ERROR_RETENTION_DAYS="${ERROR_RETENTION_DAYS:-90}"
20
21# Get yesterday's date
22YESTERDAY=$(date -d yesterday +%Y-%m-%d 2>/dev/null || date -v-1d +%Y-%m-%d 2>/dev/null)
23
24echo "DBBasic Logs Rotation - $(date)"
25echo "Log directory: $LOG_DIR"
26echo "Yesterday: $YESTERDAY"
27echo ""
28
29# Function to compress logs
30compress_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
44delete_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
65echo "=== Compressing Yesterday's Logs ==="
66compress_logs "app"
67compress_logs "errors"
68compress_logs "access"
69echo ""
70
71# Delete old logs based on retention policy
72echo "=== Cleaning Old Logs ==="
73delete_old_logs "app" "$APP_RETENTION_DAYS"
74delete_old_logs "access" "$ACCESS_RETENTION_DAYS"
75delete_old_logs "errors" "$ERROR_RETENTION_DAYS"
76echo ""
77
78# Show disk usage
79if [ -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
91fi
92
93echo ""
94echo "Rotation complete: $(date)"