使用Python简单实现数据备份和更新,视频和照片可以快速备份了

硬盘的寿命有限,但是上面的数据是无价的,硬盘有殒命的风险,所以需要定期备份数据,

网盘虽然可以存视频照片等数据,但是这个云上数据的私密性~~~,呵呵,懂的人都明白。

昨天突然发现台式机起不来了,读硬盘嘎嘎响,快见马克思了。赶紧准备备份数据,发现上次备份是半年前的了,很多文件都更新过了,还有不少新增的文件,直接拷贝文件夹合并,效率太低。网上的备份工具很多,功能太强大,使用复杂,收费,…………,不如自己写个简单的

今天有空就调试了一个Python拷贝新增和更新过文件的脚本,自用也当笔记,放上来给有需要的人参考,直接上代码

import os
import datetime
import time
import os
import shutil


FileSourcePath=r"D:\WorkSpace"               #源文件目录
FileDestinationPath=r"E:\WorkSpace"        #新文件穆罗
CopyFileListPath=r"D:\Test\CopyFileList.txt"    #拷贝的文件列表
LastTimestampFile =os.path.join(FileDestinationPath,"LastTimestamp.txt")

MakeCopyFileList=1                             #需要执行创建拷贝列表操作  0 不需要执行 1 需要执行
DoCopyFileList=1                            #需要执行拷贝操作  0 不需要执行 1 需要执行
OnlyCopyNewFile=1                           #1 只更新上次备份时间戳之后的文件,速度会更快  0 只要文件不存在就会备份,不会忽略上次备份的时间前更新的文件

LastTimestamp=0
FileList=[]

#递归遍历目录
def WalkFiles(filePath):
    global FileList
    for root, dirs, files in os.walk(filePath):
        for file in files:
            fullfile=os.path.join(root, file)
            if(not (fullfile in FileList)):
                FileList.append(fullfile)
            # print(os.path.join(root, file))
        for dir in dirs:
            WalkFiles(os.path.join(root, dir))
            #print(os.path.join(root, dir))
#读取文件行数
def count_lines(file_path):
    with open(file_path, 'r') as file:
        lines = file.readlines()
    return len(lines)

#获取文件创建日期
def get_creation_date(file_path):
    timestamp = os.path.getctime(file_path)
    creation_date = time.ctime(timestamp)
    return creation_date

#获取文件更新日期
def get_file_modified_date(file_path):
    stat_info = os.stat(file_path)
    modified_timestamp = stat_info.st_mtime
    modified_date = datetime.datetime.fromtimestamp(modified_timestamp)
    return modified_date,modified_timestamp

if OnlyCopyNewFile==1:
    try:
        TimestampFile=open(LastTimestampFile,'r')    ##读取上次备份时间戳
        TimestampStr = TimestampFile.read()
        TimestampFile.close()
        LastTimestamp=float(TimestampStr)
    except:
        LastTimestamp=0
 #   print(type(LastTimestamp))

#生成需要拷贝文件列表,文件不存在或者文件比较老,则进入拷贝列表
if MakeCopyFileList==1:
    WalkFiles(FileSourcePath)
    # print(len(FileList))
    CopyFile=open(CopyFileListPath,'w')
    for file in FileList:
        OldFilePath=file
        NewFilePath=OldFilePath.replace(FileSourcePath,FileDestinationPath)
        # print(NewFilePath)
        #print(OldFilePath)
        # print(get_file_modified_date(OldFilePath))
        # print(get_creation_date(OldFilePath))
        # print(os.path.getsize(OldFilePath))
        # print(os.path.exists(OldFilePath))
        if(os.path.exists(NewFilePath)!=True):
            CopyFile.write(OldFilePath+"\n")
            print("New "+ OldFilePath)
        else:
            # OldSize=os.path.getsize(OldFilePath)
            # NewSize=os.path.getsize(NewFilePath)
            OldModifiedTimesStr,OldModifiedTimeStamp=get_file_modified_date(OldFilePath)
            if(OnlyCopyNewFile and LastTimestamp!=0):
                if OldModifiedTimeStamp<LastTimestamp:  #早于上次的备份文件忽略
                    continue
            NewModifiedTimesStr, NewModifiedTimeStamp = get_file_modified_date(NewFilePath)
            # print(OldSize,NewSize)
            # print(OldModifiedTimesStr, NewModifiedTimesStr)
            #print(OldModifiedTimeStamp>NewModifiedTimeStamp)
            if(OldModifiedTimeStamp>NewModifiedTimeStamp):
                CopyFile.write(OldFilePath+"\n")
                print("Update "+OldFilePath)
    CopyFile.close()

#执行拷贝操作
if DoCopyFileList==1:
    AllFileCount=count_lines(CopyFileListPath)
    FileCount=0
    CopyFile = open(CopyFileListPath, 'r')
    for line in CopyFile:
        FileCount=FileCount+1
        OldFilePath = line.replace("\n", "")
        print(FileCount,AllFileCount,OldFilePath)

        NewFilePath=OldFilePath.replace(FileSourcePath,FileDestinationPath)
        DirPath=os.path.dirname(NewFilePath)
        if (os.path.exists(DirPath) != True):
            os.makedirs(DirPath)
        # print(OldFilePath)
        # print(NewFilePath)
        # 复制文件
        try:
            shutil.copy2(OldFilePath,NewFilePath)
            # print("File copied successfully.")
            # If source and destination are same
        except shutil.SameFileError:
            print(OldFilePath,"Source and destination represents the same file.")
            # If there is any permission issue
        except PermissionError:
            print(OldFilePath,"Permission denied.")
            # For other errors
        except:
            print(OldFilePath,"Error occurred while copying file.")
    CopyFile.close()
    TimestampFile = open(LastTimestampFile, 'w')   #生成备份时间戳
    current_timestamp = time.time()
    TimestampFile.write(str(current_timestamp))
    TimestampFile.close()



  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hyhsandy1803

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值