Skip to content

About

Incremental backup utility with integrity verification, rotation, and compression

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Backup Utility

🇷🇺 Русская версия

Python 3.6+ License: MIT

A simple yet powerful command-line backup utility written in Python.


Features

  • Incremental Backups — Only copy changed files to save time and storage space
  • Compression — Compress backups into ZIP archives with DEFLATE
  • Automatic Rotation — Keep only the N most recent backups
  • Integrity Verification — MD5 hash checking to detect file modifications
  • Detailed Logging — Track all operations with comprehensive log output
  • Simple CLI — Intuitive command-line interface using argparse

Installation

Requirements

  • Python 3.6 or higher
  • No external dependencies (uses only the Python standard library)

Setup

git clone https://github.com/CHUKEPC/backup-script.git
cd backup-script
chmod +x backup.py

Optionally, add to your PATH:

sudo cp backup.py /usr/local/bin/backup

Usage

Create a Backup

# Back up files or directories
python backup.py backup -s /path/to/folder /path/to/file.txt

# Backup with a custom name
python backup.py backup -s /path/to/data -n my_backup

# Full backup (non-incremental)
python backup.py backup -s /path/to/data --no-incremental

# Uncompressed backup
python backup.py backup -s /path/to/data --no-compress

# Custom backup destination directory
python backup.py backup -s /path/to/data -d /backup/location

List Backups

python backup.py list

Output:

Name                      Date                 Type            Files      Size (MB)
------------------------------------------------------------------------------------------
backup_20241223_120000    20241223_120000      incremental     42         125.50
backup_20241222_180000    20241222_180000      full            38         110.25

Restore a Backup

# Restore to current directory
python backup.py restore -n backup_20241223_120000

# Restore to a specific location
python backup.py restore -n backup_20241223_120000 -r /restore/path

Rotate Backups

# Keep only the 5 most recent backups (default)
python backup.py rotate

# Keep only the 3 most recent backups
python backup.py rotate --keep 3

Command Reference

usage: backup.py [-h] [-s SOURCE [SOURCE ...]] [-d DESTINATION] [-n NAME]
                 [-r RESTORE_PATH] [--no-incremental] [--no-compress]
                 [--keep KEEP] [--version]
                 {backup,restore,list,rotate}

positional arguments:
  {backup,restore,list,rotate}
                        Action to perform

options:
  -h, --help            Show this help message
  -s SOURCE [SOURCE ...], --source SOURCE [SOURCE ...]
                        Paths to back up (for the backup action)
  -d DESTINATION, --destination DESTINATION
                        Backup storage directory (default: backups)
  -n NAME, --name NAME  Backup name (for backup/restore actions)
  -r RESTORE_PATH, --restore-path RESTORE_PATH
                        Restore destination path (default: current directory)
  --no-incremental      Disable incremental mode (perform a full backup)
  --no-compress         Disable ZIP compression
  --keep KEEP           Number of backups to retain during rotation
  --version             Show program version

How It Works

  1. Incremental Backups: The utility computes MD5 hashes for every file and stores them in metadata. On subsequent runs, only files whose hashes have changed are copied.

  2. Metadata: All backup information is persisted in metadata.json inside the backup directory, including file hashes, timestamps, and file counts.

  3. Compression: When enabled, backups are saved as ZIP archives with DEFLATE compression.

  4. Rotation: Older backups are automatically removed based on the configured retention count, freeing disk space.


Examples

Daily Backup Script

Create a cron job for daily backups:

#!/bin/bash
# daily_backup.sh

python /path/to/backup.py backup \
  -s /home/user/documents \
  -s /home/user/projects \
  -d /backup/daily

python /path/to/backup.py rotate --keep 7 -d /backup/daily

Add to crontab:

0 2 * * * /path/to/daily_backup.sh

Back Up Important Configuration

python backup.py backup \
  -s ~/.ssh \
  -s ~/.config \
  -s ~/Documents \
  -d ~/important_backups \
  -n config_backup

File Structure

backups/
├── metadata.json              # Backup metadata and file hashes
├── backup.log                 # Operation logs
├── backup_20241223_120000.zip # Compressed backup
└── backup_20241222_180000/    # Uncompressed backup
    └── ...

Contributing

Contributions are welcome! Feel free to submit a Pull Request.

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License — see the LICENSE file for details.

Author

CHUKEPC

Acknowledgements

  • Inspired by rsync and other backup tools
  • Built entirely with the Python standard library

About

Incremental backup utility with integrity verification, rotation, and compression

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages