Files
backup_to_external_m.2/README.md
root 0367c3f7e6 Initial commit: Complete backup system with portable tools
- GUI and CLI backup/restore functionality
- Auto-detection of internal system drive
- Smart drive classification (internal vs external)
- Reboot integration for clean backups/restores
- Portable tools that survive cloning operations
- Tool preservation system for external M.2 SSD
- Complete disaster recovery workflow
- Safety features and multiple confirmations
- Desktop integration and launcher scripts
- Comprehensive documentation
2025-09-13 22:14:36 +02:00

9.6 KiB

System Backup to External M.2 SSD

A comprehensive backup solution for Linux systems that provides both GUI and command-line interfaces for cloning your internal drive to an external M.2 SSD.

Features

  • GUI Application: Easy-to-use graphical interface with drive detection
  • Auto-Detection: Automatically identifies your internal system drive as source
  • Smart Drive Classification: Distinguishes between internal and external drives
  • Command Line Script: For automated backups and scripting
  • Reboot Integration: Option to reboot and perform backup automatically
  • Drive Validation: Ensures safe operation with proper drive detection
  • Progress Monitoring: Real-time backup progress and logging
  • Desktop Integration: Creates desktop shortcuts for easy access
  • Portable Tools: Backup tools survive cloning and work when booted from external drive
  • Tool Preservation: Automatic restoration of backup tools after each clone operation

Requirements

  • Linux system (tested on Ubuntu/Debian)
  • Python 3.6+ with tkinter
  • External M.2 SSD in USB enclosure
  • sudo privileges for drive operations

Installation

  1. Clone or download this repository:

    git clone <repository-url>
    cd backup_to_external_m.2
    
  2. Make scripts executable:

    chmod +x backup_script.sh
    chmod +x backup_manager.py
    
  3. Install dependencies (if needed):

    sudo apt update
    sudo apt install python3-tk pv parted
    
  4. Optional: Set up portable tools on external M.2 SSD:

    ./setup_portable_tools.sh
    

    This creates a separate partition on your external drive that preserves backup tools even after cloning.

Usage

GUI Application

Launch the graphical backup manager:

python3 backup_manager.py

Features:

  • Automatic drive detection and classification
  • Source and target drive selection with smart defaults
  • Real-time progress monitoring
  • Backup Modes:
    • Start Backup: Immediate backup (while system running)
    • Reboot & Backup: Reboot system then backup (recommended)
  • Restore Modes:
    • Restore from External: Immediate restore from external to internal
    • Reboot & Restore: Reboot system then restore (recommended)
  • Drive Swap Button: Easily swap source and target for restore operations

Command Line Script

For command-line usage:

# List available drives
./backup_script.sh --list

# Perform backup with specific drives
sudo ./backup_script.sh --source /dev/nvme0n1 --target /dev/sda

# Restore from external to internal (note the restore flag)
sudo ./backup_script.sh --restore --source /dev/sda --target /dev/nvme0n1

# Or more simply in restore mode (source/target are swapped automatically)
sudo ./backup_script.sh --restore --source /dev/nvme0n1 --target /dev/sda

# Create desktop entry
./backup_script.sh --desktop

# Launch GUI from script
./backup_script.sh --gui

Desktop Integration

Create a desktop shortcut:

./backup_script.sh --desktop

This creates a clickable icon on your desktop that launches the backup tool.

Restore Operations

When to Restore

  • System Failure: Internal drive crashed or corrupted
  • System Migration: Moving to new hardware
  • Rollback: Reverting to previous system state
  • Testing: Restoring test environment

How to Restore

GUI Method

  1. Connect External Drive: Plug in your M.2 SSD with backup
  2. Launch GUI: python3 backup_manager.py
  3. Check Drive Selection:
    • Source should be external drive (your backup)
    • Target should be internal drive (will be overwritten)
  4. Use Swap Button: If needed, click "Swap Source↔Target"
  5. Choose Restore Mode:
    • "Restore from External": Immediate restore
    • "Reboot & Restore": Reboot then restore (recommended)

Command Line Method

# Restore with automatic drive detection
sudo ./backup_script.sh --restore

# Restore with specific drives
sudo ./backup_script.sh --restore --source /dev/sda --target /dev/nvme0n1

⚠️ Restore Safety Warnings

  • Data Loss: Target drive is completely overwritten
  • Irreversible: No undo after restore starts
  • Multiple Confirmations: System requires explicit confirmation
  • Drive Verification: Double-check source and target drives
  • Boot Issues: Ensure external drive contains valid system backup

Portable Tools (Boot from External M.2)

Setup Portable Tools

