Server Cloning with Clonezilla

Clonezilla is a powerful, free disk imaging and cloning tool that enables rapid server deployment, backups, and disaster recovery. This comprehensive guide covers creating bootable media, performing disk/partition imaging, network cloning, multicast distribution, and PXE-based deployment strategies.

Table of Contents

  1. Clonezilla Overview
  2. Creating Bootable Clonezilla Media
  3. Disk and Partition Imaging
  4. Network Cloning
  5. Multicast Cloning
  6. Restoring from Images
  7. PXE Boot Deployment
  8. Automation and Scripting
  9. Conclusion

Clonezilla Overview

Clonezilla provides:

  • Disk cloning: Complete server replication including partition tables and bootloaders
  • Image backup: Create compressed images for archive and recovery
  • Network mode: Clone over LAN using SSH, SAMBA, or NFS
  • Multicast: Efficiently clone one image to multiple servers simultaneously
  • PXE boot: Integrate with existing infrastructure for automated deployment

Creating Bootable Clonezilla Media

Download Clonezilla

# Download latest Clonezilla ISO
cd /tmp
wget https://sourceforge.net/projects/clonezilla/files/stable/clonezilla-live-*.iso

# Verify file integrity
sha256sum clonezilla-live-*.iso
# Compare with checksum from project page

# List available images
ls -lh clonezilla-live-*.iso

Create Bootable USB Drive

# Identify USB device (careful - this will overwrite data!)
lsblk

# Method 1: Using dd (for Linux systems)
# WARNING: Replace sdX with your USB device identifier
USB_DEVICE="/dev/sdb"
ISO_FILE="clonezilla-live-3.1.0-13-amd64.iso"

# Unmount the USB if already mounted
sudo umount "${USB_DEVICE}"1

# Write ISO to USB
sudo dd if="$ISO_FILE" of="$USB_DEVICE" bs=4M status=progress
sync

echo "USB drive created at $USB_DEVICE"

# Method 2: Using Etcher (more user-friendly)
# On macOS:
# brew install balena-etcher
# etcher select clonezilla-live-3.1.0-13-amd64.iso
# etcher select /dev/diskX
# etcher flash

# Method 3: Using GNOME Disks GUI
# sudo apt install gnome-disk-utility
# gnome-disks

Create Bootable CD/DVD

# For optical media
ISO_FILE="clonezilla-live-3.1.0-13-amd64.iso"

# Using growisofs (for DVD)
growisofs -dvd-compat -Z /dev/dvd="$ISO_FILE"

# Using xorriso (more modern)
xorriso -as cdrecord -v dev=/dev/cdrom -eject "$ISO_FILE"

# Using dd
dd if="$ISO_FILE" of=/dev/sr0 bs=4M status=progress

Disk and Partition Imaging

Full Disk Imaging

# Boot into Clonezilla and start the interface
# Select: Start_Clonezilla -> device-image -> Beginner mode

# Command line equivalent for full disk backup:
create_full_disk_image() {
    local source_device="/dev/sda"
    local image_path="/mnt/clonezilla-storage/server-backup"
    local image_name="prod-server-full-$(date +%Y%m%d)"
    
    mkdir -p "$image_path"
    
    echo "Creating full disk image: $image_name"
    
    # Use clonezilla's command line tools
    ocs-sr \
        -q2 \
        -j2 \
        -z1p \
        -i 2000 \
        -sfsck \
        -c \
        -r \
        -e1 auto \
        -e2 \
        -p true \
        savedisk "$image_name" "$source_device"
    
    if [ $? -eq 0 ]; then
        echo "Full disk image created successfully"
        ls -lh "$image_path/$image_name"
    else
        echo "Error: Disk imaging failed"
        return 1
    fi
}

# create_full_disk_image

Partition-Level Imaging

# Create image of specific partition only
create_partition_image() {
    local source_partition="/dev/sda1"
    local image_path="/mnt/clonezilla-storage"
    local image_name="partition-root-$(date +%Y%m%d)"
    
    mkdir -p "$image_path"
    
    echo "Creating partition image: $image_name"
    
    # For single partition
    ocs-sr \
        -q2 \
        -j2 \
        -z1p \
        -i 2000 \
        -sfsck \
        -c \
        -r \
        -e1 auto \
        -e2 \
        -p true \
        saveparts "$image_name" "$source_partition"
    
    if [ $? -eq 0 ]; then
        echo "Partition image created: $image_path/$image_name"
    fi
}

