Git Product home page Git Product logo

mysql_schedule_backup's Introduction

mysql_schedule_backup

mysql_schedule_backup是定时、全量备份指定mysql数据库的程序。可设置是否压缩备份文件、备份文件保留的天数

备份流程图

image

Requirements

  • schedule==0.4.2

How to run

  • 安装schedule
pip install schedule
  • 根据demo配置conf.py中相关的备份信息
  • 修改mysql_schedule_backup.py文件中schedule.every().day.at("06:00").do(backup_mysql, bacup_conf)为你的业务时间
  • 在mysql_schedule_backup目录下运行
python mysql_schedule_backup.py

备份结果

  • 备份是文件夹或者压缩文件,以备份时间为名称
  • 文件夹(压缩文件)里面分为*.sql*.json两类文件,其中conf.json为备份的配置文件,*.sql为备份的主文件,以datetime_database.sql方式命名
  • 最终的备份样例
└─20170611
     ├─20170611_sakila.sql
     ├─20170611_tmp.sql
     ├─20170611_world.sql
     └─conf.json

Features

  • 使用schedule支持定时备份
  • 使用配置文件conf.py统一除了备份时间外的配置备份信息(schedule的定时计划比较复杂,没有放置到配置文件conf.py中,而是放在主程序mysql_schedule_backup.py
  • 支持多个数据库备份
  • 支持压缩备份文件
  • 支持设置备份文件保留时间
  • 备份文件中包含mysql_schedule_backup运行的配置信息

项目构成

└─mysql_schedule_backup
     ├─conf.py
     ├─mysql_schedule_backup.py
     └─requirements.txt
  • requirements.txt:项目的requirements列表
  • mysql_schedule_backup.py:是备份运行的主程序,其中backup_mysql函数是与配置文件conf.py交互的主函数。如果定制配置文件的样式可以对backup_mysql函数进行相应的修改。
if __name__ == "__main__":
    SLEEP_TIME = 30
    # 测试定时运行程序 每分钟运行一次
    # schedule.every().minutes.do(backup_mysql)
    # 每天固定时间运行程序
    schedule.every().day.at("06:00").do(backup_mysql)

    while True:
        schedule.run_pending()
        time.sleep(SLEEP_TIME)
  • conf.py:mysql_schedule_backup是定时的配置文件,可配置内容包括备份存放路径、备份保留时间、备份是否压缩、备份的数据库及表(支持多个库备份)。一般的备份文件如下
job = {
    "setting": {
        # 备份的根目录
        "root": "F:/mysql_schedule_backup",
        # 备份保持的时间
        "keep_day": 60,
        # 是否压缩备份
        "is_zip": "true"
    },
    "db_connection": [
        # 第一个要备份的库
        {
            "usr": "root",
            "pwd": "mysql",
            "host": "127.0.0.1",
            "port": "3306",
            "db_and_table": {
                # "backup_database": ["backup_table_1", "backup_table_2", ...]
                "tmp": [
                    "tb1",
                    "tb2"
                ],
                # "backup_database": ["*"]   # *默认全库备份
                "world": [
                    "*"
                ]
            }
        },
		# 第二个要备份的库
        {
            "usr": "root",
            "pwd": "mysql",
            "host": "127.0.0.1",
            "port": "3306",
            "db_and_table": {
                # "backup_database": ["backup_table_1", "backup_table_2", ...]
                "sakila": [
                    "*"
                ]
            }
        }
    ]
}

mysql_schedule_backup's People

Contributors

zhongjiajie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.