Files
aaPanel/script/logsBackup
T
jose 5e2cbf9a5c v6.8.4
1. After the update, the software and panel directory permissions will be set to strict mode (it will be restored automatically after modification)
2. Increase the panel comprehensive anti-riot mechanism
3. Enhance the security of the panel session<br>
4. Enhanced panel entry verification mechanism
5. Optimize the terminal
6. Optimize the panel memory release mechanism
Note 1: Version 6.8.2/6.9.11 has security issues, please be sure to upgrade to the latest version
Note 2: After this update is successful, it will automatically log out and you need to log in again
2020-09-01 16:24:02 +08:00

94 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/python
#coding: utf-8
#-----------------------------
#宝塔Linux面板网站日志切割脚本
#-----------------------------
import sys
import os
import shutil
import time
import glob
os.chdir("/www/server/panel")
sys.path.append('class/')
import public
print ('==================================================================')
print( '★['+time.strftime("%Y/%m/%d %H:%M:%S")+']Cutting log')
print ('==================================================================')
print ('|--Currently retaining the latest ['+sys.argv[2]+'] copies')
logsPath = '/www/wwwlogs/'
is_nginx = False
if os.path.exists('/www/server/nginx/logs/nginx.pid'): is_nginx = True
px = '.log'
if not is_nginx: px = '-access_log'
def build_errlog(sitename):
if is_nginx:
log = sitename + '.error.log'
elif os.path.exists('/usr/local/lsws/bin/lswsctrl'):
log = sitename + '_ols.error_log'
else:
log = sitename + '-error_log'
return log
def clean_backlog(logname,num):
logs=sorted(glob.glob(logname+"_*"))
count=len(logs)
num=count - num
for i in range(count):
if i>num: break;
os.remove(logs[i])
print('|---The extra log ['+logs[i]+'] has been deleted!')
def split_logs(oldFileName,num,site_name):
global logsPath
errlog_name = build_errlog(site_name)
old_errlog = logsPath + errlog_name
if not os.path.exists(oldFileName):
print('|---'+oldFileName+'file does not exist!')
return
clean_backlog(oldFileName,num)
clean_backlog(old_errlog,num)
newFileName=oldFileName+'_'+time.strftime("%Y-%m-%d_%H%M%S")+'.log'
shutil.move(oldFileName,newFileName)
new_errlog = old_errlog+'_'+time.strftime("%Y-%m-%d_%H%M%S")+'.log'
shutil.move(old_errlog, new_errlog)
if not os.path.exists('/www/server/panel/data/log_not_gzip.pl'):
os.system("gzip %s" % newFileName)
os.system("gzip %s" % new_errlog)
print('|---The log has been cut to:' + newFileName + '.gz')
print('|---The log has been cut to:' + new_errlog + '.gz')
else:
print('|---The log has been cut to:'+newFileName+'.log')
print('|---The log has been cut to:' + new_errlog + '.log')
def split_all(save):
sites = public.M('sites').field('name').select()
for site in sites:
oldFileName = logsPath + site['name'] + px
split_logs(oldFileName,save,site['name'])
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]
elif siteName[-11:] == '-access_log':
siteName = siteName.replace("-access_log",'')
else:
siteName = siteName.replace("_ols.access_log", '')
oldFileName = logsPath+sys.argv[1]
split_logs(oldFileName,num,siteName)
if is_nginx:
os.system("kill -USR1 `cat /www/server/nginx/logs/nginx.pid`")
elif os.path.exists('/usr/local/lsws/bin/lswsctl'):
os.system('/usr/local/lsws/bin/lswsctl restart')
else:
os.system('/etc/init.d/httpd reload')