代码拉取完成,页面将自动刷新
'''实行备份'''
import os
import sys
import shutil
class DoBackup:
## 指定备份路径backupDir,还有几个资料记录文件
def __init__(self, backupDir, logFiles = sys.path[0]+'\\logFiles.txt', logDirs = sys.path[0]+'\\logDirs.txt',
logInfo = sys.path[0]+'\\logInfo.txt'):
self._backupDir = backupDir
self._logFiles = logFiles
self._logDirs = logDirs
self._logInfo = logInfo
## 读取备份根目录
with open(self._logInfo, 'r') as f:
self._rootDir = (f.readline().split('='))[1].strip()
## 需要生成的目录表
self._dirList = [self._backupDir]
with open(self._logDirs, 'r') as f:
for line in f:
if line:
self._dirList.append(line.strip().replace(self._rootDir, self._backupDir))
## 需要复制的文件表
self._fileList = []
with open(self._logFiles, 'r') as f:
for line in f:
if line:
self._fileList.append(line.strip())
## 准备完成
return
## 生成备份目录
def _mkDirs(self):
for d in self._dirList:
try:
os.mkdir(d)
print("Path %s created!" % d)
except:
print("Path %s is already exist!" % d)
return
## 复制所有需要的文件
def _copyFiles(self):
for srcFile in self._fileList:
try:
dstFile = srcFile.replace(self._rootDir,self._backupDir)
shutil.copyfile(srcFile, dstFile)
print("File %s copy completed!" % srcFile)
except:
print("File %s copy failed!" % srcFile)
return
## 记录备份日志,把3个备份信息文件复制到备份文件夹
def _writeLog(self):
try:
shutil.copyfile(self._logInfo, self._backupDir+'\\'+'backup_logInfo.txt')
shutil.copyfile(self._logFiles, self._backupDir+'\\'+'backup_logFiles.txt')
shutil.copyfile(self._logDirs, self._backupDir+'\\'+'backup_logDir.txt')
print("Logs written")
except:
print("Failed to write logs")
return
## 运行主函数
def main(self):
msg = input("Read to backup files, it may take some time, input 1+enter to continue\n")
if not (msg.strip() == '1'):
print('Stop!')
return
self._mkDirs()
self._copyFiles()
self._writeLog()
print('Script finished!')
return
if __name__ == '__main__':
backupDir = r'D:\MyDocument\MyNote_backup'
db = DoBackup(backupDir)
db.main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。