[Python] subprocess모듈과 7z을 이용한 전일 로그 자동 압축 보관
7z은 커맨드 명령어를 제공하며, 오픈소스이다.
다운로드 페이지 : https://www.7-zip.org/download.html
Download
Download .7z Any / x86 / x64 LZMA SDK: (C, C++, C#, Java)
www.7-zip.org
이번 포스팅에서 쓰는 옵션은 압축(a) 옵션을 사용하여 전일 로그를 압축해보려 한다. 다양한 옵션을 이용하려면 포스팅 하단의
URL 경로나 7z.exe 명령어를 실행하면 알 수 있다.
대상 로그는 IIS로그로 포맷은 ex_yyyymmdd.log 형식이며, 현재 2019-10-24일 기준으로 전일 기준인
ex_20191023.log 파일이다.
import subprocess
from datetime import timedelta
from datetime import datetime
from os import chdir
yesterdaylog = 'ex_'+str(datetime.today().date() - timedelta(days=1)).replace('-','')
chdir('path') # compress path
program = 'C:\\Program Files\\7-Zip\\7z.exe' # 7z 설치 위치 (Default)
source = yesterdaylog + '.log'
dst = yesterdaylog +'.zip'
subprocess.run([program, 'a', dst, source])
-----------------------------------------------------------------------------------------
7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21
Scanning the drive:
1 file, 122139334 bytes (117 MiB)
Creating archive: ex_20191023.zip
Add new data to archive: 1 file, 122139334 bytes (117 MiB)
Files read from disk: 1
Archive size: 7003372 bytes (6840 KiB)
Everything is Ok
실행이 잘 되었으면, 예약스케줄러에 등록해서 일 단위로 자동 수행하면 되고, 압축 리포트를 받고 싶으면 smtp 모듈을
추가하여 결과를 메일로 받으면 되겠다.
Guide URL : https://sevenzip.osdn.jp/chm/cmdline/index.htm
Command Line Version User's Guide
Command Line Version User's Guide 7z.exe is the command line version of 7-Zip. 7z.exe uses 7z.dll from the 7-Zip package. 7z.dll is used by the 7-Zip File Manager also. 7za.exe (a = alone) is a standalone version of 7-Zip. 7za.exe supports only 7z, xz, lzm
sevenzip.osdn.jp