跳转到主要内容

rsync-time-machine.py 是围绕 rsync 的包装程序,使其更容易创建和维护 Time Machine 风格的备份。

项目描述

Rsync Time Machine 🕰️💾

Build Coverage GitHub PyPI License Downloads Open Issues

介绍 rsync-time-machine.py - 这是一个 Python 版本的 rsync-time-backup 脚本,使用 rsync 提供Time Machine风格的备份。它将文件和目录的增量备份到您选择的任何位置。备份结构化以便可以轻松恢复任意时间点的任何文件。 🚀

它在 Linux、macOS 和 Windows(通过 WSL 或 Cygwin)上工作。与 Time Machine 相比,主要优势在于灵活性,因为它可以备份到/从任何文件系统,并且可以在任何平台上工作。您还可以无需问题地备份到 Truecrypt 驱动器。 😃

rsync-time-machine.py 已完全测试,无外部依赖(仅 Python ≥3.7 🐍),与 rsync-time-backup 完全兼容,提供美观的终端输出,并且完全类型化! 🎉

[目录] 📚

:star2: 功能

  • 📁 每个备份都存放在以当前时间戳命名的单独文件夹中。
  • 🔒 通过 SSH 将备份从/到远程位置。
  • 🔗 从一个备份到下一个备份未更改的文件将通过硬链接链接到上一个备份,从而节省空间。
  • ⚠️ 安全检查 - 只有当目标已明确标记为备份目标时,才会执行备份。
  • 🔄 备份恢复功能 - 如果备份失败或中断,工具将在下一次备份时从那里继续。
  • 🚫 排除文件 - 支持通过--exclude-from rsync参数进行基于模式的排除。
  • 🧹 根据可配置的过期策略自动清除旧备份。
  • 🔗 指向最新成功备份的“latest”符号链接。

:books: 使用方法

要使用rsync-time-machine.py,您需要提供源和目标路径,以及任何所需选项

rsync-time-machine --help

显示帮助信息

usage: rsync-time-machine [-h] [-p PORT] [-i ID_RSA] [--rsync-get-flags]
                          [--rsync-set-flags RSYNC_SET_FLAGS]
                          [--rsync-append-flags RSYNC_APPEND_FLAGS]
                          [--log-dir LOG_DIR] [--strategy STRATEGY]
                          [--no-auto-expire] [--allow-host-only]
                          [--exclude-from EXCLUDE_FROM] [-v]
                          src_folder dest_folder [exclusion_file]

A script for creating and managing time-stamped backups using rsync.

positional arguments:
  src_folder            Source folder for backup. Format: [USER@HOST:]SOURCE
  dest_folder           Destination folder for backup. Format:
                        [USER@HOST:]DESTINATION
  exclusion_file        Path to the file containing exclude patterns. Cannot
                        be used together with `--exclude-from`.

options:
  -h, --help            show this help message and exit
  -p PORT, --port PORT  SSH port.
  -i ID_RSA, --id_rsa ID_RSA
                        Specify the private ssh key to use.
  --rsync-get-flags     Display the default rsync flags that are used for
                        backup. If using remote drive over SSH, --compress
                        will be added.
  --rsync-set-flags RSYNC_SET_FLAGS
                        Set the rsync flags that are going to be used for
                        backup.
  --rsync-append-flags RSYNC_APPEND_FLAGS
                        Append the rsync flags that are going to be used for
                        backup.
  --log-dir LOG_DIR     Set the log file directory. If this flag is set,
                        generated files will not be managed by the script - in
                        particular they will not be automatically deleted.
                        Default: $HOME/.rsync-time-backup
  --strategy STRATEGY   Set the expiration strategy. Default: "1:1 30:7
                        365:30" means after one day, keep one backup per day.
                        After 30 days, keep one backup every 7 days. After 365
                        days keep one backup every 30 days.
  --no-auto-expire      Disable automatically deleting backups when out of
                        space. Instead, an error is logged, and the backup is
                        aborted.
  --allow-host-only     By default, the script expects a 'USER@HOST' pattern
                        for specifying SSH connections. When this flag is
                        used, it allows for the 'HOST' pattern without a
                        specified user. This is useful if you want to use
                        configurations from the `.ssh/config` file or rely on
                        the current username. Note: this option will not
                        enforce SSH usage, it only broadens the accepted input
                        formats.
  --exclude-from EXCLUDE_FROM
                        Path to the file containing exclude patterns.
                        Alternative to the positional `exclusion_file`. Not to
                        be used with `exclusion_file`.
  -v, --verbose         Enable verbose output. This will slow down the backup
                        process (in simple tests by 2x).

