zoukankan      html  css  js  c++  java
  • 在linux服务器上自动备份数据库

    脚本如下,没有任何问题:

    # !/usr/bin/python
    import os
    import time
    import datetime
    import pipes
    import glob

    DB_HOST = '192.168.44.158'
    DB_USER = 'root'
    DB_USER_PASSWORD = 'MyNewPass4!'
    DB_NAME = 'hanwo'
    BACKUP_PATH = '/mnt/dbbackup/mysql/'
    DATETIME = time.strftime('%Y%m%d-%H%M%S')
    TODAYBACKUPPATH = BACKUP_PATH + '/' + DATETIME

    try:
    os.stat(TODAYBACKUPPATH)
    except:
    os.mkdir(TODAYBACKUPPATH)
    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 listed in file" + DB_NAME)
    else:
    print("Databases file not found...")
    print("Starting backup of database" + DB_NAME)
    multi = 0

    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()
    db = db[:-1]
    dumpcmd = "mysqldump -h " + DB_HOST + " -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + pipes.qute(
    TODAYBACKUPPATH) + "/" + db + ".sql"
    os.system(dumpcmd)
    gzipcmd = "gzip" + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
    os.system(gzipcmd)
    p = p + 1
    dbfile.close()
    else:
    db = DB_NAME
    dumpcmd = "mysqldump -h " + DB_HOST + " -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + pipes.quote(
    TODAYBACKUPPATH) + "/" + db + ".sql"
    os.system(dumpcmd)
    gzipcmd = " gzip " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
    os.system(gzipcmd)
    path_file_number = glob.glob(BACKUP_PATH)
    print(len(path_file_number))
    print("")
    print("Backup script completed")
    print("Your backups have been created in '" + TODAYBACKUPPATH + "' directort")
  • 相关阅读:
    Linux脚本文件注释
    Linux三剑客之grep命令
    Linux获取本机IP
    Linux的cut命令详解
    Linux的wc命令详解
    Linux的uniq命令详解
    Linux的sort命令详解
    shell之a+b求和脚本的三种写法
    shell的文件比较运算符和字符串比较运算符
    shell中变量$系列的含义
  • 原文地址:https://www.cnblogs.com/liuage/p/10996547.html
Copyright © 2011-2022 走看看