Skip to content

Instantly share code, notes, and snippets.

@dishuostec
Created February 26, 2015 05:41
Show Gist options
  • Save dishuostec/b6982b51c6c8ffe73262 to your computer and use it in GitHub Desktop.
Save dishuostec/b6982b51c6c8ffe73262 to your computer and use it in GitHub Desktop.
#!/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