Home Backend Development Python Tutorial Back up MYSQL database using Python

Back up MYSQL database using Python

Oct 18, 2016 am 11:54 AM
mysql python backup

For work needs, I backed up the company's MYSQL database. I just started learning Python. I watched a set of simple Python teaching videos and simply wrote a backup script. I personally expressed my interest in Python's classes, functions, and build-in functions. , private variables, global variables, etc., I have no idea how to use them, when to use them, etc. I just record them here. I also welcome passing pythoners to give me advice.

Some issues known to me:

1. The script must require all behaviors in the mysql configuration file to be in the key=value format, and there must be no redundant comments. Otherwise, the ConfigParser module will make an error when parsing the configuration file. Due to lack of research I wonder if ConfigParser has a fault-tolerant method that can be called, and I don’t have time to write fault-tolerant processing. Instead, I solved it by sorting out the my.ini configuration file to make it meet the requirements of ConfigParser. The mysql configuration file I use will be attached later.

2. Extensive use of class private member variables, because I don’t know when python variables, class methods, etc. should be privatized, and what the difference is. I only know that when class private member variables are imported or inherited in other scripts, is invisible.

3. There are a lot of file operations and value transfer operations. Currently, we only ensure that there is no problem in passing values ​​in the correct format, and no redundant fault tolerance processing is done. 4. A lot of string splicing is being done. It is my first time to write an operation and maintenance related script. Since I need to call system commands and pass many parameters, I don’t know the subprocess module. I don’t know how others write operation and maintenance scripts, so I just write it directly. Spliced.

5. Other unknown bugs, undiscovered logical errors, etc.

Environment:

- Server: Dell PowerEdge T110

- OS: CentOS 6.3_x86_64

- PythonVersion: 2.7.3

- : 5.5.28 linux x86_64

MysqlBackupScript.py

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

#!/usr/bin/env python

# coding: utf8

# script MysqlBackupScript

# by Becareful

# version v1.0

   

"""

This scripts provides auto backup mysql(version == 5.5.x) database .

"""

   

import os

import sys

import datetime         #用于生成备份文件的日期

import linecache        #用于读取文件的指定行

import ConfigParser     #解析mysql配置文件

   

   

