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

相关推荐
Dr.Kun11 分钟前
【鲲码园Python】基于pytorch的鸟品种分类系统(25类)
pytorch·python·分类
是有头发的程序猿31 分钟前
Python爬虫实战:面向对象编程在淘宝商品数据抓取中的应用
开发语言·爬虫·python
萑澈1 小时前
Windows系统Anaconda/Miniconda的安装、配置、基础使用、清理缓存空间和Pycharm/VSCode配置指南
python
Onebound_Ed1 小时前
Python爬虫进阶:面向对象设计构建高可维护的1688商品数据采集系统
开发语言·爬虫·python
阿蔹1 小时前
JavaWeb-Selenium 配置以及Selenim classnotfound问题解决
java·软件测试·python·selenium·测试工具·自动化
万粉变现经纪人2 小时前
如何解决 pip install 代理报错 407 Proxy Authentication Required 问题
windows·python·pycharm·beautifulsoup·bug·pandas·pip
李剑一2 小时前
Python学习笔记3
python
luod2 小时前
Python包
python
Mr Lee_2 小时前
Smali 文件生成dex装箱算法整合
开发语言·python·算法
电饭叔2 小时前
《python语言程序设计》2018版--第8章14题利用字符串输入作为一个信用卡号之一(Luhn算法解释)
android·java·python