Skip to content

Commit fcd39aa

Browse files
authored
Merge pull request #774 from ceph/wip-openvpn-logrotate
OpenVPN logrotate configuration to keep the logs for a year.
2 parents 9b5a79b + 8d9ae41 commit fcd39aa

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

roles/gateway/files/openvpn.logrotate

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
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+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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

roles/gateway/tasks/logging.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
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

0 commit comments

Comments
 (0)