374 lines
14 KiB
Markdown
374 lines
14 KiB
Markdown
# Linux System Backuper (`linux-backuper`)
|
|
|
|
[](https://gitea.wired.synology.me/soda64/linux-backuper/releases)
|
|
[](https://www.python.org/)
|
|
[](LICENSE)
|
|
[](#architecture)
|
|
|
|
An enterprise-grade, deterministic Linux system snapshot, configuration backup, and safe restoration utility written in **pure Python >= 3.6** with **zero mandatory external pip dependencies**.
|
|
|
|
`linux-backuper` captures the comprehensive state of a Linux host—including hardware profiles, storage layout (LVM, partitions, mountpoints), network configuration, installed packages, kernel parameters, and configuration trees (`/etc/`)—into self-contained, cryptographically verifiable archives with time-bucket retention and local-first remote synchronization.
|
|
|
|
---
|
|
|
|
## Table of Contents
|
|
|
|
- [Key Features](#key-features)
|
|
- [Architecture & Guarantees](#architecture--guarantees)
|
|
- [Installation](#installation)
|
|
- [Debian / Ubuntu (`.deb`)](#debian--ubuntu-deb)
|
|
- [RHEL / AlmaLinux / Fedora (`.rpm`)](#rhel--almalinux--fedora-rpm)
|
|
- [Universal Installer Script](#universal-installer-script)
|
|
- [Manual / Standalone Tarball](#manual--standalone-tarball)
|
|
- [Quick Start](#quick-start)
|
|
- [Core CLI Commands](#core-cli-commands)
|
|
- [`snapshot` - Creating Snapshots](#snapshot---creating-snapshots)
|
|
- [`list` - Listing Snapshots](#list---listing-snapshots)
|
|
- [`show` - Deep Manifest & State Inspection](#show---deep-manifest--state-inspection)
|
|
- [`diff` - Live Configuration Drift Detection](#diff---live-configuration-drift-detection)
|
|
- [`extract` - Selective Single-File Extraction](#extract---selective-single-file-extraction)
|
|
- [`restore` - Safe System State Restoration](#restore---safe-system-state-restoration)
|
|
- [`prune` - Retention Policy Enforcement](#prune---retention-policy-enforcement)
|
|
- [`sync` - Remote Storage Synchronization](#sync---remote-storage-synchronization)
|
|
- [`completion` - Shell Tab Completions](#completion---shell-tab-completions)
|
|
- [Automation & Systemd Integration](#automation--systemd-integration)
|
|
- [Security & Guardrails](#security--guardrails)
|
|
- [Documentation Triad](#documentation-triad)
|
|
- [License](#license)
|
|
|
|
---
|
|
|
|
## Key Features
|
|
|
|
- **Zero External Dependencies:** Runs natively out of the box on standard Python 3.6+ without requiring `pip install`, virtualenvs, or external wheels.
|
|
- **Fast Pre-Indexed Archives:** Single `.tar.gz` archive with embedded `manifest.json` placed as the first entry in the tar stream, enabling sub-millisecond metadata reads without decompressing the full archive.
|
|
- **Broad Linux Distribution Support:** Tested and verified across Debian 11/12/13, Ubuntu 20.04/22.04/24.04, RHEL/AlmaLinux/CentOS Stream 8/9/10, Fedora 40+, and Arch Linux.
|
|
- **Safe Restoration Engine:**
|
|
- Dry-run by default (exit code 5) prevents accidental state modifications.
|
|
- Mandatory `--force` flag required for active filesystem changes.
|
|
- Automatic `pre_restore_<timestamp>` safety snapshot before any file modification.
|
|
- Atomic file replacement (`.tmp` write followed by rename) and automatic SELinux label restoration.
|
|
- Protection against restoring `/etc/fstab` (requires explicit `--include-fstab`) and dynamic mounts (`/proc`, `/sys`, `/dev`, `/run`).
|
|
- **Comprehensive State Inspector:** Collects OS release, package inventory (`rpm`, `dpkg`, `pacman`), system services (`systemctl`), user/group tables, block devices (`lsblk`, `lvs`), network interfaces and routes, sysctl values, and hardware telemetry.
|
|
- **Time-Bucket Retention (GFS):** Pruning policies supporting `--keep-last`, `--keep-daily`, `--keep-weekly`, and `--keep-monthly` with independent local and remote retention rules.
|
|
- **Local-First Remote Sync:** Snapshots remain securely on the host first, then sync to remote SSH/rsync targets or NFS/SMB storage with resume support and atomic directory updates.
|
|
- **Diagnostic Sanitization:** `--sanitize` flag automatically redacts password hashes, shadow entries, API tokens, and private SSL/SSH keys for safe off-host diagnostic sharing.
|
|
|
|
---
|
|
|
|
## Architecture & Guarantees
|
|
|
|
```
|
|
linux-backuper/
|
|
├── core/
|
|
│ ├── archiver.py # Tarball streamer, zip-slip defense, manifest indexer
|
|
│ ├── inspector.py # Hardware, OS, package manager, network, and storage inspector
|
|
│ ├── profiles.py # Profile manager (quick, system, full, custom)
|
|
│ ├── diff.py # Unified diff engine and filesystem drift detector
|
|
│ ├── restore.py # Safe atomic restore, pre-restore snapshots, SELinux relabel
|
|
│ ├── retention.py # Grandfather-Father-Son (GFS) time-bucket pruning engine
|
|
│ ├── transport.py # Local-first remote transport sync (rsync, scp, local)
|
|
│ ├── lock.py # System flock concurrency controller (/var/run/linux-backuper.lock)
|
|
│ └── completions.py # Native bash/zsh shell auto-completion generator
|
|
└── cli.py # Section-8 compliant command-line interface
|
|
```
|
|
|
|
### Snapshot Archive Layout:
|
|
```
|
|
snapshot_<hostname>_<timestamp>.tar.gz
|
|
├── manifest.json # Stream position 0: Metadata, inspector telemetry, SHA-256
|
|
└── data/ # Preserved directory tree with exact mode, uid, gid, mtime
|
|
└── etc/
|
|
├── nginx/...
|
|
└── systemd/...
|
|
```
|
|
|
|
---
|
|
|
|
## Installation
|
|
|
|
### Debian / Ubuntu (`.deb`)
|
|
|
|
From the Gitea Package Registry:
|
|
```bash
|
|
sudo curl -fsSL https://gitea.wired.synology.me/api/packages/soda64/debian/repository.key \
|
|
-o /etc/apt/trusted.gpg.d/gitea-wired.asc
|
|
|
|
echo "deb [signed-by=/etc/apt/trusted.gpg.d/gitea-wired.asc] https://gitea.wired.synology.me/api/packages/soda64/debian stable main" \
|
|
| sudo tee /etc/apt/sources.list.d/gitea-wired.list
|
|
|
|
sudo apt update
|
|
sudo apt install linux-backuper
|
|
```
|
|
|
|
Or install downloaded `.deb` package directly:
|
|
```bash
|
|
sudo dpkg -i linux-backuper_1.0.0_all.deb
|
|
```
|
|
|
|
### RHEL / AlmaLinux / Fedora (`.rpm`)
|
|
|
|
From the Gitea Package Registry:
|
|
```bash
|
|
sudo tee /etc/yum.repos.d/gitea-wired.repo << 'EOF'
|
|
[gitea-wired]
|
|
name = Wired LAB Gitea RPM Repository
|
|
baseurl = https://gitea.wired.synology.me/api/packages/soda64/rpm
|
|
enabled = 1
|
|
gpgcheck = 0
|
|
EOF
|
|
|
|
sudo dnf install linux-backuper
|
|
```
|
|
|
|
Or install downloaded `.rpm` package directly:
|
|
```bash
|
|
sudo rpm -ivh linux-backuper-1.0.0-1.fc44.noarch.rpm
|
|
```
|
|
|
|
### Universal Installer Script
|
|
|
|
Use the standalone POSIX-compliant installer provided with releases:
|
|
```bash
|
|
tar -xzf linux-backuper-1.0.0.tar.gz
|
|
cd linux-backuper-1.0.0
|
|
sudo ./packaging/install.sh --install
|
|
```
|
|
|
|
To uninstall:
|
|
```bash
|
|
sudo ./packaging/install.sh --uninstall
|
|
```
|
|
|
|
### Manual / Standalone Tarball
|
|
|
|
Because `linux-backuper` is pure Python, you can run it directly without installation:
|
|
```bash
|
|
tar -xzf linux-backuper-1.0.0.tar.gz
|
|
python3 linux-backuper-1.0.0/bin/linux-backuper --version
|
|
```
|
|
|
|
---
|
|
|
|
## Quick Start
|
|
|
|
```bash
|
|
# 1. Create a routine configuration snapshot
|
|
sudo linux-backuper snapshot
|
|
|
|
# 2. List all existing snapshots
|
|
linux-backuper list
|
|
|
|
# 3. Detect live configuration drift against the latest snapshot
|
|
linux-backuper diff latest
|
|
|
|
# 4. Extract a single modified file without unpacking everything
|
|
sudo linux-backuper extract latest --file /etc/hosts --out /tmp/hosts.restored
|
|
|
|
# 5. Simulate restoration with dry-run protection (exit code 5)
|
|
sudo linux-backuper restore latest --dry-run
|
|
```
|
|
|
|
---
|
|
|
|
## Core CLI Commands
|
|
|
|
### `snapshot` - Creating Snapshots
|
|
|
|
Creates a compressed snapshot archive containing configuration trees and system state telemetry.
|
|
|
|
```bash
|
|
# Quick profile (default, covers /etc)
|
|
sudo linux-backuper snapshot --profile quick
|
|
|
|
# System profile (covers /etc, /boot, and kernel telemetry)
|
|
sudo linux-backuper snapshot --profile system
|
|
|
|
# Full disaster recovery profile (covers /etc, /boot, critical /var)
|
|
sudo linux-backuper snapshot --profile full
|
|
|
|
# Custom directory targets
|
|
sudo linux-backuper snapshot --paths /etc/nginx /etc/ssl /var/www/config
|
|
|
|
# Diagnostic export with credential redaction
|
|
sudo linux-backuper snapshot --sanitize
|
|
|
|
# Snapshot with immediate remote upload and retention enforcement
|
|
sudo linux-backuper snapshot \
|
|
--push backupuser@nas.lan:/volume1/backups \
|
|
--keep-last 7 --keep-daily 14
|
|
```
|
|
|
|
### `list` - Listing Snapshots
|
|
|
|
Displays a formatted table of all local snapshot archives in `/var/backups/linux-backuper/`.
|
|
|
|
```bash
|
|
linux-backuper list
|
|
```
|
|
|
|
Sample output:
|
|
```text
|
|
SNAPSHOT ID PROFILE FILES SIZE CREATED
|
|
snapshot_alma-crash_20261009_2015 quick 1,420 14.2 MB 2026-10-09 20:15:22
|
|
snapshot_alma-crash_20261009_1200 system 2,185 48.7 MB 2026-10-09 12:00:01
|
|
```
|
|
|
|
### `show` - Deep Manifest & State Inspection
|
|
|
|
Reads the front of the archive to display recorded host telemetry, storage mounts, network state, and packages.
|
|
|
|
```bash
|
|
# General host telemetry and snapshot metadata
|
|
linux-backuper show latest
|
|
|
|
# List all packages installed at the time of the snapshot
|
|
linux-backuper show latest --packages
|
|
|
|
# List all files archived in the snapshot
|
|
linux-backuper show latest --files
|
|
```
|
|
|
|
### `diff` - Live Configuration Drift Detection
|
|
|
|
Compares files and packages between the live running host and a snapshot, or between two different snapshots.
|
|
|
|
```bash
|
|
# Compare live filesystem against the latest snapshot
|
|
linux-backuper diff latest
|
|
|
|
# Output unified diff (context patches) for modified files
|
|
linux-backuper diff latest -u
|
|
|
|
# Compare two specific snapshots against each other
|
|
linux-backuper diff snapshot_host_20261001_1200 snapshot_host_20261009_1200
|
|
```
|
|
|
|
### `extract` - Selective Single-File Extraction
|
|
|
|
Extracts an individual configuration file from an archive without decompressing or restoring the rest of the snapshot. Protected against Zip Slip path traversal.
|
|
|
|
```bash
|
|
sudo linux-backuper extract latest --file /etc/nginx/nginx.conf --out /tmp/nginx.conf.bak
|
|
```
|
|
|
|
### `restore` - Safe System State Restoration
|
|
|
|
Restores files from a snapshot back into the host filesystem.
|
|
|
|
```bash
|
|
# 1. Preview changes (Dry-Run by default, exits with code 5)
|
|
sudo linux-backuper restore latest --dry-run
|
|
|
|
# 2. Live execution (Requires --force, auto-creates pre_restore_<ts> safety snapshot)
|
|
sudo linux-backuper restore latest --force
|
|
|
|
# 3. Restore including /etc/fstab (Skipped by default for safety)
|
|
sudo linux-backuper restore latest --force --include-fstab
|
|
```
|
|
|
|
### `prune` - Retention Policy Enforcement
|
|
|
|
Applies Grandfather-Father-Son (GFS) time-bucket pruning to retain only required snapshots.
|
|
|
|
```bash
|
|
# Simulate pruning plan
|
|
sudo linux-backuper prune --keep-last 5 --keep-daily 7 --keep-weekly 4 --dry-run
|
|
|
|
# Apply pruning plan
|
|
sudo linux-backuper prune --keep-last 5 --keep-daily 7 --keep-weekly 4 --force
|
|
```
|
|
|
|
### `sync` - Remote Storage Synchronization
|
|
|
|
Synchronizes local snapshot archives to remote storage using SSH/rsync or local network mounts.
|
|
|
|
```bash
|
|
# Sync over SSH/rsync with remote pruning
|
|
sudo linux-backuper sync \
|
|
--target backupuser@nas.lan:/volume1/backups/ \
|
|
--remote-keep-last 14 --remote-keep-daily 30
|
|
|
|
# Sync to a mounted NFS / SMB path
|
|
sudo linux-backuper sync --target /mnt/nas/backups/
|
|
```
|
|
|
|
### `completion` - Shell Tab Completions
|
|
|
|
`linux-backuper` includes built-in tab completion generators for `bash` and `zsh`:
|
|
|
|
```bash
|
|
# Bash completion
|
|
sudo linux-backuper completion bash | sudo tee /etc/bash_completion.d/linux-backuper
|
|
|
|
# Zsh completion
|
|
linux-backuper completion zsh > ~/.zsh/completion/_linux-backuper
|
|
```
|
|
|
|
---
|
|
|
|
## Automation & Systemd Integration
|
|
|
|
`linux-backuper` packages ship with native systemd timer and service units for scheduled snapshots:
|
|
|
|
1. **Service Unit (`/usr/lib/systemd/system/linux-backuper.service`):**
|
|
Executes automated snapshots and retention pruning:
|
|
```ini
|
|
[Unit]
|
|
Description=Linux Backuper Automated Snapshot and Prune
|
|
After=network.target
|
|
|
|
[Service]
|
|
Type=oneshot
|
|
ExecStart=/usr/bin/linux-backuper snapshot --profile quick --keep-last 7 --keep-daily 14
|
|
```
|
|
|
|
2. **Timer Unit (`/usr/lib/systemd/system/linux-backuper.timer`):**
|
|
Runs daily at 02:00 with randomized delay:
|
|
```ini
|
|
[Unit]
|
|
Description=Daily Linux Backuper Trigger
|
|
|
|
[Timer]
|
|
OnCalendar=*-*-* 02:00:00
|
|
RandomizedDelaySec=15m
|
|
Persistent=true
|
|
|
|
[Install]
|
|
WantedBy=timers.target
|
|
```
|
|
|
|
3. **Enable & Start:**
|
|
```bash
|
|
sudo systemctl daemon-reload
|
|
sudo systemctl enable --now linux-backuper.timer
|
|
sudo systemctl list-timers linux-backuper.timer
|
|
```
|
|
|
|
---
|
|
|
|
## Security & Guardrails
|
|
|
|
- **Strict Access Control:** Local snapshots are created with `0600` file modes in `/var/backups/linux-backuper/` (accessible only by root).
|
|
- **Concurrency Locking:** Employs non-blocking `flock(2)` on `/var/run/linux-backuper.lock` to prevent overlapping snapshot or restore operations.
|
|
- **Path Traversal Shield:** All extraction and restoration paths are normalized and verified against base target directories to mitigate Zip Slip and symlink exploits.
|
|
- **SELinux Relabeling:** When restoring on SELinux-enforcing systems (RHEL, AlmaLinux, Fedora), automatically invokes `restorecon -R` on modified paths.
|
|
- **Mount & Device Protection:** Refuses to touch virtual filesystems (`/proc`, `/sys`, `/dev`, `/run`) or socket nodes.
|
|
|
|
---
|
|
|
|
## Documentation Triad
|
|
|
|
For complete documentation, refer to the guides in the `docs/` directory:
|
|
|
|
- 📖 **[User Guide](docs/user-guide.md)**: Daily operational guide for sysadmins, DevOps, and operators.
|
|
- ⚙️ **[Administrator Guide](docs/admin-guide.md)**: Systemd setup, GFS retention strategies, remote NAS sync, and JSON health monitoring.
|
|
- 💻 **[Developer Guide](docs/developer-guide.md)**: Architecture deep dive, zero-dependency engine standards, test suites, and packaging workflows.
|
|
- 📜 **UNIX Man Page**: View the Section 8 manual with `man linux-backuper`.
|
|
|
|
---
|
|
|
|
## License
|
|
|
|
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|