# Image compression options
# -z0: No compression (fastest, largest)
# -z1: gzip (default)
# -z9: Maximum compression (slowest, smallest)

# Speed up imaging with parallel processing
create_fast_disk_image() {
    local source_device="/dev/sda"
    local image_path="/mnt/clonezilla-storage"
    local image_name="fast-backup-$(date +%Y%m%d)"
    
    # Use parallel processing for faster cloning
    ocs-sr \
        -q2 \
        -j4 \
        -z0 \
        -i 0 \
        -c \
        -r \
        savedisk "$image_name" "$source_device"
}

Image Verification

# Verify image integrity after creation
verify_clonezilla_image() {
    local image_path=$1
    
    echo "Verifying image: $image_path"
    
    # Check image structure
    if [ -d "$image_path" ]; then
        echo "Image contents:"
        ls -la "$image_path/"
        
        # Look for indicator files (clonezilla creates these)
        if [ -f "$image_path/disk" ]; then
            echo "✓ Disk image detected (full disk)"
        elif [ -f "$image_path/parts" ]; then
            echo "✓ Partition image detected"
        fi
        
        # Check for disk info file
        if [ -f "$image_path/info-disk" ]; then
            echo "Disk information:"
            cat "$image_path/info-disk"
        fi
    else
        echo "Error: Image path not found"
        return 1
    fi
}

Network Cloning

SSH-Based Network Cloning

# Configure network storage for Clonezilla
configure_network_storage() {
    # On backup server: Create storage location
    mkdir -p /mnt/clonezilla-images
    chmod 755 /mnt/clonezilla-images
    
    # Generate SSH keys for automated access
    ssh-keygen -t rsa -N "" -f /root/.ssh/clonezilla_key
    
    # Copy public key to client
    # ssh-copy-id -i /root/.ssh/clonezilla_key.pub backup@client-server
}

# Clone to remote SSH server
clone_to_ssh_storage() {
    local remote_host="backup-server.example.com"
    local remote_user="backup"
    local remote_path="/mnt/clonezilla-images"
    local source_device="/dev/sda"
    local image_name="server-clone-$(date +%Y%m%d)"
    
    echo "Cloning to remote SSH storage"
    
    ocs-sr \
        -q2 \
        -j2 \
        -z1p \
        -i 2000 \
        -c \
        -r \
        -e1 auto \
        -e2 \
        -p true \
        -sfsck \
        savedisk "$image_name" "$source_device"
    
    # Transfer image via SSH
    echo "Transferring image to $remote_host"
    
    rsync -avz --progress \
        "/home/partimag/$image_name" \
        "$remote_user@$remote_host:$remote_path/"
}

# Clone from remote NFS
clone_from_nfs() {
    local nfs_server="nfs.example.com"
    local nfs_path="/export/clonezilla-images"
    local mount_point="/mnt/clonezilla-nfs"
    local image_name="server-backup-20240101"
    local target_device="/dev/sda"
    
    # Mount NFS storage
    mkdir -p "$mount_point"
    mount -t nfs "$nfs_server:$nfs_path" "$mount_point"
    
    # Restore from NFS image
    ocs-sr \
        -q2 \
        -j2 \
        -c \
        -r \
        -e1 auto \
        -e2 \
        -p reboot \
        restoredisk "$image_name" "$target_device"
}

SAMBA-Based Cloning

# Mount SAMBA share for image storage
setup_samba_cloning() {
    local samba_host="nas.example.com"
    local samba_share="clonezilla"
    local samba_user="backup_user"
    local mount_point="/mnt/clonezilla-samba"
    
    mkdir -p "$mount_point"
    
    # Mount SAMBA share
    mount -t cifs \
        "//$samba_host/$samba_share" \
        "$mount_point" \
        -o username="$samba_user",password="secure_password",uid=0,gid=0,file_mode=0755
    
    # Verify mount
    df -h "$mount_point"
}

# Store image on SAMBA via Clonezilla
clone_to_samba() {
    local samba_mount="/mnt/clonezilla-samba"
    local source_device="/dev/sda"
    local image_name="workstation-backup-$(date +%Y%m%d)"
    
    echo "Cloning to SAMBA storage at $samba_mount"
    
    ocs-sr \
        -q2 \
        -j2 \
        -z1p \
        -c \
        -r \
        savedisk "$image_name" "$source_device"
}

Multicast Cloning

Deploy a single image to multiple servers simultaneously using multicast streaming.