class DatabaseArgs(object):

    """

   

    """

   

    __MYSQL_BASE_DIR = r'/usr/local/mysql'            #mysql安装目录

    __MYSQL_BIN_DIR = __MYSQL_BASE_DIR + '/bin'       #mysql二进制目录

    __MYSQL_CONFIG_FILE = r'/usr/local/mysql/my.cnf'  #mysql配置文件

   

    __ONEDAY = datetime.timedelta(days=1)             #一天的时长,用于计算下面的前一天和后一天日期

    __TODAY = datetime.date.today()      #当天日期格式为 YYYY-MM-DD

    __YESTERDAY = __TODAY - __ONEDAY                  #计算昨天日期

    __TOMORROW = __TODAY + __ONEDAY                   #计算明天日期

    __WEEKDAY = __TODAY.strftime('%w')                #计算当天是一星期的星期几

   

    __MYSQL_DUMP_ARGS = {                             #用一个字典存储mysqldump 命令备份数据库的参数

        'MYISAM': ' -v -E -e -R --triggers -F  -n --opt --master-data=2 --hex-blob -B ',

        'INNODB': ' -v -E -e -R --triggers -F --single-transaction -n --opt --master-data=2 --hex-blob -B '

    }

   

    __DUMP_COMMAND = __MYSQL_BIN_DIR + '/mysqldump'   #mysqldump 命令的 路径 用于dump mysql数据

    __FLUSH_LOG_COMMAND = __MYSQL_BIN_DIR + '/mysqladmin'    #mysqladmin 命令的路径 ,用于执行 flush-logs 生成每天增量binlog

   

    __BACKUP_DIR = r'/backup/'                        # 指定备份文件存放的目录

   

    __PROJECTNAME = 'example'                         # 指定需要备份的数据库对应的项目名,将来会生成 projectname-YYYY-MM-DD.sql 等文件

    __DATABASE_LIST = []                              # 指定需要备份的数据库名,可以是多个,使用列表

    __HOST = 'localhost'

    __PORT = 3306

    __USERNAME = 'root'

    __PASSWORD = ''

    __LOGINARGS = ''                                  # 如果在localhost登陆,需要密码,可以设定登陆的参数,具体在下面有说明

    __LOGFILE = __BACKUP_DIR + '/backup.logs'

   

    def __init__(self, baseDir=__MYSQL_BASE_DIR, backDir=__BACKUP_DIR, engine='MYISAM', projectName=__PROJECTNAME,

                 dbList=__DATABASE_LIST, host=__HOST, port=__PORT, user=__USERNAME, passwd=__PASSWORD):

   

        """

            实例化对象时传入的参数,如不传入默认使用类的私有成员变量作为默认值

    :param baseDir:  

    :param backDir:

    :param engine:

    :param projectName:

    :param dbList:

    :param host:

    :param port:

    :param user:

    :param passwd:

    """

        self.__MYSQL_BASE_DIR = baseDir

        self.__BACKUP_DIR = backDir

        self.__PROJECTNAME = projectName

        self.__DATABASE_LIST = dbList

        self.__HOST = host

        self.__PORT = port

        self.__USERNAME = user

        self.__PASSWORD = passwd

        self.__ENGINE = self.__MYSQL_DUMP_ARGS[engine]

        #下面定义了如需登陆时,参数 其实就是生成 这样的格式  “-hlocalhost -uroot --password=‘xxxx’”

        self.__LOGINARGS = " -h" + self.__HOST + " -P" + str(

            self.__PORT) + " -u" + self.__USERNAME + " --password='" + self.__PASSWORD + "'"

        self.checkDatabaseArgs()   #调用检查函数

   

    def __getconfig(self, cnf=__MYSQL_CONFIG_FILE, item=None):  # 解析mysql配置文件的小函数,简单封装了下,传入一个值作为my.cnf的key去查找对应的value

   

        __mycnf = ConfigParser.ConfigParser()

        __mycnf.read(cnf)

        try:

            return __mycnf.get("mysqld", item)

        except BaseException, e:

            sys.stderr.write(str(e))

            sys.exit(1)

   

    def __getBinlogPath(self): #  取每天需要增量备份的binlog日志的绝对路径,从mysql的binlog.index文件取倒数第二行

   

        __BINLOG_INDEX = self.__getconfig(item='log-bin') + '.index'

   

        if not os.path.isfile(__BINLOG_INDEX):

            sys.stderr.write('BINLOG INDEX FILE: [' + __BINLOG_INDEX + ' ] NOT FOUND! \n')

            sys.exit(1)

        else:

            try:

                __BINLOG_PATH = linecache.getline(__BINLOG_INDEX, len(open(__BINLOG_INDEX,'r').readlines()) - 1)

                linecache.clearcache()

            except BaseException, e:

                sys.stderr.write(str(e))

                sys.exit(1)

            return __BINLOG_PATH.strip()

   

    def flushDatabaseBinlog(self):  # 调用此函数,将会执行  mysqladmin flush-logs ,刷新binlog日志

        return os.popen(self.__FLUSH_LOG_COMMAND + self.__LOGINARGS + ' flush-logs')

   

    def dumpDatabaseSQL(self):  #|通过mysqladmin 对指定数据库进行全备

        if not os.path.isfile(self.__BACKUP_DIR + '/' + self.__PROJECTNAME + '/' + str(self.__YESTERDAY) + '-' + self.__PROJECTNAME + '.sql'):

            return os.popen(self.__DUMP_COMMAND + self.__LOGINARGS + self.__ENGINE + ' '.join(

                self.__DATABASE_LIST) + ' >> ' + self.__BACKUP_DIR + '/' + self.__PROJECTNAME + '/' +str(

                self.__YESTERDAY) + '-' + self.__PROJECTNAME + '.sql')

        else:

            sys.stderr.write('Backup File [' + str(self.__YESTERDAY) + '-' + self.__PROJECTNAME +'.sql]  already exists.\n')

   

   

   

    def dumpDatabaseBinlog(self):#通过copy2() 将需要备份的binlog日志复制到指定备份目录

   

        if not os.path.isfile(self.__BACKUP_DIR + '/' + self.__PROJECTNAME + '/' +str(self.__YESTERDAY) + '-' + os.path.split(self.__getBinlogPath())[1]):

            from shutil import copy2

            try:

                copy2(self.__getBinlogPath(), self.__BACKUP_DIR + '/' + self.__PROJECTNAME + '/' +str(self.__YESTERDAY) + '-' + os.path.split(self.__getBinlogPath())[1])

            except BaseException, e:

                sys.stderr.write(str(e))

        else:

            sys.stderr.write('Binlog File [' + str(self.__YESTERDAY) + '-' +os.path.split(self.__getBinlogPath())[1] + '] already exists\n' )

   

   

    def checkDatabaseArgs(self):  #对一些必要条件进行检查

        __rv = 0

   

        if not os.path.isdir(self.__MYSQL_BASE_DIR):  #检查指定的mysql安装目录是否存在

            sys.stderr.write('MYSQL BASE DIR: [ ' + self.__MYSQL_BASE_DIR + ' ] NOT FOUND\n')

            __rv += 1

   

        if not os.path.isdir(self.__BACKUP_DIR):   #检查指定的备份目录是否存在,如不存在自动创建

            sys.stderr.write('BACKUP DIR: [ ' + self.__BACKUP_DIR + '/' + self.__PROJECTNAME +  ' ] NOT FOUND ,AUTO CREATED\n')

            os.makedirs(self.__BACKUP_DIR + '/' + self.__PROJECTNAME)

   

        if not os.path.isfile(self.__MYSQL_CONFIG_FILE): #检查mysql配置文件是否存在

            sys.stderr.write('MYSQL CONFIG FILE: [' + self.__MYSQL_CONFIG_FILE + ' ] NOT FOUND\n')

            __rv += 1

   

        if not os.path.isfile(self.__DUMP_COMMAND):  #检查备份数据库时使用的mysqldump命令是否存在

            sys.stderr.write('MYSQL DUMP COMMAND: [' + self.__DUMP_COMMAND + ' ] NOT FOUND\n')

            __rv += 1

   

        if not os.path.isfile(self.__FLUSH_LOG_COMMAND): #检查刷新mysql binlog日志使用的mysqladmin命令是否存在

            sys.stderr.write('MYSQL FLUSH LOG COMMAND: [' + self.__DUMP_COMMAND + ' ] NOT FOUND\n')

            __rv += 1

   

        if not self.__DATABASE_LIST:  #检查需要备份的数据库列表是否存在

            sys.stderr.write('Database List is None \n')

            __rv += 1

   

        if __rv:   # 判断返回值,由于上述任何一步检查失败,都会导致 __rv 值 +1 ,只要最后__rv != 0就直接退出了。

            sys.exit(1)

   

   

