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自动化办公 编程工具

相关推荐
喂完待续6 小时前
【Tech Arch】Spark为何成为大数据引擎之王
大数据·hadoop·python·数据分析·spark·apache·mapreduce
王者鳜錸7 小时前
PYTHON让繁琐的工作自动化-猜数字游戏
python·游戏·自动化
若天明8 小时前
深度学习-计算机视觉-微调 Fine-tune
人工智能·python·深度学习·机器学习·计算机视觉·ai·cnn
倔强青铜三9 小时前
苦练Python第39天:海象操作符 := 的入门、实战与避坑指南
人工智能·python·面试
一百天成为python专家10 小时前
Python循环语句 从入门到精通
开发语言·人工智能·python·opencv·支持向量机·计算机视觉
Sunhen_Qiletian10 小时前
朝花夕拾(五)--------Python 中函数、库及接口的详解
开发语言·python
三年呀10 小时前
标题:移动端安全加固:发散创新,筑牢安全防线引言:随着移动互联网
网络·python·安全
关山12 小时前
MCP实战
python·ai编程·mcp
悠哉悠哉愿意12 小时前
【Python语法基础学习笔记】if语句
笔记·python·学习
Q_Q196328847512 小时前
python的电影院座位管理可视化数据分析系统
开发语言·spring boot·python·django·flask·node.js·php