logrotate-mongodb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/bin/bash
  2. # MongoDB Log Rotation Script for Wekan Snap
  3. # This script handles log rotation for MongoDB logs in the Wekan snap environment
  4. set -e
  5. # Source settings if available, otherwise use defaults
  6. if [ -f "$SNAP/bin/wekan-read-settings" ]; then
  7. source $SNAP/bin/wekan-read-settings
  8. else
  9. # Default values when wekan-read-settings is not available
  10. SNAP_COMMON="/var/snap/wekan/common"
  11. SNAP_NAME="wekan"
  12. fi
  13. # Configuration
  14. MONGODB_LOG="${SNAP_COMMON}/mongodb.log"
  15. MAX_SIZE_MB=100
  16. KEEP_COPIES=10
  17. COMPRESS_LOGS=true
  18. # Logging functions
  19. log_message() {
  20. local message="$1"
  21. local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
  22. echo "[$timestamp] $message"
  23. }
  24. log_error() {
  25. local message="$1"
  26. log_message "ERROR: $message"
  27. }
  28. log_success() {
  29. local message="$1"
  30. log_message "SUCCESS: $message"
  31. }
  32. log_warning() {
  33. local message="$1"
  34. log_message "WARNING: $message"
  35. }
  36. # Check if log file exists and is large enough to rotate
  37. check_rotation_needed() {
  38. if [ ! -f "$MONGODB_LOG" ]; then
  39. log_message "MongoDB log file not found: $MONGODB_LOG"
  40. return 1
  41. fi
  42. # Get log file size in MB
  43. local log_size_mb=$(du -m "$MONGODB_LOG" | cut -f1)
  44. if [ "$log_size_mb" -lt "$MAX_SIZE_MB" ]; then
  45. log_message "MongoDB log size (${log_size_mb}MB) is below rotation threshold (${MAX_SIZE_MB}MB)"
  46. return 1
  47. fi
  48. log_message "MongoDB log size (${log_size_mb}MB) exceeds rotation threshold (${MAX_SIZE_MB}MB)"
  49. return 0
  50. }
  51. # Rotate MongoDB log file
  52. rotate_log() {
  53. local mongodb_log="$1"
  54. local max_size_mb="$2"
  55. local keep_copies="$3"
  56. local compress="$4"
  57. log_message "Starting MongoDB log rotation"
  58. # Create rotated log file with timestamp
  59. local timestamp=$(date +%Y%m%d-%H%M%S)
  60. local rotated_log="${mongodb_log}.${timestamp}"
  61. # Copy current log to rotated file
  62. if cp "$mongodb_log" "$rotated_log"; then
  63. log_message "Created rotated log file: $rotated_log"
  64. # Truncate original log file
  65. if > "$mongodb_log"; then
  66. log_message "Truncated original log file"
  67. else
  68. log_error "Failed to truncate original log file"
  69. return 1
  70. fi
  71. # Compress rotated log file if requested
  72. if [ "$compress" = "true" ]; then
  73. if gzip "$rotated_log"; then
  74. log_message "Compressed rotated log file: ${rotated_log}.gz"
  75. else
  76. log_warning "Failed to compress rotated log file"
  77. fi
  78. fi
  79. # Clean up old rotated logs (keep only specified number)
  80. local old_logs=$(ls -t "${mongodb_log}".* 2>/dev/null | tail -n +$((keep_copies + 1)))
  81. if [ -n "$old_logs" ]; then
  82. echo "$old_logs" | xargs rm -f
  83. log_message "Cleaned up old rotated log files"
  84. fi
  85. log_success "MongoDB log rotation completed successfully"
  86. return 0
  87. else
  88. log_error "Failed to create rotated log file"
  89. return 1
  90. fi
  91. }
  92. # Show log file statistics
  93. show_log_stats() {
  94. if [ ! -f "$MONGODB_LOG" ]; then
  95. log_message "MongoDB log file not found: $MONGODB_LOG"
  96. return 1
  97. fi
  98. local log_size_mb=$(du -m "$MONGODB_LOG" | cut -f1)
  99. local log_lines=$(wc -l < "$MONGODB_LOG" 2>/dev/null || echo "0")
  100. local rotated_count=$(ls -1 "${MONGODB_LOG}".* 2>/dev/null | wc -l)
  101. log_message "MongoDB Log Statistics:"
  102. log_message " Current log size: ${log_size_mb}MB"
  103. log_message " Current log lines: ${log_lines}"
  104. log_message " Rotated log files: ${rotated_count}"
  105. log_message " Rotation threshold: ${MAX_SIZE_MB}MB"
  106. log_message " Keep copies: ${KEEP_COPIES}"
  107. }
  108. # Force rotation regardless of size
  109. force_rotation() {
  110. log_message "Force rotating MongoDB log file"
  111. if [ ! -f "$MONGODB_LOG" ]; then
  112. log_error "MongoDB log file not found: $MONGODB_LOG"
  113. return 1
  114. fi
  115. rotate_log "$MONGODB_LOG" "$MAX_SIZE_MB" "$KEEP_COPIES" "$COMPRESS_LOGS"
  116. }
  117. # Main function
  118. main() {
  119. local action="${1:-check}"
  120. case "$action" in
  121. "check")
  122. log_message "Checking if MongoDB log rotation is needed"
  123. if check_rotation_needed; then
  124. log_message "Log rotation is needed"
  125. rotate_log "$MONGODB_LOG" "$MAX_SIZE_MB" "$KEEP_COPIES" "$COMPRESS_LOGS"
  126. else
  127. log_message "Log rotation is not needed"
  128. fi
  129. ;;
  130. "force")
  131. force_rotation
  132. ;;
  133. "stats")
  134. show_log_stats
  135. ;;
  136. "help"|"-h"|"--help")
  137. echo "Usage: $0 [check|force|stats|help]"
  138. echo ""
  139. echo "Commands:"
  140. echo " check - Check if rotation is needed and rotate if so (default)"
  141. echo " force - Force rotation regardless of log size"
  142. echo " stats - Show log file statistics"
  143. echo " help - Show this help message"
  144. echo ""
  145. echo "Configuration:"
  146. echo " Log file: $MONGODB_LOG"
  147. echo " Max size: ${MAX_SIZE_MB}MB"
  148. echo " Keep copies: $KEEP_COPIES"
  149. echo " Compress: $COMPRESS_LOGS"
  150. ;;
  151. *)
  152. log_error "Unknown action: $action"
  153. echo "Use '$0 help' for usage information"
  154. exit 1
  155. ;;
  156. esac
  157. }
  158. # Run main function
  159. main "$@"