File tree Expand file tree Collapse file tree 3 files changed +40
-2
lines changed
Expand file tree Collapse file tree 3 files changed +40
-2
lines changed Original file line number Diff line number Diff line change 11/var/log/openvpn/*.log {
22 daily
3- rotate 90
3+ rotate 7
44 compress
55 missingok
66 copytruncate
77 notifempty
8- create 644 nobody nobody
8+ dateext
9+ dateformat -%Y-%m-%d-daily
910}
11+
Original file line number Diff line number Diff line change 1+ #! /bin/bash
2+
3+ LOG_DIR=" /var/log/openvpn"
4+ LOG_FILE=" openvpn.log"
5+ ARCHIVE_DIR=" /var/log/openvpn/weekly-logs"
6+ WEEKLY_ARCHIVE_NAME=" openvpn-$( date +\% Y-\% m-\% d) _weekly_logs.tar.gz"
7+ RETENTION_DAYS=365 # Keep weekly archives for a year
8+ TEMP_DIR=$( mktemp -d) # Temporary folder for decompressed logs
9+
10+ # Clean the temporary folder in case of failure
11+ trap ' rm -rf "$TEMP_DIR"' ERR
12+
13+ # Create archive directory if it doesn't exist
14+ mkdir -p " $ARCHIVE_DIR "
15+
16+ # Decompress all rotated daily logs (mylogfile.log.1.gz, mylogfile.log.2.gz, etc.)
17+ for file in $( find " $LOG_DIR " -name " $LOG_FILE -*" ) ;
18+ do gzip -d -c $file > " $TEMP_DIR /$( basename $file .gz) " ;
19+ done
20+
21+ # Create a compressed archive with all daily logs from the past week
22+ tar -czf " $ARCHIVE_DIR /$WEEKLY_ARCHIVE_NAME " -C " $TEMP_DIR " .
23+
24+ # Cleanup temporary files
25+ rm -rf " $TEMP_DIR "
26+
27+ # Delete archives older than a year
28+ find " $ARCHIVE_DIR " -name " *.tar.gz" -mtime +$RETENTION_DAYS -delete
Original file line number Diff line number Diff line change 1313 dest : /etc/logrotate.d/openvpn
1414 notify : restart rsyslog
1515
16+ - name : Create weekly log rotation script
17+ copy :
18+ src : files/openvpn_weekly_rotation
19+ dest : /etc/cron.weekly/openvpn_weekly_rotation
20+ mode : ' 0755'
21+ owner : root
22+ group : root
23+
1624- name : Write rsyslog conf file
1725 copy :
1826 src : files/openvpn.rsyslog
You can’t perform that action at this time.
0 commit comments