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

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

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

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

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

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

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

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

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-22 21:32:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-22 21:32:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-22 21:32:05       82 阅读
  4. Python语言-面向对象

    2024-04-22 21:32:05       91 阅读

热门阅读

  1. CSS中的display: flex;

    2024-04-22 21:32:05       40 阅读
  2. TCP为什么需要3次握手?

    2024-04-22 21:32:05       29 阅读
  3. 安全开发之碰撞检测与伤害计算逻辑

    2024-04-22 21:32:05       44 阅读
  4. Kubernetes Kafka 系列|MirrorMaker 2 同步数据

    2024-04-22 21:32:05       40 阅读
  5. android 内存优化

    2024-04-22 21:32:05       32 阅读