mirror of
https://github.com/EtherDream/jsproxy
synced 2024-11-23 19:58:26 +00:00
增加日志备份脚本
This commit is contained in:
parent
7bc2896393
commit
3040a3d600
21
log-svc/README.md
Normal file
21
log-svc/README.md
Normal file
@ -0,0 +1,21 @@
|
||||
nginx 日志备份服务
|
||||
|
||||
## 说明
|
||||
|
||||
nginx 长时间运行会导致日志文件过大,该服务定期备份日志到 `backup` 目录,并进行压缩。
|
||||
|
||||
|
||||
## 依赖
|
||||
|
||||
用到了 `brotli` 压缩工具,执行 `setup-brotli.sh` 安装。
|
||||
|
||||
最终安装在 `/home/jsproxy/tools/brotli`。
|
||||
|
||||
|
||||
## 启动
|
||||
|
||||
```bash
|
||||
./svc.sh &
|
||||
```
|
||||
|
||||
使用 `jsproxy` 用户运行,无需 `root`。
|
55
log-svc/backup.sh
Executable file
55
log-svc/backup.sh
Executable file
@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env bash
|
||||
# 功能:备份 nginx 日志到 backup 目录
|
||||
|
||||
SVC_DIR=/home/jsproxy/server
|
||||
LOG_DIR=$SVC_DIR/nginx/logs
|
||||
|
||||
LOG_FILE=$LOG_DIR/proxy.log
|
||||
LOG_SIZE=$(( 32 * 1024 * 1024 ))
|
||||
|
||||
ERR_FILE=$LOG_DIR/error.log
|
||||
ERR_SIZE=$(( 1 * 1024 * 1024 * 1024 ))
|
||||
|
||||
|
||||
# error.log 达到 ERR_SIZE,开始备份(目前只清理)
|
||||
errsize=$(stat --printf=%s $ERR_FILE)
|
||||
if (( $errsize >= $ERR_SIZE )); then
|
||||
echo > $ERR_FILE
|
||||
fi
|
||||
|
||||
# proxy.log 达到 LOG_SIZE,开始备份
|
||||
logsize=$(stat --printf=%s $LOG_FILE)
|
||||
if (( $logsize < $LOG_SIZE )); then
|
||||
exit
|
||||
fi
|
||||
|
||||
logtime=$(date "+%Y-%m-%d-%H-%M-%S")
|
||||
logfile=$SVC_DIR/log-svc/backup/$logtime.log
|
||||
|
||||
#
|
||||
# 先移走日志文件,然后创建新的日志文件,通知 nginx 重新打开
|
||||
# https://www.nginx.com/resources/wiki/start/topics/examples/logrotation/
|
||||
#
|
||||
mv $LOG_FILE $logfile
|
||||
touch $LOG_FILE
|
||||
kill -USR1 $(< $LOG_DIR/nginx.pid)
|
||||
sleep 1
|
||||
|
||||
#
|
||||
# 日志压缩
|
||||
# 根据实际情况调整策略,在不影响系统的前提下,充分利用剩余 CPU
|
||||
# 可尝试其他工具(例如 7z),在开销和效果之间找一个平衡点
|
||||
#
|
||||
echo "compress $logtime ($logsize bytes)"
|
||||
|
||||
if (( $logsize > 100 * 1024 * 1024 )); then
|
||||
# 日志较大,使用快速压缩
|
||||
nice -n 19 \
|
||||
gzip $logfile
|
||||
else
|
||||
# 日志不大,使用高强度压缩
|
||||
nice -n 19 \
|
||||
~/tools/brotli $logfile --rm
|
||||
fi
|
||||
|
||||
echo "done"
|
1
log-svc/backup/README.md
Normal file
1
log-svc/backup/README.md
Normal file
@ -0,0 +1 @@
|
||||
该目录存放临时备份的日志。
|
15
log-svc/setup-brotli.sh
Normal file
15
log-svc/setup-brotli.sh
Normal file
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
# 功能:安装 brotli 压缩工具
|
||||
# 依赖:cmake(yum install -y cmake)
|
||||
|
||||
git clone --depth 1 https://github.com/google/brotli.git
|
||||
cd brotli
|
||||
|
||||
./configure-cmake
|
||||
make
|
||||
|
||||
mkdir -p ~/tools
|
||||
mv brotli ~/tools
|
||||
|
||||
cd ..
|
||||
rm -rf brotli
|
11
log-svc/svc.sh
Executable file
11
log-svc/svc.sh
Executable file
@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
# 功能:定时调用 backup.sh
|
||||
|
||||
echo "log svc running"
|
||||
|
||||
# 也可用 crontab
|
||||
while true
|
||||
do
|
||||
./backup.sh
|
||||
sleep 60
|
||||
done
|
Loading…
Reference in New Issue
Block a user