System ManageMent

[Python] sqlite db를 이용한 서버와 스토리지 파일 용량 분석

Pydole 2019. 8. 21. 12:50

 

 

서버와 스토리지를 운영하다 보면 디스크 용량이 부족하게 되고, 과거파일을 삭제하면서 공간을 확보할 수 있으면

 

좋겠지만.. 그렇지 못할 때에는 디스크 용량을 증설해야한다.

 

 python(파이썬)을 이용하여 디렉토리와 파일들을 DB화 할 수 있는데, DB를 이용하여 최근 5년간 증가한 파일용량과 파일 수를 분석해보자.

 

DB는 sqlite3 파일DB를 이용

 

import sqlite3
import os
from datetime import datetime

outputFile = 'File.db'
print(outputFile)

conn = sqlite3.connect(outputFile)
c = conn.cursor()
c.execute('''create table fileinfo (filename VARCHAR(100), filesize INT(100), filemotify DATETIME)''')
c.execute('create index idx on fileinfo(filemotify)')

for (path, dir, files) in os.walk(r' '):   # path
     for filename in files:
          try:
              fSize = os.path.getsize(path + '\\' + filename)
              fMotify = str(datetime.fromtimestamp(os.path.getmtime(path + '\\' + filename)))[:19]
              c.execute('''INSERT INTO fileinfo VALUES("%s","%s","%s")''' % (filename, str(fSize), fMotify))
          
          except Exception as e:
              print(e)
        
conn.commit()
c.close()
conn.close()

 

 

샘플로 약 179만개의 파일을 분석해보겠다.

 

import sqlite3

conn = sqlite3.connect(r'D:\File.db')
c = conn.cursor()

rows = c.execute('select count(*) from fileinfo')

for row in rows.fetchone():
    print(row)

c.close()
conn.close()

1,797,877

 

 

 

 

 

2010년부터 2019년까지 생성된 파일 수

 

 

import sqlite3

tup = ('10','11','12','13','14','15','16','17','18','19')

conn = sqlite3.connect(r'D:\File.db')
c = conn.cursor()

for x in tup:
    rows = c.execute('select count(*) from fileinfo where fMdate between "20%s-01-01 00:00:00" and "20%s-12-31 23:59:59"' %
                     (x, x))
    print("20%s-01-01 00:00:00  ~ 20%s-12-31 23:59:59 : %s" % (x, x, rows.fetchone()[0]))

c.close()
conn.close()

 

 

2010-01-01 00:00:00 ~ 2010-12-31 23:59:59 : 362058

2011-01-01 00:00:00 ~ 2011-12-31 23:59:59 : 339408

2012-01-01 00:00:00 ~ 2012-12-31 23:59:59 : 330830

2013-01-01 00:00:00 ~ 2013-12-31 23:59:59 : 284964

2014-01-01 00:00:00 ~ 2014-12-31 23:59:59 : 215356

2015-01-01 00:00:00 ~ 2015-12-31 23:59:59 : 93194

2016-01-01 00:00:00 ~ 2016-12-31 23:59:59 : 71182

2017-01-01 00:00:00 ~ 2017-12-31 23:59:59 : 52611

2018-01-01 00:00:00 ~ 2018-12-31 23:59:59 : 37553

2019-01-01 00:00:00 ~ 2019-12-31 23:59:59 : 10713

 

 

 

 

2010년부터 2019년까지 생성된 파일용량의 합

 

import sqlite3

tup = ('10','11','12','13','14','15','16','17','18','19')

conn = sqlite3.connect(r'D:\File.db')
c = conn.cursor()

for x in tup:
    rows = c.execute('select sum(fsize) from fileinfo where fMdate between "20%s-01-01 00:00:00" and "20%s-12-31 23:59:59"' %
                     (x, x))
    print("20%s-01-01 00:00:00  ~ 20%s-12-31 23:59:59 : %s" % (x, x, rows.fetchone()[0]))

c.close()
conn.close()

 

2010-01-01 00:00:00 ~ 2010-12-31 23:59:59 : 149201224848

2011-01-01 00:00:00 ~ 2011-12-31 23:59:59 : 129454602268

2012-01-01 00:00:00 ~ 2012-12-31 23:59:59 : 117549629648

2013-01-01 00:00:00 ~ 2013-12-31 23:59:59 : 98386699736

2014-01-01 00:00:00 ~ 2014-12-31 23:59:59 : 83533193076

2015-01-01 00:00:00 ~ 2015-12-31 23:59:59 : 38641729471

2016-01-01 00:00:00 ~ 2016-12-31 23:59:59 : 28824412573

2017-01-01 00:00:00 ~ 2017-12-31 23:59:59 : 18583139728

2018-01-01 00:00:00 ~ 2018-12-31 23:59:59 : 11836034638

2019-01-01 00:00:00 ~ 2019-12-31 23:59:59 : 2987908788

 

 

 

 

최근 5년간 증가한 파일 수와 용량을 분석해보면 해당 폴더는 지속적으로 감소하고 있음을 알 수 있다.

 

디스크 용량을 증설할 때는, 보수적으로 잡아도 2015년 용량 38GB를 기준으로 5년 계산하여도 190GB 정도만

 

증설하면 될 것이다. 총 용량기준으로 1TB ~ 2TB를 증설하면 Price 낭비가 될 수도 있다.