Skip to content

basnijholt/rsync-time-machine.py

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Rsync Time Machine 🕰️💾

Build Coverage GitHub PyPI License Downloads Open Issues

Introducing rsync-time-machine.py - a Python port of the rsync-time-backup script, offering Time Machine-style backups using rsync. It creates incremental backups of files and directories to the destination of your choice. The backups are structured in a way that makes it easy to recover any file at any point in time. 🚀

It works on Linux, macOS, and Windows (via WSL or Cygwin). The main advantage over Time Machine is flexibility, as it can backup from/to any filesystem and works on any platform. You can also backup to a Truecrypt drive without any problem. 😃

rsync-time-machine.py is fully tested, has no external dependencies (only Python ≥3.7 🐍), is fully compatible with rsync-time-backup, offers pretty terminal output, and is fully typed! 🎉

[ToC] 📚

🌟 Features

  • 📁 Each backup is in its own folder named after the current timestamp.
  • 🔒 Backup to/from remote destinations over SSH.
  • 🔗 Files that haven't changed from one backup to the next are hard-linked to the previous backup, saving space.
  • ⚠️ Safety check - the backup will only happen if the destination has explicitly been marked as a backup destination.
  • 🔄 Resume feature - if a backup has failed or was interrupted, the tool will resume from there on the next backup.
  • 🚫 Exclude file - support for pattern-based exclusion via the --exclude-from rsync parameter.
  • 🧹 Automatically purge old backups based on a configurable expiration strategy.
  • 🔗 "latest" symlink that points to the latest successful backup.

📚 Usage

To use rsync-time-machine.py, you'll need to provide source and destination paths, along with any desired options:

rsync-time-machine --help

Shows the help message:

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] [-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.

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.
  -v, --verbose         Enable verbose output.

Please refer to the original rsync-time-backup README for a list of options, as they have been preserved in the Python port.

🛠️ Installation

To install rsync-time-machine.py, simply clone the repository:

pip install rsync-time-machine

and use it like rsync-time-machine --help

Or just copy the script to your computer:

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

and use it like ./rsync_time_machine.py --help

💡 Examples

  • Backup the home folder to backup_drive:
./rsync_time_machine.py /home /mnt/backup_drive
  • Backup with exclusion list:
./rsync_time_machine.py /home /mnt/backup_drive excluded_patterns.txt

For more examples and detailed usage instructions, please refer to the original rsync-time-backup README.

📆 Backup Expiration Logic

Backup sets are automatically deleted following a simple expiration strategy defined with the --strategy flag. The default strategy is 1:1 30:7 365:30. Please see the original README for a detailed explanation.

📄 Exclusion File

An optional exclude file can be provided as a third parameter, compatible with the --exclude-from parameter of rsync.

🔒 Built-in Lock

The script is designed so that only one backup operation can be active for a given directory, avoiding conflicts.

⚙️ Rsync Options

To display, add, or remove rsync options, use the --rsync-get-flags, --rsync-append-flags, or --rsync-set-flags options.

🚫 No Automatic Backup Expiration

Use the --no-auto-expire flag to disable the default behavior of purging old backups when out of space.

🔄 How to Restore

Restoring files from the backup is simple, as the script creates a backup in a regular directory. You can easily copy the files back to the original directory using a command like:

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

Consider using the --dry-run option to check what exactly is going to be copied. If you want to delete files that exist in the destination but not in the backup, use the --delete option. Be extra cautious when using this option to avoid data loss.

You can also restore files using any file explorer, including Finder on macOS or the command line.

❤️ Support and Contributions

We appreciate your feedback and contributions! If you encounter any issues or have suggestions for improvements, please file an issue on the GitHub repository. We also welcome pull requests for bug fixes or new features.

Happy backing up! 💾🕰️🎉