- 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
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
-
Clone or download this repository:
git clone <repository-url> cd backup_to_external_m.2 -
Make scripts executable:
chmod +x backup_script.sh chmod +x backup_manager.py -
Install dependencies (if needed):
sudo apt update sudo apt install python3-tk pv parted -
Optional: Set up portable tools on external M.2 SSD:
./setup_portable_tools.shThis 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
- Connect External Drive: Plug in your M.2 SSD with backup
- Launch GUI:
python3 backup_manager.py - Check Drive Selection:
- Source should be external drive (your backup)
- Target should be internal drive (will be overwritten)
- Use Swap Button: If needed, click "Swap Source↔Target"
- 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:
-
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 -
Restore Internal Drive:
- Launch backup tools from external drive
- External drive (source) → Internal drive (target)
- Click "Reboot & Restore" for safest operation
-
Create Desktop Entry:
cd /mnt/tools/backup_system ./create_desktop_entry.sh
Disaster Recovery Workflow
- Normal Operation: Internal drive fails
- Boot from External: Use M.2 SSD as boot drive
- Access Tools: Run
./access_tools.sh - Restore System: Use backup tools to restore to new internal drive
- 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
- Drive Detection: Automatically scans for available drives
- Auto-Selection: Internal drive as source, external as target
- Operation Selection: Choose backup or restore mode
- Validation: Confirms drives exist and are different
- Execution: Uses
ddcommand to clone entire drive - 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
- Script Creation: Creates a backup script for post-reboot execution
- Reboot Scheduling: Schedules system reboot
- Auto-Execution: Backup starts automatically after reboot
- Notification: Shows completion status
Command Line Mode
- Direct
ddcloning with progress monitoring - Comprehensive logging to
/var/log/system_backup.log - Drive size validation and safety checks
Technical Details
Backup Method
- Uses
ddcommand for bit-perfect drive cloning - Block size optimized at 4MB for performance
fdatasyncensures all data is written to disk
Drive Detection
- Uses
lsblkto 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
-
Permission Denied
# Run with sudo for drive operations sudo python3 backup_manager.py -
Drive Not Detected
# Check if drive is connected and recognized lsblk dmesg | tail -
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
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- 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.