Skip to content

Instantly share code, notes, and snippets.

@henter
Created September 17, 2014 10:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henter/d4d5e6438bcc4c29aec9 to your computer and use it in GitHub Desktop.
Save henter/d4d5e6438bcc4c29aec9 to your computer and use it in GitHub Desktop.
backup nginx logs
#!/bin/bash
log_dir=/var/log/nginx/
backup_log_dir=/home/backup/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 "$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}"
#/bin/gzip "${backup_log_dir}${log_date}-${log_name}"
fi
done
#tell nginx create new log file
/bin/kill -USR1 $(cat $nginx_pid) && /bin/sleep 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment