文件备份OK

pull/310/head
midoks 2 years ago
parent 77425dfc48
commit a8a9e9033e
  1. 23
      class/core/mw.py
  2. 44
      scripts/backup.py

@ -1060,7 +1060,7 @@ def makeConf():
file = getRunDir() + '/data/json/config.json' file = getRunDir() + '/data/json/config.json'
if not os.path.exists(file): if not os.path.exists(file):
c = {} c = {}
c['title'] = '猫王面板' c['title'] = '大圣面板'
c['home'] = 'http://github/midoks/mdserver-web' c['home'] = 'http://github/midoks/mdserver-web'
c['recycle_bin'] = True c['recycle_bin'] = True
c['template'] = 'default' c['template'] = 'default'
@ -1453,3 +1453,24 @@ def getMyORMDb():
import ormDb import ormDb
o = ormDb.ORM() o = ormDb.ORM()
return o return o
# ---------------------------------------------------------------------------------
# 打印相关
# ---------------------------------------------------------------------------------
def echoStart(tag):
print("=" * 90)
print("★开始{}[{}]".format(tag, formatDate()))
print("=" * 90)
def echoEnd(tag):
print("=" * 90)
print("{}完成[{}]".format(tag, formatDate()))
print("=" * 90)
print("\n")
def echoInfo(msg):
print("|-{}".format(msg))

@ -181,10 +181,17 @@ class backupTools:
for database in databases: for database in databases:
self.backupDatabase(database['name'], save) self.backupDatabase(database['name'], save)
def findPathName(self, path, filename):
f = os.scandir(path)
l = []
for ff in f:
if ff.name.find(filename) > -1:
l.append(ff.name)
return l
def backupPath(self, path, count): def backupPath(self, path, count):
print("=" * 90)
print("★开始备份[{}]".format(mw.formatDate())) mw.echoStart('备份')
print("=" * 90)
backup_path = mw.getBackupDir() + '/path' backup_path = mw.getBackupDir() + '/path'
if not os.path.exists(backup_path): if not os.path.exists(backup_path):
@ -195,15 +202,36 @@ class backupTools:
dirname, mw.formatDate("%Y%m%d_%H%M%S")) dirname, mw.formatDate("%Y%m%d_%H%M%S"))
dfile = os.path.join(backup_path, fname) dfile = os.path.join(backup_path, fname)
p_size = mw.getPathSize(path)
stime = time.time()
cmd = "cd " + os.path.dirname(path) + " && tar zcvf '" + dfile + "' '" + dirname + "' 2>{err_log} 1> /dev/null".format( cmd = "cd " + os.path.dirname(path) + " && tar zcvf '" + dfile + "' '" + dirname + "' 2>{err_log} 1> /dev/null".format(
err_log='/tmp/backup_err.log') err_log='/tmp/backup_err.log')
mw.execShell(cmd) mw.execShell(cmd)
print("=" * 90) tar_size = os.path.getsize(dfile)
print("☆备份完成[{}]".format(mw.formatDate()))
print("=" * 90) mw.echoInfo('备份目录:' + path)
print("\n") mw.echoInfo('目录已备份到:' + dfile)
mw.echoInfo("目录大小:{}".format(mw.toSize(p_size)))
mw.echoInfo("开始压缩文件:{}".format(mw.formatDate(times=stime)))
mw.echoInfo("文件压缩完成,耗时{:.2f}秒,压缩包大小:{}".format(
time.time() - stime, mw.toSize(tar_size)))
mw.echoInfo('保留最新的备份数:' + count + '')
backups = self.findPathName(backup_path, 'path_{}'.format(dirname))
num = len(backups) - int(count)
backups.sort()
if num > 0:
for backup in backups:
abspath_bk = backup_path + "/" + backup
mw.execShell("rm -f " + abspath_bk)
mw.echoInfo("|---已清理过期备份文件:" + abspath_bk)
num -= 1
if num < 1:
break
mw.echoEnd('备份')
if __name__ == "__main__": if __name__ == "__main__":
backup = backupTools() backup = backupTools()

Loading…
Cancel
Save