Shell script realizes automatic retention of the latest n backup records

problems in the project

One morning, the server was stuck very seriously, the page loading speed was extremely slow, and some pages refreshed with a 404 problem, and even the automatic prompt of the server's tab command had problems. The landlord spent a lot of effort. According to the server investigation, it is found that the server data disk is 100% occupied. The reason for this problem is that every time Jenkins deploys the server, it will automatically back up the last war. Due to the frequent deployment in the development phase, the final hard disk When it is full, the situation described above occurs.

Solution implementation process

Get all files under backup folder

According to Google's father's prompt, the landlord found the following command,

find 对应目录 -mtime +天数 -name "文件名" -exec rm -rf {} \;

Example command:

find /opt/soft/log/ -mtime +30 -name "*.log" -exec rm -rf {} \;

illustrate:

Delete all files with ".log" 30 days ago in the /opt/soft/log/ directory.

The specific parameters are described as follows:

find: linux search command, the user finds files with specified conditions;
/opt/soft/log/: any directory you want to clean up;
-mtime: standard sentence writing;
+30: find files 30 days ago, use numbers here Represents the number of days;
" ×.log": the data type you want to find, "×.jpg" means to find all files with the extension jpg, "×" means to find all files, this can be used flexibly, and infer other things;
-exec: fixed writing method ;
rm -rf: forcibly delete files, including directories;
{} \; : fixed writing, a pair of braces + space++;

Problem-solving ideas:

Of course, the landlord can't stupidly delete all the files in the backup directory. In this case, the backup will lose its meaning.
So change your thinking and you have the following command

find ${BAK_HOME} -mtime +1 -name "*:*" | wc -l

illustrate:

Get the number of all files with ":" in the backup directory one day ago.

find ${BAK_HOME} -mtime +1 -name "*:*"

illustrate:

Get the number of all files with ":" in the backup directory one day ago.

At this point our problem is almost solved. so, please read on:

The idea of ​​the solution and the realization of shell script

ideas

The current solution to this problem is to add a script to the original deployment script to keep the backup records of the last 10 deployments, and the backup records of more than 10 deployments will be deleted.

Implementation of shell script

The logic is very clear, the thinking is very clear, I will not elaborate here, thank you!

#!/bin/sh
BAK_HOME="/home/saveHistoryData/iam-share-8083"

keepNum=5
fileNum=$(find ${BAK_HOME} -mtime +1 -name "*:*" | wc -l)

echo "${fileNum}"

for file in $(find ${BAK_HOME} -mtime +1 -name "*:*"); do
    if test $[fileNum] -gt $[keepNum];then
       rm -rf ${file}
       fileNum=${fileNum}-1
       echo "delete backup file"
    else
       echo "do no thing"
    fi
done 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325949661&siteId=291194637