Fix USB backup permission issues

**Problem:**
- USB drives mounted as root were not writable by regular user
- Caused "Permission denied" errors on backup
- Required running application as root (not secure)

**Solution:**
- Better permission error messages with fix suggestions
- Try to create backup directory first (more flexible)
- Show helpful error: "run sudo chown -R \$USER /media/usb0"

**USB Setup Script (setup_usb.sh):**
- Interactive USB drive mounting
- Automatically detects USB devices
- Mounts with user ownership (uid/gid)
- Tests write permissions
- Shows free space
- Offers to add to /etc/fstab
- Color-coded output

**Documentation Updates:**
- Added 3 methods for mounting with permissions
- Recommended method: mount with uid/gid options
- Added fstab auto-mount example
- Added quick setup script example
- Clear instructions for each method

**Usage:**
```bash
# Easiest method
sudo ./setup_usb.sh

# Or manual mounting
sudo mount -o uid=$(id -u),gid=$(id -g) /dev/sda1 /media/usb0

# Or fix existing mount
sudo chown -R $USER /media/usb0
```

**Security:**
- No need to run wedding phone as root
- User-owned USB mount points
- Proper permission checking
- Clear error messages

**Web Interface:**
- Shows helpful permission error messages
- Includes fix command in error text
- Better UX for permission issues

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-24 17:04:21 +07:00
parent d0bbaf6d4e
commit 42041006b7
3 changed files with 215 additions and 9 deletions

View File

@@ -186,18 +186,30 @@ def get_usb_backup_status():
if drive_info["mounted"]:
try:
# Check if writable
test_file = os.path.join(usb_path, ".wedding_phone_test")
# Try to create backup directory first
backup_dir = os.path.join(usb_path, "wedding-phone-backup")
try:
os.makedirs(backup_dir, exist_ok=True)
test_location = backup_dir
except PermissionError:
# Can't create backup dir, try root
test_location = usb_path
# Check if writable in backup directory
test_file = os.path.join(test_location, ".wedding_phone_test")
with open(test_file, 'w') as f:
f.write("test")
os.remove(test_file)
drive_info["writable"] = True
drive_info["backup_dir"] = backup_dir if test_location == backup_dir else None
# Get free space
stat = os.statvfs(usb_path)
drive_info["free_space"] = stat.f_bavail * stat.f_frsize
drive_info["free_space_mb"] = drive_info["free_space"] / (1024 * 1024)
except PermissionError as e:
drive_info["error"] = f"Permission denied. Mount with proper permissions or run: sudo chown -R $USER {usb_path}"
except Exception as e:
drive_info["error"] = str(e)