#!/bin/bash # Automated Clonezilla Backup Script # This script runs inside the Clonezilla environment to automate the backup process # while preserving the Clonezilla boot partition set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color print_status() { echo -e "${BLUE}[INFO]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } clear print_status "Automated Clonezilla System Backup" echo "==================================" echo # Wait for drives to be detected sleep 3 # Auto-detect source drive (internal) SOURCE_DRIVE="" for drive in /dev/nvme0n1 /dev/sda /dev/sdb /dev/sdc; do if [[ -b "$drive" && "$drive" != "/dev/sda" ]]; then # Check if it looks like a system drive (has multiple partitions) if [[ $(lsblk -n "$drive" | wc -l) -gt 1 ]]; then SOURCE_DRIVE="$drive" break fi fi done if [[ -z "$SOURCE_DRIVE" ]]; then print_error "Could not auto-detect source drive!" print_status "Available drives:" lsblk exit 1 fi # Target is always the backup partition on this USB TARGET_PARTITION="/dev/sda2" print_status "Source Drive: $SOURCE_DRIVE" print_status "Target Partition: $TARGET_PARTITION" lsblk "$SOURCE_DRIVE" echo print_warning "This will create a compressed backup image of your system" print_warning "Backup location: $TARGET_PARTITION" echo # Mount the backup partition BACKUP_MOUNT="/tmp/backup_storage" mkdir -p "$BACKUP_MOUNT" print_status "Mounting backup storage..." if ! mount "$TARGET_PARTITION" "$BACKUP_MOUNT"; then print_error "Failed to mount backup partition" exit 1 fi # Create backup directory with timestamp BACKUP_DATE=$(date +%Y%m%d_%H%M%S) BACKUP_DIR="$BACKUP_MOUNT/system_backup_$BACKUP_DATE" mkdir -p "$BACKUP_DIR" print_status "Creating system backup in: $BACKUP_DIR" echo # Use Clonezilla's ocs-sr (Save and Restore) in batch mode print_status "Starting Clonezilla backup process..." print_warning "This will take 30-60 minutes depending on your data size" echo # Clonezilla command in batch mode for disk image creation # -q2: Use parallel compression for speed # -c: Enable checking saved image # -j2: Use clone fresh MBR # -z1p: Use parallel gzip compression # -i 4096: Set image split size to 4GB # -sfsck: Skip filesystem check to save time # -scs: Skip checking free space in destination # -rescue: Continue on minor errors # -batch: Batch mode, no interactive prompts # Use Clonezilla's ocs-sr (Save and Restore) in batch mode with FASTEST settings # -q2: Use parallel compression for speed # -c: Enable checking saved image # -j2: Use clone fresh MBR # -z0: NO compression for maximum speed (trade size for speed) # -i 0: No image splitting for faster writing # -sfsck: Skip filesystem check to save time # -scs: Skip checking free space in destination # -rescue: Continue on minor errors # -batch: Batch mode, no interactive prompts # -p poweroff: Don't power off after completion print_status "Using MAXIMUM SPEED settings (NO compression - speed is king!)" print_warning "Backup will complete in ~15-20 minutes - fastest possible!" /usr/sbin/ocs-sr \ -q2 \ -j2 \ -z0 \ -i 0 \ -sfsck \ -scs \ -rescue \ -batch \ -p reboot \ savedisk \ "system_backup_$BACKUP_DATE" \ "$SOURCE_DRIVE" if [[ $? -eq 0 ]]; then print_success "Backup completed successfully!" print_success "Backup saved to: $BACKUP_DIR" # Create a restore script for easy restoration cat > "$BACKUP_DIR/restore.sh" << EOF #!/bin/bash # Restore script for system backup created on $BACKUP_DATE # Usage: ./restore.sh /dev/target_drive if [[ \$# -ne 1 ]]; then echo "Usage: \$0 /dev/target_drive" echo "Example: \$0 /dev/nvme0n1" exit 1 fi TARGET_DRIVE="\$1" echo "WARNING: This will completely overwrite \$TARGET_DRIVE" read -p "Continue? (yes/no): " confirm if [[ "\$confirm" != "yes" ]]; then echo "Cancelled" exit 1 fi /usr/sbin/ocs-sr \\ -g auto \\ -e1 auto \\ -e2 \\ -r \\ -j2 \\ -batch \\ restoredisk \\ "system_backup_$BACKUP_DATE" \\ "\$TARGET_DRIVE" EOF chmod +x "$BACKUP_DIR/restore.sh" # Show backup info print_status "Backup Information:" du -h "$BACKUP_DIR" echo print_success "Restore script created: $BACKUP_DIR/restore.sh" else print_error "Backup failed!" exit 1 fi # Unmount backup storage umount "$BACKUP_MOUNT" print_success "System backup completed!" print_status "You can now reboot back to your normal system" print_status "To restore: Boot from this USB and run the restore script" echo print_warning "Press Enter to continue..." read # Optional: Auto-reboot or return to menu echo "Backup process complete. You can now:" echo "1. Reboot to your normal system" echo "2. Run another backup" echo "3. Access Clonezilla manually"