Run this once to set up backup tools on your external M.2 SSD:

./setup_portable_tools.sh

This will:

  • Create a 512MB tools partition on your external drive
  • Install backup tools that survive cloning operations
  • Set up automatic tool restoration after each backup

Using Portable Tools

When Booted from External M.2 SSD:

  1. Access Tools:

    # Easy access helper
    ./access_tools.sh
    
    # Or manually:
    sudo mount LABEL=BACKUP_TOOLS /mnt/tools
    cd /mnt/tools/backup_system
    ./launch_backup_tools.sh
    
  2. Restore Internal Drive:

    • Launch backup tools from external drive
    • External drive (source) → Internal drive (target)
    • Click "Reboot & Restore" for safest operation
  3. Create Desktop Entry:

    cd /mnt/tools/backup_system
    ./create_desktop_entry.sh
    

Disaster Recovery Workflow

  1. Normal Operation: Internal drive fails
  2. Boot from External: Use M.2 SSD as boot drive
  3. Access Tools: Run ./access_tools.sh
  4. Restore System: Use backup tools to restore to new internal drive
  5. Back to Normal: Boot from restored internal drive

Safety Features

  • Drive Validation: Prevents accidental overwriting of wrong drives
  • Size Checking: Ensures target drive is large enough
  • Confirmation Prompts: Multiple confirmation steps before destructive operations
  • Mount Detection: Automatically unmounts target drives before backup
  • Progress Monitoring: Real-time feedback during backup operations

File Structure

backup_to_external_m.2/
├── backup_manager.py      # GUI application
├── backup_script.sh       # Command-line script
├── install.sh            # Installation script
├── systemd/              # Systemd service files
│   └── backup-service.service
└── README.md             # This file

How It Works

GUI Mode

  1. Drive Detection: Automatically scans for available drives
  2. Auto-Selection: Internal drive as source, external as target
  3. Operation Selection: Choose backup or restore mode
  4. Validation: Confirms drives exist and are different
  5. Execution: Uses dd command to clone entire drive
  6. Progress: Shows real-time progress and logging

Backup vs Restore

  • Backup: Internal → External (preserves your system on external drive)
  • Restore: External → Internal (overwrites internal with backup data)
  • Swap Button: Easily switch source/target for restore operations

Reboot vs Immediate Operations

  • Immediate: Faster start, but system is running (potential file locks)
  • Reboot Mode: System restarts first, then operates (cleaner, more reliable)
  • Recommendation: Use reboot mode for critical operations

Reboot & Backup Mode

  1. Script Creation: Creates a backup script for post-reboot execution
  2. Reboot Scheduling: Schedules system reboot
  3. Auto-Execution: Backup starts automatically after reboot
  4. Notification: Shows completion status

Command Line Mode

  • Direct dd cloning with progress monitoring
  • Comprehensive logging to /var/log/system_backup.log
  • Drive size validation and safety checks

Technical Details

Backup Method

  • Uses dd command for bit-perfect drive cloning
  • Block size optimized at 4MB for performance
  • fdatasync ensures all data is written to disk

Drive Detection

  • Uses lsblk to enumerate block devices
  • Filters for actual disk drives
  • Shows drive sizes for easy identification

Safety Mechanisms

  • Root privilege verification
  • Block device validation
  • Source/target drive comparison
  • Mount status checking
  • Size compatibility verification

Troubleshooting

Common Issues

  1. Permission Denied

    # Run with sudo for drive operations
    sudo python3 backup_manager.py
    
  2. Drive Not Detected

    # Check if drive is connected and recognized
    lsblk
    dmesg | tail
    
  3. Backup Fails

    # Check system logs
    sudo journalctl -f
    tail -f /var/log/system_backup.log
    

Performance Tips

  • Use USB 3.0+ connection for external M.2 SSD
  • Ensure sufficient power supply for external enclosure
  • Close unnecessary applications during backup
  • Use SSD for better performance than traditional HDD

Security Considerations

  • Data Destruction: Target drive data is completely overwritten
  • Root Access: Scripts require elevated privileges
  • Verification: Always verify backup integrity after completion
  • Testing: Test restore process with non-critical data first

Limitations

  • Only works with block devices (entire drives)
  • Cannot backup to smaller drives
  • Requires manual drive selection for safety
  • No incremental backup support (full clone only)

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly
  5. Submit a pull request

License

This project is open source. Use at your own risk.

Disclaimer

WARNING: This tool performs low-level disk operations that can result in data loss. Always:

  • Verify drive selections carefully
  • Test with non-critical data first
  • Maintain separate backups of important data
  • Understand the risks involved

The authors are not responsible for any data loss or system damage.