Update backup.py

pull/258/head
midoks 3 years ago
parent 099971f768
commit eb52435ce7
  1. 25
      plugins/mysql-yum/scripts/backup.py

@ -20,17 +20,23 @@ sys.path.append(chdir + '/class/core')
import mw
import db
import time
import re
'''
DEBUG:
python3 /www/server/mdserver-web/plugins/mysql-yum/scripts/backup.py database admin 3
'''
class backupTools:
def backupDatabase(self, name, count):
db_path = mw.getServerDir() + '/mysql-ya'
db_path = mw.getServerDir() + '/mysql-yum'
db_name = 'mysql'
name = mw.M('databases').dbPos(db_path, 'mysql').where(
find_name = mw.M('databases').dbPos(db_path, 'mysql').where(
'name=?', (name,)).getField('name')
startTime = time.time()
if not name:
if not find_name:
endDate = time.strftime('%Y/%m/%d %X', time.localtime())
log = "数据库[" + name + "]不存在!"
print("★[" + endDate + "] " + log)
@ -45,11 +51,11 @@ class backupTools:
filename = backup_path + "/db_" + name + "_" + \
time.strftime('%Y%m%d_%H%M%S', time.localtime()) + ".sql.gz"
import re
mysql_root = mw.M('config').dbPos(db_path, db_name).where(
"id=?", (1,)).getField('mysql_root')
mycnf = mw.readFile(db_path + '/etc/my.cnf')
my_conf_path = db_path + '/etc/my.cnf'
mycnf = mw.readFile(my_conf_path)
rep = "\[mysqldump\]\nuser=root"
sea = "[mysqldump]\n"
subStr = sea + "user=root\npassword=" + mysql_root + "\n"
@ -57,8 +63,9 @@ class backupTools:
if len(mycnf) > 100:
mw.writeFile(db_path + '/etc/my.cnf', mycnf)
mw.execShell(
db_path + "/bin/mysqldump --opt --default-character-set=utf8 " + name + " | gzip > " + filename)
cmd = db_path + "/bin/usr/bin/mysqldump --defaults-file=" + my_conf_path + " --single-transaction --quick --default-character-set=utf8 " + \
name + " | gzip > " + filename
mw.execShell(cmd)
if not os.path.exists(filename):
endDate = time.strftime('%Y/%m/%d %X', time.localtime())
@ -80,7 +87,7 @@ class backupTools:
mw.M('backup').add('type,name,pid,filename,addtime,size', (1, os.path.basename(
filename), pid, filename, endDate, os.path.getsize(filename)))
log = "数据库[" + name + "]备份成功,用时[" + str(round(outTime, 2)) + u"]秒"
log = "数据库[" + name + "]备份成功,用时[" + str(round(outTime, 2)) + "]秒"
mw.writeLog('计划任务', log)
print("★[" + endDate + "] " + log)
print("|---保留最新的[" + count + "]份备份")
@ -101,7 +108,7 @@ class backupTools:
break
def backupDatabaseAll(self, save):
db_path = mw.getServerDir() + '/mysql-ya'
db_path = mw.getServerDir() + '/mysql-yum'
db_name = 'mysql'
databases = mw.M('databases').dbPos(
db_path, db_name).field('name').select()

Loading…
Cancel
Save