需求汇总分析:
自动备份CAM资料库近三天有修改保存的料号(增量备份)
import os
import sys
import time
import re
import datetime
import xml.dom.minidom
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
sys.path.append('/incam/server/site_data/scripts/py3_package')
import sendMail
from ApiClasses import *
_header = {
'脚本名称': 'InCAM料号增量备份',
'开发人员': 'Twei Tang',
'开发时间': '2022年11月15日',
'版本信息': 'A.1.0',
'联系方式': (
'邮箱地址<>',
'微信号码<>',
'手机号码<>'
),
'开发信息': (
'无'
),
'修改信息': (
'当前版本(A.1.0),首次开发测试,暂无版本变更信息'
),
'沟通记录': (
'无'
)
}
class BackUpJobs(object):
def __init__(self):
super(BackUpJobs, self).__init__()
self.curDate = ""
self.AllJobsPath = time.strftime("%Y-%m-%d-%H", time.localtime())
self.ser_backPath = '/backup/backup/Backup_tgz/cam_job_bak/days_backup/' + self.AllJobsPath
if not os.path.exists(self.ser_backPath):
try:
os.system(f"mkdir -p {self.ser_backPath}")
except BaseException as e:
print(e)
self.backOkLogfile = self.ser_backPath + f'/备份成功料号清单-{self.AllJobsPath}.log'
self.backNgLogfile = self.ser_backPath + f'/备份失败料号清单-{self.AllJobsPath}.log'
self.backTime = time.strftime('%Y-%m-%d', time.localtime())
self.runTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
self.backOkJobsList = []
self.backNgJobsList = []
self.jobNameListRcs = []
self.DBpath = []
self.getDBlist()
self.getCurDate()
self.getRcsJobs()
self.getFwJobs()
self.getBakJobsLog()
self.SendMail()
self.send_wechat_message()
def getDBlist(self):
"""
获取资料库列表
:return: dblist
"""
db_list_file = "/incam/server/site_data/dblist.xml"
DOMTree = xml.dom.minidom.parse(db_list_file)
DBLists = DOMTree.documentElement.getElementsByTagName("db")
for u in DBLists:
self.DBpath.append(u.getAttribute("nfspath"))
def getCurDate(self):
"""近3天的日期"""
curDateTmp = datetime.datetime.now()
curDateTmp2 = (curDateTmp + datetime.timedelta(days=-8)).strftime("%Y-%m-%d %H:%M:%S")
tmp1 = str(curDateTmp2.split(" ")[0]).split("-")
self.curDate = tmp1[0] + tmp1[1] + tmp1[2]
def getRcsJobs(self):
"""获取rcs文件里的job"""
rcsFile = "/incam/server/config/rcs"
f = open(rcsFile, 'r')
for line in f:
if line.find('NAME') > 0:
tmp2 = line.split('=')[1][1:]
tmp3 = tmp2.replace("\n", "")
self.jobNameListRcs.append(tmp3)
f.close()
def getFwJobs(self):
"""获取所有的jobs"""
for db in self.DBpath:
incamJobPath = db + '/' + 'jobs'
jobDirs = os.listdir(incamJobPath)
for job in jobDirs:
tmp = incamJobPath + "/" + job
if os.path.isdir(tmp) and os.path.exists(tmp + "/misc/last_save"):
with open(tmp + "/misc/last_save", "r") as t:
T = t.readline().split(".")[0]
try:
SaveTime = datetime.datetime(int(T[0:4]), int(T[4:6]), int(T[6:]))
except BaseException as e:
print(e)
SaveTime = datetime.datetime(int("20" + T[0:2]), int(T[2:4]), int(T[4:]))
CompTime = datetime.datetime(int(self.curDate[0:4]), int(self.curDate[4:6]), int(self.curDate[6:]))
if (SaveTime - CompTime).days >= 0:
cadTure = re.match("^[a-zA-Z]\d{2}[a-zA-Z]{2}\d{4}\d{4}[a-zA-Z]\d-org$", job)
OldCadTure = re.match("^[a-zA-Z0-9]{2}[a-zA-Z][0-9]{7}[a-zA-Z]\d-org$", job)
workTure = re.match("^[a-zA-Z]\d{2}[a-zA-Z]{2}\d{4}\d{4}[a-zA-Z]\d$", job)
OldWorkTure = re.match("^[a-zA-Z0-9]{2}[a-zA-Z][0-9]{7}[a-zA-Z]\d$", job)
if cadTure or OldCadTure:
self.__backUpJobs(job, incamJobPath, "原稿料号")
elif workTure or OldWorkTure:
self.__backUpJobs(job, incamJobPath, "工作料号")
def __backUpJobs(self, jobName_, scrPath_, jobType_):
jobPath = scrPath_ + "/" + jobName_
ser_backPath = self.ser_backPath + "/" + jobName_ + '.tgz'
if os.path.isdir(jobPath):
os.system("cd {0}".format(scrPath_))
try:
os.system(f"tar czvf {ser_backPath} -C {scrPath_} {jobName_}")
self.backOkJobsList.append(jobName_)
except BaseException as f:
self.backNgJobsList.append(jobName_ + f" 错误信息:{f}")
def getBakJobsLog(self):
if len(self.backOkJobsList) != 0:
JobListString = "\n".join(self.backOkJobsList)
if not os.path.exists(self.backOkLogfile):
os.system("touch {0}".format(self.backOkLogfile))
with open(self.backOkLogfile, "w") as f:
f.write(JobListString)
if len(self.backNgJobsList) != 0:
JobListString = "\n".join(self.backNgJobsList)
if not os.path.exists(self.backNgLogfile):
os.system("touch {0}".format(self.backNgLogfile))
with open(self.backNgLogfile, "w") as f:
f.write(JobListString)
def SendMail(self):
att_list = []
if os.path.exists(self.backOkLogfile):
att_list.append(self.backOkLogfile)
if os.path.exists(self.backNgLogfile):
att_list.append(self.backNgLogfile)
_log_text = "某某基地服务器CAM料号增量备份完成,详细备份清单参考附件,增量备份每天执行三次"
mess_list = [f'<tr bgcolor=#DDDDDD><th>backup_day_jobs.py</th><th>{self.runTime}</th><th>{_log_text}</th></tr>']
mess_text = "\n".join(mess_list)
html_text = f'''
<p>某某基地服务器CAM料号增量备份日志</p>
<table border=2 bordercolor=#1212FC>
<tr border=2 bgcolor=#FF6666><th>程序</th><th>时间</th><th>日志信息</th></tr>
{mess_text}
</table>
'''
_to_list = ["用户邮箱1", "用户邮箱2", "用户邮箱3"]
_send_server = "sysinfo@askpcb.com"
try:
SM = sendMail.MailMain("某某基地服务器(backup_day_jobs.py)",
_send_server,
_to_list,
f"{html_text}",
f"某某基地CAM料号增量备份日志")
if len(att_list) > 0:
SM.SendMail(text_type="html", att_type=att_list)
else:
SM.SendMail(text_type="html")
except BaseException as e:
_error_log = f"/incam/server/logs/料号备份邮件发送失败日志/{self.AllJobsPath}.log"
if not os.path.exists(_error_log):
os.system(f"touch {_error_log}")
with open(_error_log, "a", encoding="utf-8") as f:
f.write(f"Error: 无法发送邮件, 详情:{e}\n")
@staticmethod
def send_wechat_message():
_wc = WeChatMessage()
html_text = (f'某某基地服务器CAM料号增量备份日志\n'
f'执行程序:<你的脚本程式路径(此处标明可以方便维护人员随时查看维护)>\n'
f'执行时间:午夜23:00,中午12:00,下午17:30\n'
f'执行频率:每天执行三次\n'
f'执行权限:incam普通用户\n'
f'执行内容:备份三天内有保存修改的正式&原稿料号')
_dict = _wc.get_users_id()
_users_id = [list(i.values())[0] for i in _dict["接收信息的部门"] if
list(i.keys())[0] in ["接收用户1", "接收用户2", "接收用户3"]]
_wc.send_text_message(to_users=_users_id,
agent_id=你的企微应用ID,
_msg_system="某某基地服务器CAM料号增量备份",
_message=html_text)
if __name__ == '__main__':
buj = BackUpJobs()