Python教程:备份你的文件夹里面的数据

1.完全备份是最基本的备份类型,它涉及复制所有选定的数据到备份位置。无论文件是否自上次备份以来发生了变化,所有文件都会被复制。这种备份方式简单直接,确保了备份存储的数据总是最新的。

完全备份是通过递归复制源文件夹中的所有文件和子文件夹来实现的。我们使用os库来遍历文件夹,并使用shutil库的copy2函数来复制文件。

2.增量备份仅复制自上次备份以来发生变化的文件。这种备份方式比完全备份更高效,因为它只处理新的或修改过的数据。增量备份节省了时间和存储空间,但恢复数据时可能需要更多步骤,因为它需要结合之前的备份。

增量备份通过比较源文件和目标文件夹中相应文件的最后修改时间来实现。如果目标文件夹中不存在文件,或文件自上次备份以来已更改,则该文件将被复制。

3.镜像备份创建数据的精确副本,包括所有文件和文件夹的结构。这种备份方式不仅复制所有数据,还包括目标文件夹中不存在于源文件夹中的任何额外文件的删除。镜像备份提供了一种恢复到特定时间点的完整数据副本的方法。

在我们的软件中,镜像备份首先删除目标文件夹中不在源文件夹中的所有项目,然后复制源文件夹中的所有内容。

python 复制代码
# -*- coding: utf-8 -*-
# @Author : 小红牛
# 微信公众号:WdPython
import tkinter as tk
from tkinter import filedialog, messagebox
import os
import shutil
import filecmp

def choose_source():
    # 用户选择源文件夹
    folder_path = filedialog.askdirectory()
    if folder_path:
        source_path.set(folder_path)
        label_source.config(text=folder_path)

def choose_destination():
    # 用户选择目标文件夹
    folder_path = filedialog.askdirectory()
    if folder_path:
        destination_path.set(folder_path)
        label_destination.config(text=folder_path)


def full_backup(source, destination):
    if not os.path.exists(destination):
        os.makedirs(destination)

    for item in os.listdir(source):
        source_item = os.path.join(source, item)
        destination_item = os.path.join(destination, item)

        if os.path.isdir(source_item):
            if not os.path.exists(destination_item):
                os.makedirs(destination_item)
            full_backup(source_item, destination_item)
        else:
            shutil.copy2(source_item, destination_item)

def incremental_backup(source, destination):
    if not os.path.exists(destination):
        os.makedirs(destination)

    for item in os.listdir(source):
        source_item = os.path.join(source, item)
        destination_item = os.path.join(destination, item)

        if os.path.isdir(source_item):
            if not os.path.exists(destination_item):
                os.makedirs(destination_item)
            incremental_backup(source_item, destination_item)
        else:
            if not os.path.exists(destination_item) or not filecmp.cmp(source_item, destination_item, shallow=False):
                shutil.copy2(source_item, destination_item)

def mirror_backup(source, destination):
    if not os.path.exists(destination):
        os.makedirs(destination)

    destination_items = set(os.listdir(destination))
    source_items = set(os.listdir(source))

    for item in destination_items - source_items:
        destination_item = os.path.join(destination, item)
        if os.path.isdir(destination_item):
            shutil.rmtree(destination_item)
        else:
            os.remove(destination_item)

    for item in source_items:
        source_item = os.path.join(source, item)
        destination_item = os.path.join(destination, item)

        if os.path.isdir(source_item):
            if not os.path.exists(destination_item):
                os.makedirs(destination_item)
            mirror_backup(source_item, destination_item)
        else:
            shutil.copy2(source_item, destination_item)

def backup_files():
    source = source_path.get()
    destination = destination_path.get()

    if not source or not destination:
        messagebox.showerror("错误", "请选择有效的源和目标文件夹")
        return

    try:
        if backup_type.get() == "完全备份":
            full_backup(source, destination)
        elif backup_type.get() == "增量备份":
            incremental_backup(source, destination)
        elif backup_type.get() == "镜像备份":
            mirror_backup(source, destination)

        messagebox.showinfo("成功", "备份完成")
    except Exception as e:
        messagebox.showerror("错误", str(e))

# 设置主窗口
root = tk.Tk()
root.title('文件夹备份软件')
root.geometry('400x240')  # 设置窗口大小

source_path = tk.StringVar()
destination_path = tk.StringVar()
backup_type = tk.StringVar(value="完全备份")

# 创建界面元素
tk.Button(root, text="选择源文件夹", command=choose_source).pack()
label_source = tk.Label(root, text="", wraplength=300)
label_source.pack()

tk.Button(root, text="选择目标文件夹", command=choose_destination).pack()
label_destination = tk.Label(root, text="", wraplength=300)
label_destination.pack()

tk.Radiobutton(root, text="完全备份", variable=backup_type, value="完全备份").pack()
tk.Radiobutton(root, text="增量备份", variable=backup_type, value="增量备份").pack()
tk.Radiobutton(root, text="镜像备份", variable=backup_type, value="镜像备份").pack()

tk.Button(root, text="开始备份", command=backup_files).pack()
root.mainloop()

完毕!!感谢您的收看

----------★★历史博文集合★★----------
我的零基础Python教程,Python入门篇 进阶篇 视频教程 Py安装py项目 Python模块 Python爬虫 Json Xpath 正则表达式 Selenium Etree CssGui程序开发 Tkinter Pyqt5 列表元组字典数据可视化 matplotlib 词云图 Pyecharts 海龟画图 Pandas Bug处理 电脑小知识office自动化办公 编程工具

相关推荐
chimchim661 天前
Azure Data Factory (ADF)‌ 之databricks使用
后端·python·flask
ふり1 天前
测试的“三重境界”:黑盒、白盒、灰盒的对比与实践
网络·python·测试工具·需求分析
开源量化GO1 天前
多合约期货策略目标持仓怎么更新不容易乱
python·区块链
Sylvia33.1 天前
2026世界杯全套数据API接入教程:WebSocket实时进球推送实例
java·网络·python·websocket·网络协议
zyl837211 天前
Python 线性代数:矩阵与向量
开发语言·python·机器学习
金銀銅鐵1 天前
用 Tkinter 实现一个简单的干支纪年计算器
后端·python
AC赳赳老秦1 天前
OpenClaw+MySQL 深度应用:自动生成建表语句、索引优化建议与数据迁移脚本
开发语言·数据库·人工智能·python·mysql·算法·openclaw
西贝爱学习1 天前
旅游推荐数据集.csv
python·数据集·旅游
qcx231 天前
【AI Daily 2026-06-05】「持续迭代」已成为 2026 年 Agent 研究的核心命题
人工智能·python·agent
2601_961194021 天前
2026四级词汇闪过电子版|高频词+真题词速记PDF
数据库·python·django·pdf·pygame