def crontab():  # 使用字典,来进行相关参数传递,并实例化对象,调用相关方法进行操作

   

    zabbix = {

        'baseDir': '/usr/local/mysql/',

        'backDir': '/backup/',

        'projectName': 'Monitor',

        'dbList': ['zabbix'],

        'host': 'localhost',

        'port': 3306,

        'user': 'root',

        'passwd': 'xxxxxxx'

    }

   

    monitor = DatabaseArgs(**zabbix)

    monitor.dumpDatabaseSQL()

    monitor.dumpDatabaseBinlog()

    monitor.flushDatabaseBinlog()

   

if __name__ == '__main__':

    crontab()

Copy after login

my.cnf

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

[client]

port                            = 3306

socket                          = /mysql/var/db.socket

   

[mysqld]

socket                          = /mysql/var/db.socket

datadir                         = /mysql/db/

skip-external-locking           = 1

skip-innodb                     = 0

key_buffer_size                 = 256M

max_allowed_packet              = 10M

table_open_cache                = 2048

sort_buffer_size                = 4M

read_buffer_size                = 4M

read_rnd_buffer_size            = 8M

myisam_sort_buffer_size         = 64M

myisam_max_sort_file_size       = 1G

myisam_repair_threads           = 1

myisam_recover                  = DEFAULT

thread_cache_size               = 32

query_cache_size                = 32M

query_cache_min_res_unit        = 2k

bulk_insert_buffer_size         = 64M

tmp_table_size                  = 128M

thread_stack                    = 192K

skip-name-resolve               = 1

max_connections                 = 65500

default-storage-engine          = myisam

federated                       = 0

server-id                       = 1

slave-skip-errors               = all

#log                            = /var/log/sql_query.log

slow-query-log                  = 1

slow-query-log-file             = /mysql/log/sql_query_slow.log

long-query-time                 = 5

log-queries-not-using-indexes   = 1

log-slow-admin-statements       = 1

log-bin                         = /mysql/var/log/binlog/bin-log

log-error                       = /mysql/var/log/mysql.err

master-info-file                = /mysql/var/log/master.info

relay-log                       = /mysql/var/log/relay-bin/relay-bin

relay-log-index                 = /mysql/var/log/relay-bin/relay-bin.index

relay-log-info-file             = /mysql/var/log/relay-bin/relay-bin.info

binlog_cache_size               = 8M

binlog_format                   = MIXED

max_binlog_cache_size           = 20M

max_binlog_size                 = 1G

binlog-ignore-db                = mysql

binlog-ignore-db                = performance_schema

binlog-ignore-db                = information_schema

replicate-ignore-db             = mysql

replicate-ignore-db             = performance_schema

replicate-ignore-db             = information_schema

   

innodb_data_home_dir            = /mysql/ibdata/

innodb_data_file_path           = ibdata:156M:autoextend

innodb_log_group_home_dir       = /mysql/ibdata/

log-slave-updates               = 0

back_log                        = 512

transaction_isolation           = READ-COMMITTED

max_heap_table_size             = 246M

