zoukankan      html  css  js  c++  java
  • Python 数据库备份脚本

    #!/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"

  • 相关阅读:
    实现自己的类加载时,重写方法loadClass与findClass的区别
    MQ中将消息发送至远程队列的配置
    IOS开发之控件篇UITabBarControllor第一章
    IOS开发-图片尺寸
    IOS开发之进阶篇第一章
    AStar算法(转载)
    GEF
    WizardDialog 进度条使用记录
    Struts2学习笔记-jsp中引用struts2框架
    Struts2学习笔记-基本结构
  • 原文地址:https://www.cnblogs.com/Van214/p/7999069.html
Copyright © 2011-2022 走看看