Ejemplo n.º 1
0
def backup():
    backup_time = datetime.now()
    time_str = backup_time.strftime('%Y-%m-%d-%H:%M:%S')  # 文件名不能有空格,否则mysql导出报错
    deferred_job = DeferredJob()
    notice = u'备份时间:%s<br>' % time_str
    # 添加备份任务并记录到数据库
    filename = "{}.sql.zip".format(time_str)
    #table_name为空时表示备份整个数据库
    job = MySQLExport(config.BACKUP_STORAGE_NAME, filename, '', '')
    job_id = deferred_job.add(job)
    notice += u'ID:%s, 文件名:%s<br>' % (job_id, filename)
    backup = BackUp(job_id=job_id, timestamp=backup_time, file_name=filename)
    db.session.add(backup)
    db.session.commit()
    notice = u'备份提交!<br>%s' % notice
    # 发送备份提交邮件
    html_mail(config.BACKUP_TO_MAIL, 'xxx-数据库备份', notice, config.MAIL_SMTP)
    return notice
Ejemplo n.º 2
0
def backup():
    backup_time = datetime.now()
    time_str = backup_time.strftime('%Y-%m-%d-%H:%M:%S')  # 文件名不能有空格,否则mysql导出报错
    deferred_job = DeferredJob()
    notice = u'备份时间:%s<br>' % time_str
    # 添加备份任务并记录到数据库
    filename = "{}.sql.zip".format(time_str)
    #table_name为空时表示备份整个数据库
    job = MySQLExport(config.BACKUP_STORAGE_NAME, filename, '', '')
    job_id = deferred_job.add(job)
    notice += u'ID:%s, 文件名:%s<br>' % (job_id, filename)
    backup = BackUp(job_id=job_id, timestamp=backup_time, file_name=filename)
    db.session.add(backup)
    db.session.commit()
    notice = u'备份提交!<br>%s' % notice
    # 发送备份提交邮件
    html_mail(config.BACKUP_TO_MAIL, 'whwb-数据库备份', notice, config.MAIL_SMTP)
    return notice
Ejemplo n.º 3
0
def backup_status():
    deferred_job = DeferredJob()
    latest_backup = BackUp.query.order_by(BackUp.timestamp.desc()).first()
    notice = u"备份时间:%s<br>" % str(latest_backup.timestamp)
    # 查询数据库保存的备份状态
    if latest_backup.is_done:
        # html_mail(config.BACKUP_TO_MAIL, 'whlove-数据库备份', u'备份已完成,邮件已发送!')
        return u'备份已完成,邮件已发送!'
    # 向服务器查询备份状态
    status = deferred_job.status(latest_backup.job_id)
    notice += u"文件名:%s, ID:%s, 状态:%s<br>" % (latest_backup.file_name, str(latest_backup.job_id), status)
    # 修改备份状态, 完成备份发送邮件
    if status == 'done':
        latest_backup.is_done = True
        db.session.add(latest_backup)
        db.session.commit()
        notice = u'备份已完成!<br>%s' % notice
        html_mail(config.BACKUP_TO_MAIL, 'whwb-数据库备份', notice, config.MAIL_SMTP)
        return notice
    else:
        return u'备份未完成!<br>%s' % notice
Ejemplo n.º 4
0
def backup_status():
    deferred_job = DeferredJob()
    latest_backup = BackUp.query.order_by(BackUp.timestamp.desc()).first()
    notice = u"备份时间:%s<br>" % str(latest_backup.timestamp)
    # 查询数据库保存的备份状态
    if latest_backup.is_done:
        # html_mail(config.BACKUP_TO_MAIL, 'whlove-数据库备份', u'备份已完成,邮件已发送!')
        return u'备份已完成,邮件已发送!'
    # 向服务器查询备份状态
    status = deferred_job.status(latest_backup.job_id)
    notice += u"文件名:%s, ID:%s, 状态:%s<br>" % (latest_backup.file_name, str(latest_backup.job_id), status)
    # 修改备份状态, 完成备份发送邮件
    if status == 'done':
        latest_backup.is_done = True
        db.session.add(latest_backup)
        db.session.commit()
        notice = u'备份已完成!<br>%s' % notice
        html_mail(config.BACKUP_TO_MAIL, 'whwb-数据库备份', notice, config.MAIL_SMTP)
        return notice
    else:
        return u'备份未完成!<br>%s' % notice