一个好用的Python备份mysql的脚本

前几天打算用Python写一个mysql脚本,上Google看了下老外写的,写的挺好的,原地址在http://tecadmin.net/python-script-for-mysql-database-backup/#,所以就给 copy过来了

 1 #!/usr/bin/python
 2 ###########################################################
 3 #
 4 # This python script is used for mysql database backup
 5 # using mysqldump utility.
 6 #
 7 # Written by : Rahul Kumar
 8 # Website: http://tecadmin.net
 9 # Created date: Dec 03, 2013
10 # Last modified: Dec 03, 2013
11 # Tested with : Python 2.6.6
12 # Script Revision: 1.1
13 #
14 ##########################################################
15
16 # Import required python libraries
17 import os
18 import time
19 import datetime
20
21 # MySQL database details to which backup to be done. Make sure below user having enough privileges to take databases backup.
22 # To take multiple databases backup, create any file like /backup/dbnames.txt and put databses names one on each line and assignd to DB_NAME variable.
23
24 DB_HOST = ‘localhost‘
25 DB_USER = ‘root‘
26 DB_USER_PASSWORD = ‘_root_user_password_‘
27 #DB_NAME = ‘/backup/dbnames.txt‘
28 DB_NAME = ‘db_name‘
29 BACKUP_PATH = ‘/backup/dbbackup/‘
30
31 # Getting current datetime to create seprate backup folder like "12012013-071334".
32 DATETIME = time.strftime(‘%m%d%Y-%H%M%S‘)
33
34 TODAYBACKUPPATH = BACKUP_PATH + DATETIME
35
36 # Checking if backup folder already exists or not. If not exists will create it.
37 print "creating backup folder"
38 if not os.path.exists(TODAYBACKUPPATH):
39     os.makedirs(TODAYBACKUPPATH)
40
41 # Code for checking if you want to take single database backup or assinged multiple backups in DB_NAME.
42 print "checking for databases names file."
43 if os.path.exists(DB_NAME):
44     file1 = open(DB_NAME)
45     multi = 1
46     print "Databases file found..."
47     print "Starting backup of all dbs listed in file " + DB_NAME
48 else:
49     print "Databases file not found..."
50     print "Starting backup of database " + DB_NAME
51     multi = 0
52
53 # Starting actual database backup process.
54 if multi:
55    in_file = open(DB_NAME,"r")
56    flength = len(in_file.readlines())
57    in_file.close()
58    p = 1
59    dbfile = open(DB_NAME,"r")
60
61    while p <= flength:
62        db = dbfile.readline()   # reading database name from file
63        db = db[:-1]         # deletes extra line
64        dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH + "/" + db + ".sql"
65        os.system(dumpcmd)
66        p = p + 1
67    dbfile.close()
68 else:
69    db = DB_NAME
70    dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH + "/" + db + ".sql"
71    os.system(dumpcmd)
72
73 print "Backup script completed"
74 print "Your backups has been created in ‘" + TODAYBACKUPPATH + "‘ directory"
# chmod +x dbbackup.py
# python dbbackup.py做定时任务执行:
0 2 * * * /usr/bin/python dbbackup.py
时间: 2024-10-09 01:22:36

一个好用的Python备份mysql的脚本的相关文章

Python 备份MySQL,并同步rsync server

# vim: tabstop=4 shiftwidth=4 softtabstop=4 # Copyright 2010 United States Government as represented by the # Administrator of the National Aeronautics and Space Administration. # Copyright 2011 Justin Santa Barbara # All Rights Reserved. # Copyright

python备份mysql脚本

今天简单的写了个python的mysql备份脚本,其实也不是很难呀.比shell简洁了很多! 开整: 注释都用英文写了,有些英语基础的朋友应该都可以看得懂了! #!/usr/bin/env python #backup the gtshop #author:ley #encoding=utf8 #date:2015-06 import os,sys,datetime,time from stat import * #mysqlbackup user User = 'root' #mysqlbac

一个小玩具:Python调用Mysql

1. ubuntu安装MySQL how to install:$ sudo apt-get install mysql-server$ sudo apt-get install mysql-client$ sudo apt-get install libmysqlclient-dev#python DB API$  sudo apt-get install python-mysqldb check:sudo netstat -tap | grep mysql run:mysql -u root

python备份mysql数据库

原本可以用shell完成的功能,现在学习python,就照抄照改.完成数据库备份. #!/usr/bin/python #-*-coding:utf-8-*- #MYSQL BACK import string,time,os,datetime import sys,logging,stat import subprocess #os.environ.get('PERONA_A') os.environ["PATH"]="/usr/local/mysql/bin/:"

python备份mysql数据库并发送邮件

#!/usr/bin/env python #coding:utf-8 #Write by JIANGLEI.YU #Update On  2016-01-24 20:26 import os import time import sys import datetime import smtplib import string from stat import * #Define DB  Informations HOST = '192.168.0.135' USER = 'root' PASS

python检测mysql状态脚本--参考

#!/usr/bin/env python #encoding:utf-8 import MySQLdb import paramiko  import sys,os from email.mime.text import MIMEText from email.header import Header import smtplib db1_ip="172.17.2.51" db2_ip="172.17.2.52" num = '' mmm_info = '' Io

自动备份mysql数据库脚本

#!/bin/bash#auto backup any one or all db#by zhaoyanfeng#2017-8-15 11:17:54 #defined ALL_DB=`mysql -uroot -p123456 -e "show databases;"| grep -v Database | xargs -n 1`MYSQL_USER=rootMYSQL_PASSWD="123456"MYSQL_BACKUP_DIR=/home/zhaoyanfe

Windows下MySQL自动备份的batch脚本

在Unix系统环境中可以通过各种Unix shell结合cron任务实现对MySQL的自动备份,那在Windows下要如何实现呢,其实很简单只要写好自定义的batch脚本在结合taskschd.msc(任务计划程序),就可以实现,最近需要修改调优Windows环境下的MySQL就顺便写了一个使用mysqldump做逻辑备份的batch脚本,如下: @echo off ::mysql_backup.bat set hour=%time:~0,2% if "%time:~0,1%"==&q

备份MySQL数据库

备份MySQL数据库脚本: #!/bin/bash # description: MySQL buckup shell script # author: lmj # web site: http://www.cnblogs.com/dscode st=$(date +%s) USER="root" # 用户名 PASSWORD="root" # 密码 DATABASE="myblogdb" # 数据库 MAIL="[email prot