Last active
December 16, 2015 14:39
-
-
Save nothinking/5450475 to your computer and use it in GitHub Desktop.
bash: data remove: 데이타 수집서버나 mysql 리플리케이션 Master 서버의 경우 지속적으로 파일이 추가되고 늘어나기 때문에 주기적인 디스크 정리 정책이 없으면 금새 디스크 풀 의 상태에 도달하게 된다.이런 상황에서 간단하게 사용할 수 있는 디스크 정리 스크립트를 소개한다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#데이타가 쌓이는 디렉토리가 /data1/crawl_data 라면 아래 스크립트는 최근 300 개만 남기고 파일을 지운다. | |
#!/bin/bash | |
DATE=`date +'%Y%m%d_%H%m%S'` ## 로그 기록을 위해 작업시간을 기록한다. | |
BACKUPCOUNT=300 ## 최근 300 개만 남기고 지운다. | |
WORKDIR=/data1/crawl_data | |
dirlist=`/bin/ls -t $WORKDIR` | |
i=1; | |
for fd in $dirlist; do | |
if [ "$i" -ge $BACKUPCOUNT ]; then | |
#echo $i $fd | |
rm -f $WORKDIR/$fd | |
fi | |
i=$(($i+1)) | |
done | |
#디스크사용량을 체크하여 일정수준 이상이 되면 가장 오래된 파일을 삭제한다. | |
#!/bin/sh | |
check_disk=/data20/ftplog | |
cut_usage=80 | |
while [ `df -h $check_disk|cut -c40-42|grep -i [^a-z]|bc` -ge $cut_usage ] | |
do | |
fd=`/bin/ls -tr $check_disk | head -n1 2>/dev/null` | |
echo $fd | |
rm $check_disk/$fd | |
done | |
#mysql 에는 max_binlog_size 로 로그파일을 일정크기로 제한하는 설정이 있다. 로그파일이 이 크기에 도달하게 되면 다음 로그파일에 기록하게 된다. 디폴트는 1G(1073741824) 이다. | |
#!/bin/sh | |
DATE=`date '+%Y-%m-%d %H:%M:%S' --date '1 day ago'` | |
echo $DATE | |
/usr/local/mysql/bin/mysql -vv -uroot -ppassword --execute="PURGE BINARY LOGS BEFORE '${DATE}'"; | |
# 작성되거나 변경된지 30일 이상 지난 파일을 모두 지울려면 | |
find . -mtime +30 -exec rm -f {} \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment