Problems in the project
Server appeared one day morning caton is extremely serious, page load, slow, and some page refresh 404 problems, even the server TAB command automatically suggests there is a problem, the original poster had a try, according to the server screen found that data server disk occupied 100% of the problems, the cause of the problem is, Jenkins automatically backed up the previous WAR each time he deployed the server, and as a result of frequent deployments during the development phase, the hard drive eventually became full, resulting in the situation described above.
The implementation process of the solution
Get all files in the backup folder
Following a hint from Google’s dad, the creator found the following command,
Find Corresponding directory -mtime + number of days -name"File name" -exec rm -rf {} \;
Copy the code
Example command:
find /opt/soft/log/ -mtime +30 -name "*.log" -exec rm -rf {} \;
Copy the code
Description:
Delete all files that contain.log 30 days ago from the /opt/soft/log/ directory.
The parameters are described as follows:
Find: Linux find command. The user searches for a specified file. /opt/soft/log/ : Any directory that you want to clean. -mtime: standard statement. +30: Find files 30 days ago, where numbers represent days; “×.log” : the type of data that you want to find, “×.jpg” means to find all files with the extension JPG, “×” means to find all files, this can be used in a variety of ways. -exec: fixed; Rm -rf: forcibly deletes files, including directories. {}; : fixed, a pair of braces + space ++;
Solutions to the problem:
Of course, you can’t delete all the files in the backup directory foolishly, so that the backup will not be meaningless. So a change of thought leads to the following command
find ${BAK_HOME} -mtime +1 -name : "* *" | wc -l
Copy the code
Description:
Obtain the number of all files in the backup directory with “:” one day ago.
find ${BAK_HOME} -mtime +1 -name : "* *"
Copy the code
Description:
Obtain the number of all files in the backup directory that contain a colon one day ago.
This is pretty much the end of our problem. So, please continue to read:
Solution ideas and shell script implementation
Train of thought
To solve this problem, add a script to the original deployment script to save the backup records of the last 10 deployments and delete the backup records of more than 10 deployments.
Implementation of shell scripts
Logic is very clear, the idea is very clear, I will not continue to 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}- 1echo "delete backup file"
else
echo "do no thing"
fi
done
Copy the code