项目运行久了系统盘就会越来越大,此时就要将一些业务迁移到其它服务器,购买商业数据库等服务。但对于小公司通常还是希望尽量保持较低成本,此时挂载数据盘,数据备份迁移也是常见的任务。因国内监管要求保存6个月的日志,本文以Nginx日志迁移为例,针对部署初期即挂载了数据盘或在运行一段时间后再挂载数据盘的场景。
关于挂载,可以参见一篇旧文:阿里云ECS Linux服务器如何实现挂载
比如我们将数据盘挂载到/data/目录下,创建Nginx日志目录:
|
1 |
sudo mkdir -p /data/log/nginx |
接下来查看Nginx的用户:
|
1 |
ps aux | grep "nginx: worker process" | awk '{print $1}' | head -n 1 |
一般情况下要么是nginx要么是www-data,根据输出设置日志目录的用户:
|
1 2 3 |
sudo chown -R www-data:adm /data/log/nginx/ # 或 sudo chown -R nginx:nginx /data/log/nginx/ |
接下来编辑/etc/nginx/nginx.conf:
|
1 2 3 4 5 6 7 8 |
http { # ... other settings ... access_log /data/log/nginx/access.log; error_log /data/log/nginx/error.log; # ... other settings ... } |
当然你也可以按server来进行配置,接下来检测配置sudo nginx -t,没有问题则重启Nginx:
|
1 2 3 |
sudo systemctl reload nginx # 或 sudo nginx -s reload |
注意至此并未结束,因为一般我们会轮询日志,进行压缩存取,此时添加一个/etc/logrotate.d/data-nginx文件(建议直接通过/etc/logrotate.d/nginx拷贝):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/data/log/nginx/*.log { daily missingok rotate 14 compress delaycompress notifempty create 0640 www-data adm sharedscripts postrotate if [ -f /var/run/nginx.pid ]; then kill -USR1 `cat /var/run/nginx.pid` fi endscript } |
原目录的文件可以拷贝到新目录或另行备份存取,至此操作结束。