Created
February 26, 2015 05:41
-
-
Save dishuostec/b6982b51c6c8ffe73262 to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/bash | |
log_dir=/usr/local/nginx/logs/ | |
backup_log_dir=/data/nginx_logs/$(date +"%Y%m")/ | |
log_date=$(date +"%Y%m%d") | |
nginx_pid=/var/run/nginx.pid | |
keep_days=30 | |
echo "processing ${log_dir}" | |
#create backup dir | |
mkdir -p $backup_log_dir | |
#remove old logs | |
find "$backup_log_dir" -name "*\.log.gz" -type f -mtime +"${keep_days}" -exec rm -rf {} \; | |
#backup logs | |
for log_name in `ls "$log_dir" | awk '/.log$/'`;do | |
echo "${log_name}" | |
if [ -e "${backup_log_dir}${log_date}-${log_name}" ];then | |
echo "${backup_log_dir}${log_date}-${log_name} Already exists" && continue | |
else | |
/bin/mv "${log_dir}${log_name}" "${backup_log_dir}${log_date}-${log_name}" | |
fi | |
done | |
#tell nginx create new log file | |
/bin/kill -USR1 $(cat $nginx_pid) && /bin/sleep 1 | |
#compress log file | |
find "$backup_log_dir" -name "*\.log" -type f -exec /bin/gzip {} \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment