@@ -51,8 +51,8 @@ archive_logs() {
5151
5252 log_message " Starting log archiving process"
5353
54- # Find recently rotated logs (compressed files from today )
55- local rotated_logs=$( find /var/log/nginx -name " *.log.*" -type f -mtime -1 2> /dev/null || true)
54+ # Find recently rotated logs (top-level only, skip archive dir )
55+ local rotated_logs=$( find /var/log/nginx -maxdepth 1 - name " *.log.*" -type f -mtime -1 2> /dev/null || true)
5656
5757 if [ -z " $rotated_logs " ]; then
5858 log_message " No rotated logs found to archive"
@@ -82,10 +82,13 @@ archive_logs() {
8282
8383 # Create compressed archive of today's logs
8484 cd " $ARCHIVE_DIR "
85- tar -czf " $archive_path " * .$( date +%Y%m%d_* ) 2> /dev/null || true
85+ local today=$( date +%Y%m%d)
86+ tar -czf " $archive_path " * ." ${today} " _* 2> /dev/null || true
8687
8788 if [ -f " $archive_path " ]; then
8889 chown www-data:www-data " $archive_path "
90+ # Remove individual files after successful tar
91+ rm -f * ." ${today} " _*
8992 log_message " Created compressed archive: $archive_path "
9093 fi
9194 fi
@@ -132,23 +135,25 @@ transfer_via_scp() {
132135 fi
133136
134137 local ssh_opts=" -i $SSH_KEY_PATH -p $SSH_PORT -o StrictHostKeyChecking=no"
135-
136- # Create remote directory if it doesn't exist
137- ssh $ssh_opts " $REMOTE_USER @$REMOTE_HOST " " mkdir -p $REMOTE_PATH "
138-
138+ local date_subdir=" $( date +%Y-%m) /$( date +%d) "
139+ local remote_dest=" $REMOTE_PATH /$date_subdir "
140+
141+ # Create remote date-based directory
142+ ssh $ssh_opts " $REMOTE_USER @$REMOTE_HOST " " mkdir -p $remote_dest "
143+
139144 # Transfer archive files
140145 if [ " $COMPRESS_ARCHIVE " = " true" ]; then
141146 local archive_name=" nginx-logs-$( date +%Y%m%d) .tar.gz"
142147 local archive_path=" $ARCHIVE_DIR /$archive_name "
143-
148+
144149 if [ -f " $archive_path " ]; then
145- scp $ssh_opts " $archive_path " " $REMOTE_USER @$REMOTE_HOST :$REMOTE_PATH /"
146- log_message " Transferred compressed archive to remote: $archive_path "
150+ scp $ssh_opts " $archive_path " " $REMOTE_USER @$REMOTE_HOST :$remote_dest /"
151+ log_message " Transferred compressed archive to remote: $remote_dest / $archive_name "
147152 fi
148153 else
149154 # Transfer individual files
150- scp $ssh_opts " $ARCHIVE_DIR " /* " $REMOTE_USER @$REMOTE_HOST :$REMOTE_PATH /"
151- log_message " Transferred individual log files to remote"
155+ scp $ssh_opts " $ARCHIVE_DIR " /* " $REMOTE_USER @$REMOTE_HOST :$remote_dest /"
156+ log_message " Transferred individual log files to remote: $remote_dest / "
152157 fi
153158}
154159
@@ -161,27 +166,31 @@ transfer_via_sftp() {
161166
162167 local sftp_opts=" -P $SSH_PORT -i $SSH_KEY_PATH -o StrictHostKeyChecking=no -o BatchMode=yes"
163168 local batch_file=$( mktemp /tmp/sftp-batch-XXXXXX)
169+ local date_subdir=" $( date +%Y-%m) /$( date +%d) "
170+ local remote_dest=" $REMOTE_PATH /$date_subdir "
164171
165- # Create remote directories (dash prefix = ignore errors if already exists)
172+ # Create remote date-based directories (dash prefix = ignore errors if already exists)
166173 local parent_dir=$( dirname " $REMOTE_PATH " )
167174 if [ " $parent_dir " != " /" ] && [ " $parent_dir " != " ." ]; then
168175 echo " -mkdir $parent_dir " >> " $batch_file "
169176 fi
170177 echo " -mkdir $REMOTE_PATH " >> " $batch_file "
178+ echo " -mkdir $REMOTE_PATH /$( date +%Y-%m) " >> " $batch_file "
179+ echo " -mkdir $remote_dest " >> " $batch_file "
171180
172181 # Add files to upload
173182 local file_count=0
174183 if [ " $COMPRESS_ARCHIVE " = " true" ]; then
175184 for f in " $ARCHIVE_DIR " /nginx-logs-* .tar.gz; do
176185 if [ -f " $f " ]; then
177- echo " put $f $REMOTE_PATH /" >> " $batch_file "
186+ echo " put $f $remote_dest /" >> " $batch_file "
178187 file_count=$(( file_count + 1 ))
179188 fi
180189 done
181190 else
182191 for f in " $ARCHIVE_DIR " /* ; do
183192 if [ -f " $f " ]; then
184- echo " put $f $REMOTE_PATH /" >> " $batch_file "
193+ echo " put $f $remote_dest /" >> " $batch_file "
185194 file_count=$(( file_count + 1 ))
186195 fi
187196 done
@@ -213,9 +222,11 @@ transfer_via_rsync() {
213222 fi
214223
215224 local rsync_opts=" -avz -e 'ssh -i $SSH_KEY_PATH -p $SSH_PORT -o StrictHostKeyChecking=no'"
216-
217- eval " rsync $rsync_opts $ARCHIVE_DIR / $REMOTE_USER @$REMOTE_HOST :$REMOTE_PATH /"
218- log_message " Synchronized logs to remote via rsync"
225+ local date_subdir=" $( date +%Y-%m) /$( date +%d) "
226+ local remote_dest=" $REMOTE_PATH /$date_subdir "
227+
228+ eval " rsync $rsync_opts $ARCHIVE_DIR / $REMOTE_USER @$REMOTE_HOST :$remote_dest /"
229+ log_message " Synchronized logs to remote via rsync: $remote_dest /"
219230}
220231
221232# Function to transfer via S3
0 commit comments