LoginSignup
3
2

More than 5 years have passed since last update.

容量が多いフォルダ・ファイルを特定&logrotateの設定

Posted at

サーバの容量が足りなくなりRailsのアプリケーションが落ちてしまったので、原因調査した時の記録。

容量が多いフォルダを特定する

コマンド

sudo du -scmh /* | sort -rn

出力結果

503M    /home
118M    /root
84M /lib
56M /opt
31M /boot
23M /lib64
16K /lost+found
・・・

容量が多いフォルダに対して上記コマンドを続けていく。

容量が多いファイルを特定する

ファイルがあるフォルダまで到達したら、以下コマンドで容量が多いファイルからソートする。

ls -hS

※-h:容量をMG単位で表示
※-S:容量が大きい順にソート

$ ll -hS
合計 1.7G
-rw-r--r-- 1 root root 324M  4月  6 15:24 2019 access_log
-rw-r--r-- 1 root root 1.4M  4月  6 15:02 2019 error_log
-rw-r--r-- 1 root root 143K  3月 31 03:23 2019 error_log-20190331
-rw-r--r-- 1 root root 327M  3月 31 03:23 2019 access_log-20190331
-rw-r--r-- 1 root root 159K  3月 24 03:24 2019 error_log-20190324
-rw-r--r-- 1 root root 412M  3月 24 03:23 2019 access_log-20190324
-rw-r--r-- 1 root root 174K  3月 17 03:15 2019 error_log-20190317
-rw-r--r-- 1 root root 376M  3月 17 03:14 2019 access_log-20190317
-rw-r--r-- 1 root root 178K  3月 10 03:24 2019 error_log-20190310
-rw-r--r-- 1 root root 229M  3月 10 03:23 2019 access_log-20190310

logrotateの設定

私の場合はapacheのログローテーションの設定が良くなかったので、とりあえずローテしたファイルはgzipで圧縮するような設定に変更

logrotate.conf

sudo vim  /etc/logrotate.conf 
logrotate.conf
# uncomment this if you want your log files compressed
compress # ここをコメントアウト外す

logrotate.d/httpd

sudo vim /etc/logrotate.d/httpd 
/etc/logrotate.d/httpd
/var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    postrotate
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}

logrotateの設定反映

sudo logrotate /etc/logrotate.conf 

既にあるローテしたファイルを圧縮

ls /var/log/httpd/access_log-* | xargs gzip
ls /var/log/httpd/erro_log-* | xargs gzip

※xargs:lsした結果のファイルに対して指定のコマンド(gzip)を実行

ll -hS
合計 363M
-rw-r--r-- 1 root root 326M  4月  6 15:53 2019 access_log
-rw-r--r-- 1 root root  11M  3月 24 03:23 2019 access_log-20190324.gz
-rw-r--r-- 1 root root 9.5M  3月 17 03:14 2019 access_log-20190317.gz
-rw-r--r-- 1 root root 8.5M  3月 31 03:23 2019 access_log-20190331.gz
-rw-r--r-- 1 root root 7.1M  3月 10 03:23 2019 access_log-20190310.gz
-rw-r--r-- 1 root root 1.4M  4月  6 15:46 2019 error_log
-rw-r--r-- 1 root root  10K  3月 10 03:24 2019 error_log-20190310.gz
-rw-r--r-- 1 root root 8.6K  3月 24 03:24 2019 error_log-20190324.gz
-rw-r--r-- 1 root root 7.4K  3月 31 03:23 2019 error_log-20190331.gz
-rw-r--r-- 1 root root 6.7K  3月 17 03:15 2019 error_log-20190317.gz

1.7G→363Mに削減!
今後も圧縮されるはずだから容量はこれ以上は増えないはず。

参考サイト

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2