import subprocess, sqlite3,os, time, hashlib,datetime,shutil class WindowsBackup: def __init__(self, origin_path, to_path, name, is_full, log_path): self.conn = sqlite3.connect('ybm.db') self.cursor = self.conn.cursor() self.origin_path =origin_path self.to_path = to_path self.name = name self.is_full = is_full self.log_path = log_path def backup(self, task_id): afiles = [] bfiles = [] # 判断备份文件路径是否存在,如不存在,则创建 if os.path.exists(self.to_path): # return True pass else: os.mkdir(self.to_path) # 获取日志文件路径 logs_path = os.path.abspath(os.path.join(self.log_path,'task_id.txt')) # 获取备份文件名 path = os.path.normpath(self.to_path) filename1 = path.split(os.sep)[-1] # 需要备份的源文件大小 beisize = 0 for root, dirs, files in os.walk(self.origin_path): for file in files: filename = os.path.join(root, file) beisize += os.path.getsize(filename) # 使用MD5判断备份文件与源文件内容是否相同 hasher1 = hashlib.md5() afile1 = open(filename, 'rb') buf1 = afile1.read() hasher1.update(buf1) c = hasher1.hexdigest() afiles.append(c) # 如果self.is_full等于零,那么备份是全量备份 if self.is_full == 0: # 备份开始时间 start = time.localtime() subprocess.call([r'robocopy', self.origin_path, self.to_path, self.na
python调用windows robocopy命令备份还原文件
最新推荐文章于 2023-07-10 12:54:16 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)