有关选项列表,请参阅原始的rsync-time-backup README,因为这些选项在Python端口中已保留。

:hammer_and_wrench: 安装

要安装rsync-time-machine.py,只需克隆存储库

pip install rsync-time-machine

然后使用它,例如rsync-time-machine --help

或者只需将脚本复制到您的计算机

wget https://raw.githubusercontent.com/basnijholt/rsync-time-machine.py/main/rsync_time_machine.py

然后使用它,例如./rsync_time_machine.py --help

:bulb: 示例

  • 将主目录备份到backup_drive
./rsync_time_machine.py /home /mnt/backup_drive
  • 带有排除列表的备份
./rsync_time_machine.py /home /mnt/backup_drive excluded_patterns.txt

有关更多示例和详细使用说明,请参阅原始的rsync-time-backup README。

:calendar: 备份过期逻辑

根据使用--strategy标志定义的简单过期策略自动删除备份集。默认策略是1:1 30:7 365:30。请参阅原始README以获取详细说明。

:page_facing_up: 排除文件

可以提供可选的排除文件作为第三个参数,与rsync的--exclude-from参数兼容。

rsync-time-machine.py中的--exclude-from选项允许您从备份过程中排除特定文件或目录。您可以提供包含要排除的文件或目录模式的排除文件。

📖🔽 点击此处展开有关--exclude-from的文档 🔽📖

以下是如何在rsync-time-machine.py中使用--exclude-from功能

  1. 在您喜欢的位置创建一个名为exclusion_file.txt(或您喜欢的任何名称)的文本文件。
  2. 将排除模式添加到文件中,每行一个模式。模式可以是字面字符串、通配符或字符范围。
  3. 保存文件。

要在使用rsync-time-machine.py执行备份时使用此排除文件,请将其作为命令中的第三个位置参数包含(或使用--exclude-from exclusion_file.txt)。例如

rsync-time-machine.py /home /mnt/backup_drive exclusion_file.txt

在此示例中,/home是源文件夹,/mnt/backup_drive是目标文件夹,而exclusion_file.txt包含排除模式。

以下是exclusion_file.txt的示例

+ /home/.fileA
- /home/.*
- /home/junk/

在此示例中

  • + /home/.fileA:包含来自home目录的文件.fileA
  • - /home/.*:从home目录中排除所有隐藏文件(以点开头的文件)。
  • - /home/junk/:从home目录中排除整个junk目录。

请记住,模式的顺序很重要,因为rsync自上而下读取文件,并对其遇到的第一个匹配模式执行操作。

有关更多信息,请参阅本教程

:lock: 内置锁

脚本设计为对于给定目录,只能有一个备份操作处于活动状态,以避免冲突。

:gear: Rsync 选项

要显示、添加或删除rsync选项,请使用--rsync-get-flags--rsync-append-flags--rsync-set-flags选项。

:no_entry_sign: 不自动备份过期

使用--no-auto-expire标志禁用默认行为,当空间不足时清除旧备份。

:arrows_counterclockwise: 如何恢复

从备份中恢复文件很简单,因为脚本在常规目录中创建备份。您可以使用类似于的命令轻松地将文件复制回原始目录:

rsync -aP /path/to/last/backup/ /path/to/restore/to/

考虑使用--dry-run选项来检查将要复制的内容。如果您想删除目标中存在但备份中不存在的文件,请使用--delete选项。在使用此选项时要格外小心,以免丢失数据。

您还可以使用任何文件资源管理器恢复文件,包括macOS上的Finder或命令行。

:star: 推荐于

:heart: 支持和贡献

我们感谢您的反馈和贡献!如果您遇到任何问题或对改进有建议,请在GitHub仓库中提交问题。我们也欢迎提交针对错误修复或新功能的拉取请求。

备份愉快!💾🕰️🎉

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码发行版

rsync-time-machine-1.3.3.tar.gz (21.2 kB 查看哈希)

上传 源代码

构建发行版

rsync_time_machine-1.3.3-py3-none-any.whl (18.7 kB 查看哈希)

上传 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页