interactive_timeout             = 120

wait_timeout                    = 120

innodb_additional_mem_pool_size = 16M

innodb_buffer_pool_size         = 512M

innodb_file_io_threads          = 4

innodb_thread_concurrency       = 8

innodb_flush_log_at_trx_commit  = 2

innodb_log_buffer_size          = 16M

innodb_log_file_size            = 128M

innodb_log_files_in_group       = 3

innodb_max_dirty_pages_pct      = 90

innodb_lock_wait_timeout        = 120

innodb_file_per_table           = 1

innodb_open_file                = 327500

open_files_limit                = 327500

   

[mysqldump]

quick                           = 1

max_allowed_packet              = 50M

   

[mysql]

auto-rehash                     = 1

socket                          = /mysql/var/db.socket

safe-updates                    = 0

   

[myisamchk]

key_buffer_size                 = 256M

sort_buffer_size                = 256M

read_buffer                     = 2M

write_buffer                    = 2M

   

[mysqlhotcopy]

interactive-timeout             = 100

Copy after login

The final generated backup directory structure is like this

1

2

3

4

5

6

7

8

9

10

11

[root@zabbix backup]# find ./

./

./Monitor

./Monitor/2013-03-16-bin-log.000008

./Monitor/2013-03-14-bin-log.000006

./Monitor/2013-03-16-Monitor.sql

./Monitor/2013-03-15-Monitor.sql

./Monitor/2013-03-15-bin-log.000007

./Monitor/2013-03-14-Monitor.sql

   

~END~

Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The primary key of mysql can be null The primary key of mysql can be null Apr 08, 2025 pm 03:03 PM

The MySQL primary key cannot be empty because the primary key is a key attribute that uniquely identifies each row in the database. If the primary key can be empty, the record cannot be uniquely identifies, which will lead to data confusion. When using self-incremental integer columns or UUIDs as primary keys, you should consider factors such as efficiency and space occupancy and choose an appropriate solution.

Can mysql return json Can mysql return json Apr 08, 2025 pm 03:09 PM

MySQL can return JSON data. The JSON_EXTRACT function extracts field values. For complex queries, you can consider using the WHERE clause to filter JSON data, but pay attention to its performance impact. MySQL's support for JSON is constantly increasing, and it is recommended to pay attention to the latest version and features.

Can mysql run on android Can mysql run on android Apr 08, 2025 pm 05:03 PM

MySQL cannot run directly on Android, but it can be implemented indirectly by using the following methods: using the lightweight database SQLite, which is built on the Android system, does not require a separate server, and has a small resource usage, which is very suitable for mobile device applications. Remotely connect to the MySQL server and connect to the MySQL database on the remote server through the network for data reading and writing, but there are disadvantages such as strong network dependencies, security issues and server costs.

Do mysql need to pay Do mysql need to pay Apr 08, 2025 pm 05:36 PM

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

How to optimize MySQL performance for high-load applications? How to optimize MySQL performance for high-load applications? Apr 08, 2025 pm 06:03 PM

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

Can mysql handle multiple connections Can mysql handle multiple connections Apr 08, 2025 pm 03:51 PM

MySQL can handle multiple concurrent connections and use multi-threading/multi-processing to assign independent execution environments to each client request to ensure that they are not disturbed. However, the number of concurrent connections is affected by system resources, MySQL configuration, query performance, storage engine and network environment. Optimization requires consideration of many factors such as code level (writing efficient SQL), configuration level (adjusting max_connections), hardware level (improving server configuration).

HadiDB: A lightweight, horizontally scalable database in Python HadiDB: A lightweight, horizontally scalable database in Python Apr 08, 2025 pm 06:12 PM

HadiDB: A lightweight, high-level scalable Python database HadiDB (hadidb) is a lightweight database written in Python, with a high level of scalability. Install HadiDB using pip installation: pipinstallhadidb User Management Create user: createuser() method to create a new user. The authentication() method authenticates the user's identity. fromhadidb.operationimportuseruser_obj=user("admin","admin")user_obj.

Master SQL LIMIT clause: Control the number of rows in a query Master SQL LIMIT clause: Control the number of rows in a query Apr 08, 2025 pm 07:00 PM

SQLLIMIT clause: Control the number of rows in query results. The LIMIT clause in SQL is used to limit the number of rows returned by the query. This is very useful when processing large data sets, paginated displays and test data, and can effectively improve query efficiency. Basic syntax of syntax: SELECTcolumn1,column2,...FROMtable_nameLIMITnumber_of_rows;number_of_rows: Specify the number of rows returned. Syntax with offset: SELECTcolumn1,column2,...FROMtable_nameLIMIToffset,number_of_rows;offset: Skip

See all articles