logrotate-setup 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/bin/bash
  2. # Wekan Log Rotation Setup Script
  3. # This script sets up log rotation for Wekan snap installation
  4. set -e
  5. # Configuration
  6. SNAP_NAME="wekan"
  7. SNAP_COMMON="/var/snap/${SNAP_NAME}/common"
  8. LOGROTATE_DIR="/etc/logrotate.d"
  9. WEKAN_LOGROTATE_CONF="${LOGROTATE_DIR}/${SNAP_NAME}"
  10. # Log file paths
  11. MONGODB_LOG="${SNAP_COMMON}/mongodb.log"
  12. WEKAN_APP_LOG="${SNAP_COMMON}/wekan-app.log"
  13. WEKAN_ERROR_LOG="${SNAP_COMMON}/wekan-error.log"
  14. # Log rotation configuration
  15. ROTATE_SIZE="100M"
  16. KEEP_DAYS="30"
  17. KEEP_COPIES="10"
  18. log_message() {
  19. echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
  20. }
  21. log_error() {
  22. echo "[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $1" >&2
  23. }
  24. # Check if running as root
  25. if [ "$EUID" -ne 0 ]; then
  26. log_error "This script must be run as root"
  27. exit 1
  28. fi
  29. # Check if snap is installed
  30. if ! snap list | grep -q "^${SNAP_NAME} "; then
  31. log_error "Snap ${SNAP_NAME} is not installed"
  32. exit 1
  33. fi
  34. log_message "Setting up log rotation for Wekan snap..."
  35. # Create logrotate configuration
  36. cat > "${WEKAN_LOGROTATE_CONF}" << EOF
  37. # Wekan Snap Log Rotation Configuration
  38. # Generated by wekan logrotate-setup script
  39. # MongoDB logs
  40. ${MONGODB_LOG} {
  41. daily
  42. missingok
  43. rotate ${KEEP_COPIES}
  44. compress
  45. delaycompress
  46. notifempty
  47. create 644 root root
  48. postrotate
  49. # Send SIGUSR1 to MongoDB to reopen log file
  50. if [ -f "${SNAP_COMMON}/mongodb.pid" ]; then
  51. kill -USR1 \$(cat "${SNAP_COMMON}/mongodb.pid") 2>/dev/null || true
  52. fi
  53. # Alternative: restart MongoDB service if PID file doesn't exist
  54. if [ ! -f "${SNAP_COMMON}/mongodb.pid" ]; then
  55. snap restart ${SNAP_NAME}.mongodb 2>/dev/null || true
  56. fi
  57. endscript
  58. }
  59. # Wekan application logs
  60. ${WEKAN_APP_LOG} {
  61. daily
  62. missingok
  63. rotate ${KEEP_COPIES}
  64. compress
  65. delaycompress
  66. notifempty
  67. create 644 root root
  68. postrotate
  69. # Send SIGUSR1 to Wekan application to reopen log file
  70. if [ -f "${SNAP_COMMON}/wekan.pid" ]; then
  71. kill -USR1 \$(cat "${SNAP_COMMON}/wekan.pid") 2>/dev/null || true
  72. fi
  73. # Alternative: restart Wekan service if PID file doesn't exist
  74. if [ ! -f "${SNAP_COMMON}/wekan.pid" ]; then
  75. snap restart ${SNAP_NAME}.wekan 2>/dev/null || true
  76. fi
  77. endscript
  78. }
  79. # Wekan error logs
  80. ${WEKAN_ERROR_LOG} {
  81. daily
  82. missingok
  83. rotate ${KEEP_COPIES}
  84. compress
  85. delaycompress
  86. notifempty
  87. create 644 root root
  88. postrotate
  89. # Send SIGUSR1 to Wekan application to reopen log file
  90. if [ -f "${SNAP_COMMON}/wekan.pid" ]; then
  91. kill -USR1 \$(cat "${SNAP_COMMON}/wekan.pid") 2>/dev/null || true
  92. fi
  93. # Alternative: restart Wekan service if PID file doesn't exist
  94. if [ ! -f "${SNAP_COMMON}/wekan.pid" ]; then
  95. snap restart ${SNAP_NAME}.wekan 2>/dev/null || true
  96. fi
  97. endscript
  98. }
  99. # Size-based rotation for large log files
  100. ${SNAP_COMMON}/*.log {
  101. size ${ROTATE_SIZE}
  102. missingok
  103. rotate ${KEEP_COPIES}
  104. compress
  105. delaycompress
  106. notifempty
  107. create 644 root root
  108. sharedscripts
  109. postrotate
  110. # Generic postrotate for all log files
  111. # Try to signal processes to reopen log files
  112. for pidfile in "${SNAP_COMMON}"/*.pid; do
  113. if [ -f "\$pidfile" ]; then
  114. kill -USR1 \$(cat "\$pidfile") 2>/dev/null || true
  115. fi
  116. done
  117. # Restart services if no PID files found
  118. if [ ! -f "${SNAP_COMMON}/mongodb.pid" ] && [ ! -f "${SNAP_COMMON}/wekan.pid" ]; then
  119. snap restart ${SNAP_NAME}.mongodb 2>/dev/null || true
  120. snap restart ${SNAP_NAME}.wekan 2>/dev/null || true
  121. fi
  122. endscript
  123. }
  124. EOF
  125. log_message "Created logrotate configuration: ${WEKAN_LOGROTATE_CONF}"
  126. # Test logrotate configuration
  127. if logrotate -d "${WEKAN_LOGROTATE_CONF}" >/dev/null 2>&1; then
  128. log_message "Logrotate configuration test passed"
  129. else
  130. log_error "Logrotate configuration test failed"
  131. exit 1
  132. fi
  133. # Create log directory if it doesn't exist
  134. mkdir -p "${SNAP_COMMON}"
  135. # Set proper permissions
  136. chown root:root "${WEKAN_LOGROTATE_CONF}"
  137. chmod 644 "${WEKAN_LOGROTATE_CONF}"
  138. # Create initial log files if they don't exist
  139. touch "${MONGODB_LOG}" "${WEKAN_APP_LOG}" "${WEKAN_ERROR_LOG}"
  140. chown root:root "${SNAP_COMMON}"/*.log
  141. chmod 644 "${SNAP_COMMON}"/*.log
  142. log_message "Log rotation setup completed successfully"
  143. log_message "Configuration file: ${WEKAN_LOGROTATE_CONF}"
  144. log_message "Log files will be rotated daily and when they exceed ${ROTATE_SIZE}"
  145. log_message "Log files will be kept for ${KEEP_DAYS} days (${KEEP_COPIES} copies)"
  146. log_message "To test log rotation manually: sudo logrotate -f ${WEKAN_LOGROTATE_CONF}"
  147. log_message "To view logrotate status: sudo logrotate -d ${WEKAN_LOGROTATE_CONF}"
  148. echo ""
  149. echo "Next steps:"
  150. echo "1. Configure MongoDB to log to file: sudo snap set ${SNAP_NAME} mongo-log-destination=snapcommon"
  151. echo "2. Configure Wekan to log to file (if not already done)"
  152. echo "3. Test log rotation: sudo logrotate -f ${WEKAN_LOGROTATE_CONF}"
  153. echo "4. Monitor log files: ls -la ${SNAP_COMMON}/*.log*"