Skip to content

Commit

Permalink
Merge pull request #30 from rampantvoid/rampantvoid
Browse files Browse the repository at this point in the history
Fixes #9
  • Loading branch information
anshikasrivastava17 authored Jul 22, 2024
2 parents 1b53ab2 + f2ff9c3 commit 2f856c5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Day 2/Issue 9/Priyanshu/log_archiver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

# Configuration
LOG_SOURCE_DIR="./log"
ARCHIVE_DIR="./archive/logs"
DAYS_OLD=2
SCRIPT_PATH=$(readlink -f "$0")
LOCK_FILE="/tmp/log_archiver.lock"

# Function to perform log archiving
perform_archiving() {

mkdir -p "$ARCHIVE_DIR"

CURRENT_DATE=$(date +"%Y-%m-%d")


ARCHIVE_FILE="logs_${CURRENT_DATE}.tar.gz"

find "$LOG_SOURCE_DIR" -type f -mtime +$DAYS_OLD -print0 | tar czvf "$ARCHIVE_DIR/$ARCHIVE_FILE" --null -T -

find "$LOG_SOURCE_DIR" -type f -mtime +$DAYS_OLD -delete

echo "Log archiving completed. Archive saved as $ARCHIVE_DIR/$ARCHIVE_FILE"
}

if [ -e "$LOCK_FILE" ]; then
echo "Script is already running. Exiting."
exit 1
fi

touch "$LOCK_FILE"

perform_archiving

at now + 2 days <<EOF
$SCRIPT_PATH
EOF

rm "$LOCK_FILE"

echo "Next run scheduled in 2 days."
14 changes: 14 additions & 0 deletions Day 2/Issue 9/Priyanshu/start_log_archiver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

LOG_ARCHIVER_SCRIPT="./log_archiver.sh"

if [ ! -f "$LOG_ARCHIVER_SCRIPT" ]; then
echo "Error: Log archiver script not found at $LOG_ARCHIVER_SCRIPT"
exit 1
fi

chmod +x "$LOG_ARCHIVER_SCRIPT"

"$LOG_ARCHIVER_SCRIPT"

echo "Log archiver initiated. It will run every 2 days automatically."

0 comments on commit 2f856c5

Please sign in to comment.