# Setup Clonezilla multicast server
setup_multicast_server() {
    local multicast_ip="224.0.0.1"
    local multicast_port="10001"
    local image_path="/mnt/images/prod-server-20240101"
    local source_device="/dev/sda"
    
    echo "Setting up Clonezilla multicast server"
    
    # Install required packages
    apt-get install -y drbl clonezilla
    
    # Configure multicast parameters
    cat > /etc/ocs/clonezilla-default << 'EOF'
# Multicast settings
DRBL_BRANCH="stable"
CLONEZILLA_MODE="full"
MULTICAST_PORT="10001"
MULTICAST_IP="224.0.0.1"
EOF
}

# Start multicast clone session
start_multicast_clone() {
    local image_name="production-server-20240101"
    local dest_devices="/dev/sda"
    
    echo "Starting multicast clone session"
    echo "Image: $image_name"
    echo "Devices: $dest_devices"
    echo "Waiting for clients to boot..."
    
    # Clients boot into Clonezilla via PXE
    # Select: device-image -> remote mode -> Multicast -> Join existing session
    
    # Monitor multicast session
    ocs-ondemand -m join multicast
}

Restoring from Images

Full Disk Restoration

# Restore entire disk from image
restore_full_disk() {
    local image_path="/mnt/clonezilla-images/prod-server-20240101"
    local target_device="/dev/sda"
    
    echo "WARNING: This will overwrite entire disk $target_device"
    read -p "Continue? (yes/no) " -r
    
    if [[ $REPLY != "yes" ]]; then
        echo "Cancelled"
        return 1
    fi
    
    echo "Restoring disk image..."
    
    ocs-sr \
        -q2 \
        -j2 \
        -c \
        -r \
        -e1 auto \
        -e2 \
        -p reboot \
        restoredisk "$image_path" "$target_device"
    
    if [ $? -eq 0 ]; then
        echo "Disk restoration completed - server rebooting"
    fi
}

# Restore with partition size adjustment
restore_with_resize() {
    local image_name="prod-server-backup"
    local target_device="/dev/sda"
    
    # "-e1 auto" automatically resizes partitions to fit target disk
    ocs-sr \
        -q2 \
        -c \
        -r \
        -e1 auto \
        -e2 \
        -p reboot \
        restoredisk "$image_name" "$target_device"
}

Partition-Level Restoration

# Restore specific partition
restore_partition() {
    local image_name="partition-root-backup"
    local target_partition="/dev/sda1"
    
    echo "Restoring partition $target_partition from image $image_name"
    
    ocs-sr \
        -q2 \
        -c \
        -r \
        -p true \
        restoreparts "$image_name" "$target_partition"
}

# Selective file recovery (mount and browse)
mount_image_for_recovery() {
    local image_path="/mnt/clonezilla-images/server-backup"
    local recovery_mount="/mnt/image-recovery"
    
    mkdir -p "$recovery_mount"
    
    # Get disk image info
    if [ -f "$image_path/disk" ]; then
        # Extract and mount filesystem
        # This process depends on image format and filesystem
        
        echo "Mounting image for file-level recovery"
        # Detailed mounting depends on specific image structure
    fi
}

PXE Boot Deployment

DRBL Server Setup

# Setup DRBL (Distributed Replicated Block Level) server
setup_drbl_server() {
    # Install DRBL packages
    apt-get install -y drbl clonezilla
    
    # Configure DRBL
    drblsrv-offline
    
    # Setup PXE boot
    drblpxeconfig -i
    
    # Configure DHCP
    cat > /etc/dhcp/dhcpd.conf << 'EOF'
subnet 10.0.0.0 netmask 255.255.255.0 {
    range 10.0.0.100 10.0.0.200;
    option routers 10.0.0.1;
    option domain-name-servers 10.0.0.1;
    
    # PXE boot configuration
    next-server 10.0.0.10;  # DRBL server IP
    filename "pxelinux.0";
}
EOF
    
    systemctl restart dhcp
}

# Configure image for PXE deployment
configure_pxe_image() {
    local image_name="prod-server-20240101"
    local drbl_server="/usr/lib/drbl/samples"
    
    # Place image in DRBL image directory
    cp -r "/mnt/clonezilla-images/$image_name" \
        "/opt/drbl/images/"
    
    # Configure DRBL to serve this image
    cat > /opt/drbl/images/$image_name/clonezilla.conf << 'EOF'
# Clonezilla deployment configuration
CLONEZILLA_MODE="full"
TARGET_IMAGE="prod-server-20240101"
RESTORE_MODE="auto"
SHUTDOWN_AFTER="no"
EOF
}

PXE Boot Client Configuration

