From bfdb62929922d4928f0269f7c29e45dd884c43ad Mon Sep 17 00:00:00 2001 From: Mr Chen Date: Wed, 20 Feb 2019 18:31:08 +0800 Subject: [PATCH] Update backup.py --- scripts/backup.py | 86 ++++++++++++----------------------------------- 1 file changed, 22 insertions(+), 64 deletions(-) diff --git a/scripts/backup.py b/scripts/backup.py index 7cd0337ca..1032b9506 100755 --- a/scripts/backup.py +++ b/scripts/backup.py @@ -80,10 +80,12 @@ class backupTools: break def backupDatabase(self, name, count): - sql = db.Sql() - path = sql.table('databases').where('name=?', (name,)).getField('path') + db_path = public.getServerDir() + '/mysql' + db_name = 'mysql' + name = public.M('databases').dbPos(db_path, 'mysql').where( + 'name=?', (name,)).getField('name') startTime = time.time() - if not path: + if not name: endDate = time.strftime('%Y/%m/%d %X', time.localtime()) log = u"数据库[" + name + u"]不存在!" print(u"★[" + endDate + "] " + log) @@ -95,23 +97,23 @@ class backupTools: if not os.path.exists(backup_path): public.execShell("mkdir -p " + backup_path) - filename = backup_path + "/Db_" + name + "_" + \ + filename = backup_path + "/db_" + name + "_" + \ time.strftime('%Y%m%d_%H%M%S', time.localtime()) + ".sql.gz" import re - mysql_root = sql.table('config').where( + mysql_root = public.M('config').dbPos(db_path, db_name).where( "id=?", (1,)).getField('mysql_root') - mycnf = public.readFile('/etc/my.cnf') + mycnf = public.readFile(db_path + '/etc/my.cnf') rep = "\[mysqldump\]\nuser=root" sea = "[mysqldump]\n" subStr = sea + "user=root\npassword=" + mysql_root + "\n" mycnf = mycnf.replace(sea, subStr) if len(mycnf) > 100: - public.writeFile('/etc/my.cnf', mycnf) + public.writeFile(db_path + '/etc/my.cnf', mycnf) public.execShell( - "/www/server/mysql/bin/mysqldump --opt --default-character-set=utf8 " + name + " | gzip > " + filename) + db_path + "/bin/mysqldump --opt --default-character-set=utf8 " + name + " | gzip > " + filename) if not os.path.exists(filename): endDate = time.strftime('%Y/%m/%d %X', time.localtime()) @@ -121,78 +123,33 @@ class backupTools: u"----------------------------------------------------------------------------") return - mycnf = public.readFile('/etc/my.cnf') + mycnf = public.readFile(db_path + '/etc/my.cnf') mycnf = mycnf.replace(subStr, sea) if len(mycnf) > 100: - public.writeFile('/etc/my.cnf', mycnf) + public.writeFile(db_path + '/etc/my.cnf', mycnf) endDate = time.strftime('%Y/%m/%d %X', time.localtime()) outTime = time.time() - startTime - pid = sql.table('databases').where('name=?', (name,)).getField('id') + pid = public.M('databases').dbPos(db_path, db_name).where( + 'name=?', (name,)).getField('id') - sql.table('backup').add('type,name,pid,filename,addtime,size', (1, os.path.basename( + public.M('backup').add('type,name,pid,filename,addtime,size', (1, os.path.basename( filename), pid, filename, endDate, os.path.getsize(filename))) log = u"数据库[" + name + u"]备份成功,用时[" + str(round(outTime, 2)) + u"]秒" - public.WriteLog(u'计划任务', log) + public.writeLog(u'计划任务', log) print("★[" + endDate + "] " + log) print(u"|---保留最新的[" + count + u"]份备份") print(u"|---文件名:" + filename) # 清理多余备份 - backups = sql.table('backup').where( + backups = public.M('backup').where( 'type=? and pid=?', ('1', pid)).field('id,filename').select() num = len(backups) - int(count) if num > 0: for backup in backups: public.execShell("rm -f " + backup['filename']) - sql.table('backup').where('id=?', (backup['id'],)).delete() - num -= 1 - print(u"|---已清理过期备份文件:" + backup['filename']) - if num < 1: - break - - # 备份指定目录 - def backupPath(self, path, count): - sql = db.Sql() - startTime = time.time() - if path[-1:] == '/': - path = path[:-1] - name = os.path.basename(path) - backup_path = sql.table('config').where( - "id=?", (1,)).getField('backup_path') + '/path' - if not os.path.exists(backup_path): - os.makedirs(backup_path) - filename = backup_path + "/Path_" + name + "_" + \ - time.strftime('%Y%m%d_%H%M%S', time.localtime()) + '.tar.gz' - os.system("cd " + os.path.dirname(path) + " && tar zcvf '" + - filename + "' '" + os.path.basename(path) + "' > /dev/null") - - endDate = time.strftime('%Y/%m/%d %X', time.localtime()) - if not os.path.exists(filename): - log = u"目录[" + path + "]备份失败" - print(u"★[" + endDate + "] " + log) - print( - u"----------------------------------------------------------------------------") - return - - outTime = time.time() - startTime - sql.table('backup').add('type,name,pid,filename,addtime,size', - ('2', path, '0', filename, endDate, os.path.getsize(filename))) - log = u"目录[" + path + "]备份成功,用时[" + str(round(outTime, 2)) + "]秒" - public.writeLog(u'计划任务', log) - print(u"★[" + endDate + "] " + log) - print(u"|---保留最新的[" + count + u"]份备份") - print(u"|---文件名:" + filename) - - # 清理多余备份 - backups = sql.table('backup').where( - 'type=? and pid=? and name=?', ('2', 0, path)).field('id,filename').select() - num = len(backups) - int(count) - if num > 0: - for backup in backups: - public.execShell("rm -f " + backup['filename']) - sql.table('backup').where('id=?', (backup['id'],)).delete() + public.M('backup').where('id=?', (backup['id'],)).delete() num -= 1 print(u"|---已清理过期备份文件:" + backup['filename']) if num < 1: @@ -204,7 +161,10 @@ class backupTools: self.backupSite(site['name'], save) def backupDatabaseAll(self, save): - databases = public.M('databases').field('name').select() + db_path = public.getServerDir() + '/mysql' + db_name = 'mysql' + databases = public.M('databases').dbPos( + db_path, db_name).field('name').select() for database in databases: self.backupDatabase(database['name'], save) @@ -217,8 +177,6 @@ if __name__ == "__main__": backup.backupSiteAll(sys.argv[3]) else: backup.backupSite(sys.argv[2], sys.argv[3]) - elif type == 'path': - backup.backupPath(sys.argv[2], sys.argv[3]) elif type == 'database': if sys.argv[2] == 'ALL': backup.backupDatabaseAll(sys.argv[3])