-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate_logs.sh
More file actions
executable file
·60 lines (50 loc) · 1.58 KB
/
rotate_logs.sh
File metadata and controls
executable file
·60 lines (50 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
# Rotate logs script for production environment
# This script:
# 1. Rotates application log (mscopy.log) only
# 2. Compresses old logs
# 3. Keeps last 7 rotations
#
# Note: System logs (access.log, error.log) should be handled by systemd/logrotate
# Configuration
MAX_BACKUPS=7 # Keep 7 days of logs
LOG_DIR="logs"
APP_LOG="mscopy.log"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
# Ensure logs directory exists
mkdir -p "$LOG_DIR"
# Function to rotate a specific log file
rotate_log() {
local log_file="$1"
local base_name=$(basename "$log_file")
local dir_name=$(dirname "$log_file")
# Skip if file doesn't exist
[ ! -f "$log_file" ] && return
echo "Rotating $log_file..."
# Rotate existing backups (from .4 to .5, .3 to .4, etc)
for i in $(seq $((MAX_BACKUPS - 1)) -1 1); do
if [ -f "${log_file}.$i" ]; then
mv "${log_file}.$i" "${log_file}.$((i + 1))"
fi
if [ -f "${log_file}.$i.gz" ]; then
mv "${log_file}.$i.gz" "${log_file}.$((i + 1)).gz"
fi
done
# Backup current log
if [ -f "$log_file" ]; then
cp "$log_file" "${log_file}.1"
# Compress the backup
gzip "${log_file}.1"
# Clear the current log
echo "Log rotated at $TIMESTAMP" >"$log_file"
fi
# Remove backups older than 7 days
find "$dir_name" -name "${base_name}.*" -mtime +7 -delete
}
# Rotate only the application log
if [ -f "$APP_LOG" ]; then
rotate_log "$APP_LOG"
echo "Log rotation completed for $APP_LOG at $TIMESTAMP"
else
echo "Application log $APP_LOG not found at $TIMESTAMP"
fi