forked from midoks/mdserver-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs_backup.py
executable file
·70 lines (58 loc) · 1.98 KB
/
logs_backup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python
# coding: utf-8
#-----------------------------
# 网站日志切割脚本
#-----------------------------
import sys
import os
import shutil
import time
import glob
if sys.platform != 'darwin':
os.chdir('/www/server/mdserver-web')
chdir = os.getcwd()
sys.path.append(chdir + '/class/core')
reload(sys)
sys.setdefaultencoding('utf-8')
import mw
print('==================================================================')
print('★[' + time.strftime("%Y/%m/%d %H:%M:%S") + '],切割日志')
print('==================================================================')
print('|--当前保留最新的[' + sys.argv[2] + ']份')
logsPath = mw.getLogsDir()
px = '.log'
def split_logs(oldFileName, num):
global logsPath
if not os.path.exists(oldFileName):
print('|---' + oldFileName + '文件不存在!')
return
logs = sorted(glob.glob(oldFileName + "_*"))
count = len(logs)
num = count - num
for i in range(count):
if i > num:
break
os.remove(logs[i])
print('|---多余日志[' + logs[i] + ']已删除!')
newFileName = oldFileName + '_' + time.strftime("%Y-%m-%d_%H%M%S") + '.log'
shutil.move(oldFileName, newFileName)
print('|---已切割日志到:' + newFileName)
def split_all(save):
sites = mw.M('sites').field('name').select()
for site in sites:
oldFileName = logsPath + site['name'] + px
split_logs(oldFileName, save)
if __name__ == '__main__':
num = int(sys.argv[2])
if sys.argv[1].find('ALL') == 0:
split_all(num)
else:
siteName = sys.argv[1]
if siteName[-4:] == '.log':
siteName = siteName[:-4]
else:
siteName = siteName.replace("-access_log", '')
oldFileName = logsPath + '/' + sys.argv[1]
split_logs(oldFileName, num)
path = mw.getServerDir()
os.system("kill -USR1 `cat " + path + "/openresty/nginx/logs/nginx.pid`")