Shell script ------------ Clean up log files regularly

Target requirements:

There are more than 300 machines of the two types. Write a script to automatically clean up the log files in the two types of machines. The log storage path of the A and B machines is the same, but the log file storage path of the B machine needs to match (in addition to the log and Other files cannot be deleted, use .log when matching )

Type A: /opt/cloud/log/ (Delete logs 7 days ago)
Type B: /opt/cloud/instances/ (Delete logs 15 days ago)

No need to consider operations on the bastion machine, just write a shell script.

Step analysis:

  1. You need to write one, which can run on both A and B machines.
  2. To determine whether it is a type A or B machine, you can use the judgment directory /opt/cloud/log or /opt/cloud/instances directory to determine which type of machine.
  3. For a class A machine, you can find it directly, while a class B machine needs to find -name "*.log" which is a little cumbersome.
  4. After finding the log file, pass it to rm to delete it.
#!/bin/bash
dir1=/opt/cloud/instances/
dir2=/opt/cloud/log/
if [ -d $dir1 ]
then
      find $dir1 -type f -name "*.log" -mtime +15 |xargs rm -f
      elif [ -d $dir2 ]
      then
           find $dir2 -type f -mtime +7 |xargs rm -f
fi

Notes:

#####mtime parameters should be understood as follows#####

-mtime n Find files according to the time of file change, n is an integer.
n means the file change time distance is n days, -n means the file change time distance is within n days, +n means the file change time distance is n days ago

E.g:

  • -mtime 0 means that the file modification time is 0 days from the current file, that is, the file is less than 1 day (24 hours) from the current time.
  • mtime 1 means that the file modification time is 1 day away from the current time, that is, the file is 1 day (24 hours-48 hours) away from the current time.
  • mtime +1 means files whose modification time is greater than 1 day, that is, files that are 2 days (48 hours) away from the current time
  • mtime -1 means that the file modification time is less than 1 day, that is, the file is within 1 day (24 hours) from the current time.

####xargs####
xargs is a filter for passing parameters to a command and a tool for combining multiple commands.

  • xargs can convert pipe or standard input (stdin) data into command line parameters, and it can also get data from file output.
  • xargs can also convert single-line or multi-line text input into other formats, such as multi-line to single-line, and single-line to multi-line.
  • xargs is a powerful command that can capture the output of one command and pass it to another command.

The key reason why this command can be used is that many commands do not support "|" pipes to transfer parameters, but in daily work, there is the xargs command,
for example:
find /sbin -perm +700 |ls -l #
#This command is wrong find /sbin -perm +700 |xargs ls -l ##This command is correct

Guess you like

Origin blog.csdn.net/weixin_48190875/article/details/108240289