# Configure clients for PXE boot
configure_pxe_client() {
    # In BIOS/UEFI: Set PXE/Network as primary boot device
    # Save and restart
    
    # Client boot sequence:
    # 1. Network interface requests DHCP
    # 2. DHCP server provides IP and PXE server address
    # 3. Client downloads pxelinux.0 bootloader
    # 4. Bootloader loads kernel and Clonezilla
    # 5. User selects restore image and target disk
    # 6. Image restores automatically
}

# Automated deployment via PXE
automate_pxe_deployment() {
    # Create unattended Clonezilla configuration
    cat > /opt/drbl/images/ocs/ocs-live.conf << 'EOF'
# Automated deployment settings
OCS_MODE="restore"
OCS_IMAGING_MODE="disk"
TARGET_IMAGE_NAME="prod-server-20240101"
TARGET_DISK="/dev/sda"
AUTO_SHUTDOWN="yes"
SKIP_CONFIRMATION="yes"
EOF
    
    # Clients configured to auto-restore from this image on boot
}

Automation and Scripting

Automated Daily Imaging

# Automated daily backup to network storage
cat > /usr/local/bin/daily-clonezilla-backup.sh << 'EOF'
#!/bin/bash

SOURCE_DEVICE="/dev/sda"
IMAGE_REPOSITORY="/mnt/clonezilla-images"
IMAGE_NAME="server-daily-$(date +%Y%m%d)"
RETENTION_DAYS=30
LOG_FILE="/var/log/clonezilla-backup.log"

echo "[$(date)] Starting Clonezilla backup" >> "$LOG_FILE"

# Ensure repository exists
mkdir -p "$IMAGE_REPOSITORY"

# Create backup
ocs-sr \
    -q2 \
    -j4 \
    -z1p \
    -i 0 \
    -c \
    -r \
    -e1 auto \
    -e2 \
    -p true \
    savedisk "$IMAGE_NAME" "$SOURCE_DEVICE" \
    >> "$LOG_FILE" 2>&1

if [ $? -eq 0 ]; then
    echo "[$(date)] Backup completed: $IMAGE_NAME" >> "$LOG_FILE"
    
    # Rotate old images (keep 30 days)
    find "$IMAGE_REPOSITORY" -maxdepth 1 -type d -mtime +$RETENTION_DAYS -exec rm -rf {} \;
    
    # Sync to offsite location
    rsync -avz --delete \
        "$IMAGE_REPOSITORY/" \
        [email protected]:/backups/clonezilla/ \
        >> "$LOG_FILE" 2>&1
else
    echo "[$(date)] Backup FAILED" >> "$LOG_FILE"
fi
EOF

chmod +x /usr/local/bin/daily-clonezilla-backup.sh

# Schedule daily backup
(crontab -l 2>/dev/null; echo "0 2 * * * /usr/local/bin/daily-clonezilla-backup.sh") | crontab -

Clonezilla Batch Operations

# Clone multiple servers to images
batch_image_servers() {
    local servers=("web01" "web02" "db01" "db02")
    local image_repository="/mnt/clonezilla-images"
    
    for server in "${servers[@]}"; do
        echo "Imaging $server..."
        
        # SSH to server and create image
        ssh "root@$server" << 'CMD'
mkdir -p /mnt/clonezilla-work
ocs-sr \
    -q2 \
    -j2 \
    -z1p \
    -c \
    -r \
    savedisk "$(hostname)-$(date +%Y%m%d)" "/dev/sda"
CMD
        
        # Transfer image back
        rsync -avz "root@$server:/home/partimag/" \
            "$image_repository/"
    done
}

# Restore images to multiple targets
batch_restore() {
    local image_name="prod-server-template"
    local target_hosts=("server03" "server04" "server05")
    
    for target in "${target_hosts[@]}"; do
        echo "Restoring $image_name to $target"
        
        # Boot target into Clonezilla
        # Execute restoration
        # Poll for completion
    done
}

Conclusion

Clonezilla provides enterprise-grade disk cloning and imaging capabilities:

  1. Bootable Media: Create USB or CD for immediate cloning needs
  2. Network Cloning: Integrate with SSH, NFS, or SAMBA for remote storage
  3. Multicast: Deploy single image to many servers simultaneously
  4. PXE Integration: Automate deployment via network boot
  5. Automation: Script daily imaging and disaster recovery procedures

Use Clonezilla for rapid server provisioning, disaster recovery, and infrastructure migrations. Combine with regular testing to ensure images restore correctly when needed.