shell script that finds log files 

Note that this script requires the AWS CLI to be installed and configured in order to use the aws s3 cp command. Also, The first line of the script, #!/bin/bash, is called the shebang. It tells the shell which interpreter to use to run the script, which in this case is Bash.
It is necessary if you want to make script executable and run it without specifying the interpreter.

Here’s a sample shell script that finds log files older than 30 days, archives them using tar, and then uploads them to an S3 bucket:

#!/bin/bash

# Define the log directory and the S3 bucket
LOG_DIR="/path/to/logs"
S3_BUCKET="s3://my-bucket"

# Find log files older than 30 days
find $LOG_DIR -type f -mtime +30 -name "*.log" > /tmp/old_logs.txt

# Check if there are any old logs to archive
if [ -s /tmp/old_logs.txt ]
then
  # Create an archive of the old logs
  tar -czvf /tmp/old_logs.tar.gz -T /tmp/old_logs.txt

  # Upload the archive to the S3 bucket
  aws s3 cp /tmp/old_logs.tar.gz $S3_BUCKET

  # Delete the old log files
  xargs rm < /tmp/old_logs.txt
fi

# delete temp file 
rm /tmp/old_logs.txt


This script uses the find command to locate log files in the specified directory that are older than 30 days, and writes the file names to a text file called /tmp/old_logs.txt. If there are any old logs found, the script creates an archive of them using tar, and then uploads the archive to the specified S3 bucket using the aws s3 cp command. The script will finally remove the old logs file and temp file which contains the list of file names.

You can also customize this script to specify the number of days after which logs will be considered old by changing the value passed to the -mtime option of the find command, and to specify the path of logs directory and the S3 bucket as per your requirement.

Total
0
Shares
კომენტარის დატოვება

თქვენი ელფოსტის მისამართი გამოქვეყნებული არ იყო. აუცილებელი ველები მონიშნულია *

Previous Post

Run commands on your Linux instance at launch

Next Post

backup a database every day using Bash

Related Posts

SSH Server-ის უსაფრთხოება ნაწილი #2

წინა გაკვეთილში განვიხილეთ სხვადასხვა მეთოდები  SSH Server-ის უსაფრთხოებისათვის ხოლო დღეს გადავდივართ შემდეგ გაკვეთილზე და დაწვრილებით განვიხილავთ ამ გაკვეთილისათვისა…
სრულად წაკითხვა