1 Star 0 Fork 0

流风一剑1990/FileBackUp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
DoBackUp.py 2.45 KB
一键复制 编辑 原始数据 按行查看 历史
setycyas 提交于 2018-11-13 14:28 . Upload DoBackUp.py FileFilter.py
'''实行备份'''
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()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/liufengyijian/FileBackUp.git
git@gitee.com:liufengyijian/FileBackUp.git
liufengyijian
FileBackUp
FileBackUp
master

搜索帮助