Skip to content

Commit 4418ffc

Browse files
committed
log rotate fixes
1 parent f02980d commit 4418ffc

File tree

1 file changed

+24
-65
lines changed

1 file changed

+24
-65
lines changed

nginx-f1/logrotate-manager.sh

Lines changed: 24 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ REMOTE_HOST=${LOGROTATE_REMOTE_HOST:-}
1010
REMOTE_USER=${LOGROTATE_REMOTE_USER:-}
1111
REMOTE_PATH=${LOGROTATE_REMOTE_PATH:-/var/log/nginx-archive}
1212
REMOTE_METHOD=${LOGROTATE_REMOTE_METHOD:-scp} # scp, sftp, rsync, s3
13-
COMPRESS_ARCHIVE=${LOGROTATE_COMPRESS_ARCHIVE:-true}
1413
CLEANUP_AFTER_REMOTE=${LOGROTATE_CLEANUP_AFTER_REMOTE:-false}
1514
ENABLE_ARCHIVE=${LOGROTATE_ENABLE_ARCHIVE:-true}
1615
ENABLE_REMOTE=${LOGROTATE_ENABLE_REMOTE:-false}
@@ -61,37 +60,19 @@ archive_logs() {
6160

6261
create_archive_dir
6362

64-
# Move rotated logs to archive directory
63+
# Move and gzip rotated logs individually to archive directory
6564
for log_file in $rotated_logs; do
6665
if [ -f "$log_file" ]; then
6766
local filename=$(basename "$log_file")
6867
local archive_file="$ARCHIVE_DIR/${filename}.$(date +%Y%m%d_%H%M%S)"
69-
70-
# Move the file to archive directory
68+
7169
mv "$log_file" "$archive_file"
72-
chown www-data:www-data "$archive_file"
73-
74-
log_message "Archived: $log_file -> $archive_file"
70+
gzip "$archive_file"
71+
chown www-data:www-data "${archive_file}.gz"
72+
73+
log_message "Archived: $log_file -> ${archive_file}.gz"
7574
fi
7675
done
77-
78-
# Create compressed archive if enabled
79-
if [ "$COMPRESS_ARCHIVE" = "true" ]; then
80-
local archive_name="nginx-logs-$(date +%Y%m%d).tar.gz"
81-
local archive_path="$ARCHIVE_DIR/$archive_name"
82-
83-
# Create compressed archive of today's logs
84-
cd "$ARCHIVE_DIR"
85-
local today=$(date +%Y%m%d)
86-
tar -czf "$archive_path" *."${today}"_* 2>/dev/null || true
87-
88-
if [ -f "$archive_path" ]; then
89-
chown www-data:www-data "$archive_path"
90-
# Remove individual files after successful tar
91-
rm -f *."${today}"_*
92-
log_message "Created compressed archive: $archive_path"
93-
fi
94-
fi
9576
}
9677

9778
# Function to transfer logs to remote location
@@ -141,20 +122,13 @@ transfer_via_scp() {
141122
# Create remote date-based directory
142123
ssh $ssh_opts "$REMOTE_USER@$REMOTE_HOST" "mkdir -p $remote_dest"
143124

144-
# Transfer archive files
145-
if [ "$COMPRESS_ARCHIVE" = "true" ]; then
146-
local archive_name="nginx-logs-$(date +%Y%m%d).tar.gz"
147-
local archive_path="$ARCHIVE_DIR/$archive_name"
148-
149-
if [ -f "$archive_path" ]; then
150-
scp $ssh_opts "$archive_path" "$REMOTE_USER@$REMOTE_HOST:$remote_dest/"
151-
log_message "Transferred compressed archive to remote: $remote_dest/$archive_name"
125+
# Transfer archived files
126+
for f in "$ARCHIVE_DIR"/*.gz; do
127+
if [ -f "$f" ]; then
128+
scp $ssh_opts "$f" "$REMOTE_USER@$REMOTE_HOST:$remote_dest/"
129+
log_message "Transferred: $remote_dest/$(basename $f)"
152130
fi
153-
else
154-
# Transfer individual files
155-
scp $ssh_opts "$ARCHIVE_DIR"/* "$REMOTE_USER@$REMOTE_HOST:$remote_dest/"
156-
log_message "Transferred individual log files to remote: $remote_dest/"
157-
fi
131+
done
158132
}
159133

160134
# Function to transfer via SFTP (batch mode — works with SFTP-only servers)
@@ -178,23 +152,14 @@ transfer_via_sftp() {
178152
echo "-mkdir $REMOTE_PATH/$(date +%Y-%m)" >> "$batch_file"
179153
echo "-mkdir $remote_dest" >> "$batch_file"
180154

181-
# Add files to upload
155+
# Add archived files to upload
182156
local file_count=0
183-
if [ "$COMPRESS_ARCHIVE" = "true" ]; then
184-
for f in "$ARCHIVE_DIR"/nginx-logs-*.tar.gz; do
185-
if [ -f "$f" ]; then
186-
echo "put $f $remote_dest/" >> "$batch_file"
187-
file_count=$((file_count + 1))
188-
fi
189-
done
190-
else
191-
for f in "$ARCHIVE_DIR"/*; do
192-
if [ -f "$f" ]; then
193-
echo "put $f $remote_dest/" >> "$batch_file"
194-
file_count=$((file_count + 1))
195-
fi
196-
done
197-
fi
157+
for f in "$ARCHIVE_DIR"/*.gz; do
158+
if [ -f "$f" ]; then
159+
echo "put $f $remote_dest/" >> "$batch_file"
160+
file_count=$((file_count + 1))
161+
fi
162+
done
198163

199164
if [ "$file_count" -eq 0 ]; then
200165
log_message "No files to transfer via SFTP"
@@ -252,18 +217,12 @@ transfer_via_s3() {
252217
# Upload to S3
253218
local s3_path="s3://$S3_BUCKET/$S3_PREFIX/$(date +%Y/%m/%d)/"
254219

255-
if [ "$COMPRESS_ARCHIVE" = "true" ]; then
256-
local archive_name="nginx-logs-$(date +%Y%m%d).tar.gz"
257-
local archive_path="$ARCHIVE_DIR/$archive_name"
258-
259-
if [ -f "$archive_path" ]; then
260-
aws s3 cp "$archive_path" "$s3_path"
261-
log_message "Uploaded compressed archive to S3: $s3_path$archive_name"
220+
for f in "$ARCHIVE_DIR"/*.gz; do
221+
if [ -f "$f" ]; then
222+
aws s3 cp "$f" "$s3_path"
223+
log_message "Uploaded to S3: $s3_path$(basename $f)"
262224
fi
263-
else
264-
aws s3 sync "$ARCHIVE_DIR/" "$s3_path"
265-
log_message "Synchronized logs to S3: $s3_path"
266-
fi
225+
done
267226
}
268227

269228
# Function to cleanup local archives after remote transfer

0 commit comments

Comments
 (0)