资讯专栏INFORMATION COLUMN

简单备份文件并发送到指定邮箱

lingdududu / 3127人阅读

摘要:背景一哥们发了个诉求,总觉得自己的服务器不安全,想搞个定时备份文件并发送到自己的邮箱实现代码如下简单说明打包文件这个实现比较初级,直接用命令进行打包发送邮件这个就不说了,现成的模块直接拿来用日志记录加上日志,可以很清

背景

一哥们发了个诉求,总觉得自己的服务器不安全,想搞个定时备份文件并发送到自己的邮箱

1 实现代码如下
# -*- coding: utf-8 -*-

from __future__ import absolute_import, unicode_literals

import os
import datetime
import logging
import logging.config

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email.mime.application import MIMEApplication
import smtplib

name = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
base_path = "/root/xxxx/temp"
zip_path = "/root/xxxx/backup/{}.tar.bz2".format(name)


def set_logging():
    """"""

    log_dir, log_file = "/root/xxxx/logs", "/root/xxxx/logs/backup.log"

    if not os.path.exists(log_dir):
        os.mkdir(log_dir)

    if not os.path.exists(log_file):
        open(log_file, "w")

    DEFAULT_LOGGING = {
        "version": 1,
        "disable_existing_loggers": False,
        "formatters": {
            "formatone": {
                "format": "[%(asctime)s] %(levelname)s : %(message)s",
            }
        },
        "handlers": {
            "file": {
                "level": "DEBUG",
                "filename": "/root/xxxx/logs/backup.log",
                "formatter": "formatone",
                "class": "logging.handlers.RotatingFileHandler",
                "maxBytes": 100 * 1024 * 1024,
                "backupCount": 10,
            },
        },
        "loggers": {
            "backup": {
                "handlers": ["file"],
                "level": "INFO",
            },
        }
    }

    logging.config.dictConfig(DEFAULT_LOGGING)


def zip_files():
    """zip files"""
    os.system("tar -cjf {} -C {} data".format(zip_path, base_path))


def sendmail():
    """send mail"""
    set_logging()
    zip_files()

    logger = logging.getLogger("backup")

    mail_from, password = "xxxxxxx@aliyun.com", "xxxxxxx"
    mail_to = "xxxxx@qq.com"
    smtp_server = "smtp.aliyun.com"

    msgRoot = MIMEMultipart("related")
    msgRoot["Subject"] = "send backup files {}".format(name)
    msgRoot["From"] = "{}<{}>".format(Header("backup", "utf-8"), mail_from)
    msgRoot["To"] = mail_to

    msgText = MIMEText("backup files", "plain", "utf-8")
    msgRoot.attach(msgText)

    zip_con = MIMEApplication(open(zip_path,"rb").read())
    zip_con.add_header("Content-Disposition", "attachment",
                       filename="{}.tar.bz2".format(name))
    msgRoot.attach(zip_con)

    try:
        server = smtplib.SMTP_SSL(smtp_server)
        server.login(mail_from, password)
        server.sendmail(mail_from, mail_to, msgRoot.as_string())
        server.quit()
        logger.info("send {} backup files success".format(name))
    except Exception, e:
        logger.error("send {} failed {}".format(name, e))
        sendmail()


if __name__ == "__main__":
    sendmail()
2 简单说明 2.1 打包文件

这个实现比较初级,直接用 shell 命令进行打包

def zip_files():
    """zip files"""
    os.system("tar -cjf {} -C {} data".format(zip_path, base_path))
2.2 发送邮件

这个就不说了,现成的模块直接拿来用

2.3 日志记录

加上日志,可以很清楚的让我知道发送情况如下,示例如下:

[2017-04-14 00:00:03,251] INFO : send 20170414000001 backup files success
[2017-04-14 03:00:02,620] INFO : send 20170414030001 backup files success
[2017-04-14 06:00:02,406] INFO : send 20170414060001 backup files success
[2017-04-14 09:00:02,349] INFO : send 20170414090001 backup files success
[2017-04-14 12:00:02,299] INFO : send 20170414120001 backup files success
[2017-04-14 15:01:04,696] ERROR : send 20170414150001 failed [Errno 110] Connection timed out
[2017-04-14 15:01:05,401] INFO : send 20170414150001 backup files success
2.4 定时处理

定时这个处理,直接使用 crontab 命令,创建个 backup_cron 文件,写入

0 */3 * * *  python /root/xxxxx/backup.py
3 简单小结

业务比较简单,实现也比较简单,没啥可说的

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/40708.html

相关文章

  • Linux定时备份数据库到指定邮箱

    摘要:本文转自豆浆下每天备份数据库并发送到指定邮箱一配置邮箱这里使用的是网易邮箱邮箱的服务,服务器是。成功收到邮件,没问题。编写脚本和定时任务万事俱备,接下来要做自动化工作建立一个备份脚本,并使用定时任务每天执行它。 本文转自豆浆Melon :linux下每天备份Mysql数据库并发送到指定邮箱 一、配置邮箱 这里使用的是网易邮箱126邮箱的STMP服务,服务器是smtp.126.com。...

    hellowoody 评论0 收藏0
  • SegmentFault 技术周刊 Vol.42 - MySQL:从删库到跑路

    摘要:肖鹏微博数据库那些事儿肖鹏,微博研发中心技术经理,主要负责微博数据库相关的业务保障性能优化架构设计,以及周边的自动化系统建设。经历了微博数据库各个阶段的架构改造,包括服务保障及体系建设微博多机房部署微博平台化改造等项目。 showImg(https://segmentfault.com/img/bV24Gs?w=900&h=385); 对于手握数据库的开发人员来说,没有误删过库的人生是...

    aboutU 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<