Python实现备份MySQL数据库的方法示例
作者:郭一军_guoyJoe 发布时间:2024-01-27 18:20:04
标签:Python,备份,MySQL数据库
本文实例讲述了Python实现备份MySQL数据库的方法。分享给大家供大家参考,具体如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#导入模块
import MySQLdb
import time
import datetime
import os
"""
Purpose: 备份数据库
Created: 2015/5/12
Modified:2015/5/12
@author: guoyJoe
"""
dbUser='root'
dbPasswd='root'
dbHost='192.168.1.6'
dbCharset = 'utf8'
backupDir = '/u02/backup/mysql'
backupDate = time.strftime("%Y%m%d")
#查出MySQL中所有的数据库名称
sqlStr1 = "show databases like 'db%'"
try:
connDB= MySQLdb.connect("192.168.1.6","root","root","test" )
connDB.select_db('test')
curSql1=connDB.cursor()
curSql1.execute(sqlStr1)
allDatabase = curSql1.fetchall()
print 'The database backup to start! %s' %time.strftime('%Y-%m-%d %H:%M:%S')
for db in allDatabase:
dbName = db[0]
fileName = '%s/%s_%s.sql' %(backupDir,backupDate,dbName)
print fileName
if os.path.exists(fileName):
os.remove(fileName)
os.system("mysqldump -h%s -u%s -p%s %s --default_character-set=%s > %s/%s_%s.sql" %(dbHost,dbUser,dbPasswd,dbName,dbCharset,backupDir,backupDate,dbName))
print 'The database backup success! %s' %time.strftime('%Y-%m-%d %H:%M:%S')
#异常
except MySQLdb.Error,err_msg:
print "MySQL error msg:",err_msg
希望本文所述对大家Python程序设计有所帮助。
来源:http://blog.csdn.net/guoyjoe/article/details/45848057
0
投稿
猜你喜欢
- 解决中文乱码问题项目地址 github: https://github.com/Delgan/loguru文档:https://loguru
- 亲身实践安装mysql,用时居然花费了三个小时,在有那么多教程的情况下,依然在不该花费时间的路上浪费了太多时间。希望这篇文章能够帮助大家少走
- 在 python 中除了用 opencv,也可以用 matplotlib 和 PIL 这两个库操作图片。本人偏爱 matpoltlib,因为
- 高级查询在数据库中用得是最频繁的,也是应用最广泛的。基本常用查询--selectselect * from student;--all 查询
- 本文实例为大家分享了python绘制柱形图的具体代码,供大家参考,具体内容如下#柱形图import pandasimport numpyim
- 求3721,163,1,4832,1980,2008,68686688,9999,17173,5173,8848中最大的数明白后,试着求一下
- 菜鸟一枚,写着试了试,虽说有点杂乱,但还是能用,我是在linux下运行的大致说下过程:1、把需要ping的网段中所有ip存到数组中(我是放到
- PDOStatement::fetchColumnPDOStatement::fetchColumn — 从结果集中的下一行返回单独的一列。
- CREATE PROCEDURE 创建存储过程,存储过程是保存起来的可以接受和返回用户提供的参数的 Transact-SQL 语句的集合。
- 本文实例讲述了php实现的日历程序。分享给大家供大家参考。具体如下:<?php /* * php 输出日历程序 */ header(&
- 一般常规的我们保存数据为dict类型时,系统会自动帮我们排序;但有时我们想按照输入顺序的key:value保存到dict中,而不想要改变顺序
- NumPy 支持的几类矩阵乘法也很重要。元素级乘法你已看过了一些元素级乘法。你可以使用 multiply 函数或 * 运算符来实现。回顾一下
- 基本开发环境· Python 3.6· Pycharm相关模块使用import requestsimport timefrom tkinte
- 在密码学中,凯撒密码(或称恺撒加密、恺撒变换、变换加密)是一种最简单且最广为人知的加密技术。它是一种替换加密的技术。这个加密方法是以恺撒的名
- 前言目前学习python几个月了,由于自己比较喜欢跑马拉松,已经跑过了很多场比赛,前些天就写了个简单的爬虫爬取了网上三千多场马拉松比赛的报名
- 如今,随着深度学习的发展,python已经成为了深度学习研究中第一语言。绝大部分的深度学习工具包都有python的版本,很多重要算法都有py
- 用类和对象实现一个银行账户的资金交易管理, 包括存款、取款和打印交易详情, 交易详情中包含每次交易的时间、存款或者取款的金额、每次交易后的余
- 伴随着自然语言技术和机器学习技术的发展,越来越多的有意思的自然语言小项目呈现在大家的眼前,聊天机器人就是其中最典型的应用,今天小编就带领大家
- tkinter获取复选框(Checkbutton)的值定义GUI:from tkinter import *# 初始化Tk()myWindo
- 前段时间在网上找了一个“完美的”JavaScript对象克隆的函数,感觉还不错,但随后便出现了一些问题,发现这个克隆并不好用,在使用发现了如