#!/usr/bin/python
##########################################################
# Created date: 2017/12/7
# Last modified: 201712/7
# Tested with : Python 2.6.6
# Script Revision: Mysql_dump.py
##########################################################

import os
import time
import datetime

DB_HOST = '192.168.1.203'
DB_USER = 'root'
DB_USER_PASSWORD = '123456'
DB_NAME = '/data/DB_back/dbnames.txt'
#DB_NAME = 'Var'
Back_up_dir = '/data/DB_back/'
DATETIME = time.strftime('%Y%m%d')
Dir_backup = Back_up_dir + DATETIME

print "creating backup folder"
if not os.path.exists(Dir_backup):
os.makedirs(Dir_backup)
# Code for checking if you want to take single database backup or assinged multiple backups in DB_NAME.
print "checking for databases names file."
if os.path.exists(DB_NAME):
file1 = open(DB_NAME)
multi = 1
print "Databases file found..."
print "Starting backup of all dbs listed in file " + DB_NAME
else:
print "Databases file not found..."
print "Starting backup of database " + DB_NAME
multi = 0
# Starting actual database backup process.
if multi:
in_file = open(DB_NAME,"r")
flength = len(in_file.readlines())
in_file.close()
p = 1
dbfile = open(DB_NAME,"r")
while p <= flength:
db = dbfile.readline() # reading database name from file
db = db[:-1] # deletes extra line
dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + Dir_backup + "/" + db + ".sql"
os.system(dumpcmd)
p = p + 1
dbfile.close()
else:
db = DB_NAME
dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + Dir_backup + "/" + db + ".sql"
os.system(dumpcmd)
print "Backup script completed"
print "Your backups has been created in '" + Dir_backup + "' directory"

Python 数据库备份脚本的更多相关文章

  1. Python数据库备份脚本

    Python数据库备份脚本 #!/usr/bin/env python # author: liudong # -*- coding: utf-8 -*- # filename: db_bak.py ...

  2. Windows下MySQL数据库备份脚本(一)

    说明: MySQL数据库安装目录:C:\Program Files\MySQL\MySQL Server 5.0 MySQL数据库存放目录:C:\Program Files\MySQL\MySQL S ...

  3. Windows下MySQL数据库备份脚本(二)

    说明: MySQL数据库安装目录:C:\Program Files\MySQL\MySQL Server 5.0 MySQL数据库存放目录:C:\Program Files\MySQL\MySQL S ...

  4. mysql数据库备份脚本

    mysql数据库备份脚本 mysql数据库分库备份脚本:[root@localhost tmp]# cat mysql.sh #!/bin/bash USER=root PASSWORD=joy4yo ...

  5. Shell脚本使用汇总整理——达梦数据库备份脚本

    Shell脚本使用汇总整理——达梦数据库备份脚本 Shell脚本使用的基本知识点汇总详情见连接: https://www.cnblogs.com/lsy-blogs/p/9223477.html 脚本 ...

  6. SQLSERVER 数据库备份脚本-支持多库备份

    原文:SQLSERVER 数据库备份脚本-支持多库备份 <pre name="code" class="sql">--变量定义 DECLARE @b ...

  7. Ubuntu Server下MySql数据库备份脚本代码

    明: 我这里要把MySql数据库存放目录/var/lib/mysql下面的pw85数据库备份到/home/mysql_data里面,并且保存为mysqldata_bak_2012_04_11.tar. ...

  8. mysql数据库备份脚本一例

    例子,mysql数据库备份脚本.vim mysql.sh #!/bin/bash DAY=`date +%Y-%m-%d` //日期以年月日显示并赋予DAY变量 SIZE=`du -sh /var/l ...

  9. PostgreSQL 数据库备份脚本

    PostgreSQL 数据库备份脚本 #!/bin/bash # PG家目录(/opt/postgresql/pg96/) PG_HOME=${PGHOME} # pg数据库连接信息 PG_HOST= ...

随机推荐

  1. LINUX 笔记-条件测试

    格式:test condition 文件测试状态 -d 目录 -s 文件长度大于0,非空 -f 正规文件 -w 可写 -l 符号链接 -u 文件有suid位设置 -r 可读 -x 可执行 字符串测试 ...

  2. LeetCode 63. Unique Path II(所有不同路径之二)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. JavaAPI 中 <E> 与 <T> 的含义

    今天看集合的代码,发现在泛型的使用时的区别,Collection<E>.List<E>,而Iterator<T>,那么<E>和<T>含义有什 ...

  4. 微信小程序之bindtap事件绑定传参

    wxml代码: <view class='fen'> <text bindtap='prev' data-page="{{pageDang}}">上一页&l ...

  5. 谈谈今年很火的区块链 CDN

    2017 年初,区块链被越来越多的人知道.区块链的概念其实很早就被提出来,曾经有人说过"区块链技术被认为是继蒸汽机.电力.互联网之后,下一代颠覆性的核心技术. 如果说蒸汽机释放了人们的生产力 ...

  6. Again Stone Game

    Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains ...

  7. Problem D

    Problem Description An entropy encoder is a data encoding method that achieves lossless data compres ...

  8. html 自定义标签使用实现方法

    通过指定html命名空间的名字来定义自定义标签:默认的一些标签p div等都在html默认的命名空间下.而自定义的标签可以放在自定义的命名空间下,可通过xmlns:命名空间名 来指定,而自定义标签需要 ...

  9. 算法:JavaScript两数之和

    题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  10. webmagic学习-使用注解编写爬虫

    写在前面: 官方文档:http://webmagic.io/docs/zh/posts/ch5-annotation/README.html WebMagic支持使用独有的注解风格编写一个爬虫,引入w ...