A simple yet powerful command-line backup utility written in Python.
- 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
- Python 3.6 or higher
- No external dependencies (uses only the Python standard library)
git clone https://github.com/CHUKEPC/backup-script.git
cd backup-script
chmod +x backup.pyOptionally, add to your PATH:
sudo cp backup.py /usr/local/bin/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/locationpython backup.py listOutput:
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 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# Keep only the 5 most recent backups (default)
python backup.py rotate
# Keep only the 3 most recent backups
python backup.py rotate --keep 3usage: 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
-
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.
-
Metadata: All backup information is persisted in
metadata.jsoninside the backup directory, including file hashes, timestamps, and file counts. -
Compression: When enabled, backups are saved as ZIP archives with DEFLATE compression.
-
Rotation: Older backups are automatically removed based on the configured retention count, freeing disk space.
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/dailyAdd to crontab:
0 2 * * * /path/to/daily_backup.sh
python backup.py backup \
-s ~/.ssh \
-s ~/.config \
-s ~/Documents \
-d ~/important_backups \
-n config_backupbackups/
├── metadata.json # Backup metadata and file hashes
├── backup.log # Operation logs
├── backup_20241223_120000.zip # Compressed backup
└── backup_20241222_180000/ # Uncompressed backup
└── ...
Contributions are welcome! Feel free to submit a Pull Request.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License — see the LICENSE file for details.
CHUKEPC
- GitHub: @CHUKEPC
- Telegram: @ArtyomRoyal
- Inspired by rsync and other backup tools
- Built entirely with the Python standard library