import os,shutil,time,subprocess
def exec_shell(cmd, ignore_err=False):
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = process.communicate()
retcode = process.poll()
if retcode == 0 or ignore_err:
return output, err
else:
return -1000, f'execute "{cmd}" failed'
def strDuration(duration):
return f'{duration//60}分钟{int(duration%60)}秒'
def cmpUpdated(tim,d):
for root,ds,fs in os.walk(d):
for f in fs:
mtime=os.path.getmtime(os.path.join(root,f))
if mtime>tim:
return True
return False
hexoPath='C:/Users/tellw/hexo'
searxPath='C:/Users/tellw/Desktop/searx'
os.chdir(searxPath)
output,_=exec_shell('python convert.py')
os.chdir(os.path.dirname(__file__))
if os.path.exists('hexoGenerateTime.txt'):
with open('hexoGenerateTime.txt') as f:
lastTime=float(f.read())
else:
lastTime=time.time()
if cmpUpdated(lastTime,hexoPath+'/source'):
with open('hexoGenerateTime.txt','w') as f:
f.write(str(time.time()))
os.chdir(hexoPath)
output,err=exec_shell('hexo generate')
else:
output=None
os.chdir(os.path.dirname(__file__))
if output==-1000:
with open('hexoGenerateErr.log','w',encoding='utf8') as f:
f.write(err)
start=time.time()
# backupd={'C:\\Users\\tellw\\hexo\\public':['C:/Users/tellw/blog'],'C:/Users/tellw/blog':['D:/backup/blog','C:\\Users\\tellw\\apps\\nginx-1.22.1\\html\\blog','E:/backup/blog'],'C:/Users/tellw/open_title':['D:/backup/open_title','E:/backup/open_title'],'C:\\Users\\tellw\\bt-platform-company':['D:/backup/bt-platform-company','E:/backup/bt-platform-company']}
backupd={'C:\\Users\\tellw\\hexo\\public':['C:/Users/tellw/blog'],'C:/Users/tellw/blog':['D:/backup/blog','C:\\Users\\tellw\\apps\\nginx-1.22.1\\html\\blog','E:/backup/blog'],'C:/Users/tellw/open_title':['D:/backup/open_title','E:/backup/open_title'],'C:\\Users\\tellw\\bt-platform-company':['D:/backup/bt-platform-company','E:/backup/bt-platform-company'], 'C:\\Users\\tellw\\hexo':['D:/backup/hexo','E:/backup/hexo']}
errStr=''
for budk in backupd:
if not os.path.exists(budk):
print(f'{budk}目录不存在')
errStr+=f'{budk}目录不存在\n'
continue
for budv in backupd[budk]:
if not os.path.exists(budv):
os.makedirs(budv)
for root,dirs,files in os.walk(budk):
print(f'entering {root}')
for budv in backupd[budk]:
for di in dirs:
if not os.path.exists(os.path.join(budv,os.path.relpath(root,budk),di)):
os.mkdir(os.path.join(budv,os.path.relpath(root,budk),di))
print(f'creating directory {os.path.join(budv,os.path.relpath(root,budk),di)}')
for file in files:
try:
if not os.path.exists(os.path.join(budv,os.path.relpath(root,budk),file)):
shutil.copy(os.path.join(root,file),os.path.join(budv,os.path.relpath(root,budk),file))
print(f'copying {os.path.join(root,file)} to {os.path.join(budv,os.path.relpath(root,budk),file)}')
else:
# 源文件修改时间晚于目标文件修改时间,需要在备份文件夹里更新源文件更改的内容
smtime=os.path.getmtime(os.path.join(root,file))
tmtime=os.path.getmtime(os.path.join(budv,os.path.relpath(root,budk),file))
if smtime>tmtime:
shutil.copy(os.path.join(root,file),os.path.join(budv,os.path.relpath(root,budk),file))
print(f'copying {os.path.join(root,file)} to {os.path.join(budv,os.path.relpath(root,budk),file)}')
except PermissionError:
# os.remove(os.path.join(budv,os.path.relpath(root,budk),file))
# shutil.copy(os.path.join(root,file),os.path.join(budv,os.path.relpath(root,budk),file))
# print(f'copying {os.path.join(root,file)} to {os.path.join(budv,os.path.relpath(root,budk),file)}')
continue
with open('err.log','w',encoding='utf8') as f:
f.write(errStr)
print(f'花费时间{strDuration(time.time()-start)}')
我们常在计算机上工作,工作中的一些重要数据需要备份,以防特殊情况下数据消失,而累积的数据经过日积月累,一旦这些数据丢失,会遭受到难以估量的损失。日常进行数据的备份是个好习惯。backd是个字典,配置源目录要备份的其他磁盘的目录,可以有多个备份目录。接着脚本进行拷贝和更新。
最新的脚本首先执行了hexo generate,生成hexo渲染的静态网页文件,每次都动态地执行此任务,备份最新的博客文件,之后再是本身已有的应该保存的文件的备份。
参考链接:python获取文件创建和修改时间
本文创建于2023.1.9/16.22,修改于2023.3.27/18.48