PCB制前工程CAM资料日备份项目制作

  1. 编写备份自动化程式
  • 需求汇总分析:
    自动备份CAM资料库近三天有修改保存的料号(增量备份)
    #!/usr/bin/env py3k
    # Linux系统自动执行任务计划设置模板(如何自动执行备份脚本)
    # crontab setup  (Used by system automatic execution) :
    # command : minute hour day month week command
    #      ex :   30    23   *    *    *   python3  /incam/server/site_data/scripts/backup_day_jobs.py
    #      ex :   00    12   *    *    *   python3  /incam/server/site_data/scripts/backup_day_jobs.py
    #      ex :   30    17   *    *    *   python3  /incam/server/site_data/scripts/backup_day_jobs.py
    
    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)
    
            # 备份log文件
            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())
    
            # 执行完成状态(收集ok/ng各自料号清单)
            self.backOkJobsList = []
            self.backNgJobsList = []
    
            # rcs状态(收集被锁定的料号清单)
            self.jobNameListRcs = []
            self.DBpath = []
    
    		# 获取incam/incampro的所有资料库
            self.getDBlist()
    
    		# 获取指定时间的
            self.getCurDate()
            self.getRcsJobs()
            self.getFwJobs()
            self.getBakJobsLog()
            self.SendMail()
    
            # 企微推送系统维护人员
            self.send_wechat_message()
    
        def getDBlist(self):
            """
            获取资料库列表
            :return: dblist
            """
            # 此处在执行crontab时会报错,需改为绝对路径
            # db_list_file = os.environ.get("INCAM_DIR") + "/server/site_data/dblist.xml"
            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'
            # Tar
            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"]	# 替换成你需要接收邮件信息的email地址
            _send_server = "sysinfo@askpcb.com"		# 替换成你发送邮件的服务器的email地址
            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()
    
    
  1. 配置脚本自动执行
  • 创建自动执行任务计划
    (1).进入root用户,终端下输入su回车
    $ su
    密码: <输入密码后回车>
    # 
    
    (2).编辑任务计划
    # crontab -e
    输入以下内容
    # CAM料号增量备份(23:00)
    0 23 * * *      /usr/bin/python3 /备份脚本目录/backup_day_jobs.py >/日志文件保存路径/crontab.log 2>&1
    
    # CAM料号增量备份(12:00)
    0 12 * * *      /usr/bin/python3 备份脚本目录/backup_day_jobs.py >/日志文件保存路径/crontab.log 2>&1
    
    # CAM料号增量备份(17:30)
    30 17 * * *     /usr/bin/python3 /备份脚本目录/backup_day_jobs.py >/日志文件保存路径/crontab.log 2>&1
    
  1. 自动任务执行效果
  • 自动邮件信息:
    在这里插入图片描述- 自动企业微信信息推送:
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一阵寒风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值