python 備份與匯入mysql指令碼

2021-08-20 13:18:06 字數 2957 閱讀 7508

本文實現了用python 備份與匯入mysql指令碼,其中get_cmd_result方法可以返回命令的執行結果,預設1800秒不結束就殺死程序(已在linux下測試成功),get_scmd_result方法直接呼叫system命令比較簡單,但不能返回執行命令的輸出結果,實踐中建議使用get_cmd_result方法。注意在windwos下執行,mysql的命令需要帶絕對路徑,linux下不帶路徑。

#config.py

host= "192.168.101.186"

port="3306"

username = "root"

password = "root"

#database = "stockcn"

database = "stockcn"

dbcharset="utf8"

# -*- coding: utf-8 -*-

#!/usr/bin/env python

from  config  import *

import logging  

import time 

import  datetime

import  os

import  signal

from subprocess import popen, pipe

yesterday=datetime.date.fromordinal(datetime.date.today().toordinal()-1).strftime("%y-%m-%d")

report_date=datetime.date.today().strftime("%y-%m-%d")

basedir=os.path.dirname(os.path.realpath(__file__))

logfile="%s\\log\\st_%s_stock.log" %(basedir,datetime.datetime.now().strftime("%y-%m-%d"))  

logging.basicconfig(filename=logfile, level=logging.debug,format='%(asctime)s  %(message)s')     

def runbackup(backupcmd):

try:

get_cmd_result(backupcmd)

print("backup done with command ", backupcmd)

except:

print("backup failed with command", backupcmd) 

def runimportup(importcmd):

try:

get_cmd_result(importcmd)

print("backup done with command ", importcmd)

except:

print("backup failed with command", importcmd)        

def get_cmd_result(command, timeout=1800):    

try:

start = datetime.datetime.now()

process = popen(command.split(" "), shell =true,  stdout=pipe, stderr=pipe)

while process.poll() is none:

time.sleep(0.2)

now = datetime.datetime.now()

if (now - start).seconds> timeout:

os.kill(process.pid, signal.sigkill)

os.waitpid(-1, os.wnohang)

return "timeout to get command result"

return process.communicate()[0]

except:

return("run command  failed") 

def get_scmd_result(command, timeout=1800):

return os.system(command)

sql_file =os.path.join( basedir,"data" , 'st_2018-06-06_stock.sql')

dump_cmd_mod="d:\xampps\mysql\bin\mysqldump -h%s -p%s -u%s -p%s %s --default_character-set=%s >%s"

dump_cmd=dum_cmd_mod%(host,port,username,password,database,dbcharset,sql_file)

infor="dump begins with cmd :  %s  ! " % dump_cmd

logging.info(infor) 

ret=runimportup(dumpcmd) 

infor="import   sql data ended with return %s! " %ret

logging.info(infor)

import_cmd_mod="d:\\xampps\\mysql\\bin\mysql -h%s -p%s -u%s -p%s %s <%s"

import_cmd=import_cmd_mod%(host,port,username,password,database,sql_file)

infor="import begins with cmd :  %s  ! " % import_cmd

logging.info(infor)

ret=runimportup(import_cmd) 

infor="import   sql data ended with return %s! " %ret

logging.info(infor)

python讀取excel並匯入mysql

如果excel檔案內的資料是這樣的 idname agebirthday 1kate 112008 2 2 2mike 221997 4 4 3tom 331986 5 5 首先,匯入python包xlrd,以及它的乙個方法 xldate as tuple。import xlrd from xlrd...

mysql 備份與匯入

usr local mysql bin mysqldump uroot p1 l f test home test.sql 備份資料庫test l 鎖定當前正常匯出的表,其它表不影響,f,重新整理日誌後再開始匯出,主從設計時從庫複製主庫時重新整理日誌點再進行主從 usr local mysql bi...

資料備份與匯入

在mysql中進行資料備份的方法有兩種,一種是使用mysqldump程式 c mysql bin mysqldump uroot p opt databasename c databasename.sql opt選項還可啟用 add drop table選項,它將會在備份檔案的每條create ta...