cleanup: Archive old complex scripts and documentation
- Move all old complex backup scripts to old_scripts/ - Archive previous documentation versions - Clean up temporary files and debian packages - Update README to focus on new simple system - Keep only the enhanced simple backup system in main directory Main directory now contains only: - simple_backup_gui.py (GUI interface) - enhanced_simple_backup.sh (CLI interface) - list_drives.sh (helper) - simple_backup.sh (basic CLI) - SIMPLE_BACKUP_README.md (detailed docs) - README.md (project overview)
This commit is contained in:
245
old_scripts/improved_lvm_migration.sh
Executable file → Normal file
245
old_scripts/improved_lvm_migration.sh
Executable file → Normal file
@@ -16,7 +16,7 @@ NC='\033[0m' # No Color
|
||||
# Configuration variables
|
||||
INTERNAL_DRIVE=""
|
||||
EXTERNAL_DRIVE=""
|
||||
VG_NAME="system-vg"
|
||||
VG_NAME="migration-vg" # Changed to avoid conflict with existing system-vg
|
||||
ROOT_LV="root"
|
||||
HOME_LV="home"
|
||||
SWAP_LV="swap"
|
||||
@@ -133,10 +133,10 @@ detect_drives() {
|
||||
error "Internal and external drives cannot be the same!"
|
||||
fi
|
||||
|
||||
local external_size_bytes=$(lsblk -bno SIZE "$EXTERNAL_DRIVE")
|
||||
local internal_size_bytes=$(lsblk -bno SIZE "$INTERNAL_DRIVE")
|
||||
local external_size_bytes=$(lsblk -bno SIZE "$EXTERNAL_DRIVE" | head -1 | tr -d ' ')
|
||||
local internal_size_bytes=$(lsblk -bno SIZE "$INTERNAL_DRIVE" | head -1 | tr -d ' ')
|
||||
|
||||
if [ "$external_size_bytes" -lt "$internal_size_bytes" ]; then
|
||||
if [ -n "$external_size_bytes" ] && [ -n "$internal_size_bytes" ] && [ "$external_size_bytes" -lt "$internal_size_bytes" ]; then
|
||||
error "External drive is smaller than internal drive"
|
||||
fi
|
||||
|
||||
@@ -163,14 +163,20 @@ detect_drives() {
|
||||
analyze_source_system() {
|
||||
log "Analyzing source system..."
|
||||
|
||||
local partitions=($(lsblk -pno NAME "$INTERNAL_DRIVE" | grep -v "^$INTERNAL_DRIVE$"))
|
||||
# Get partitions using a more reliable method
|
||||
local partitions=($(lsblk -lpno NAME "$INTERNAL_DRIVE" | tail -n +2))
|
||||
|
||||
echo "Source drive partitions:"
|
||||
for part in "${partitions[@]}"; do
|
||||
local size=$(lsblk -no SIZE "$part")
|
||||
local fstype=$(lsblk -no FSTYPE "$part")
|
||||
local label=$(lsblk -no LABEL "$part")
|
||||
local mountpoint=$(lsblk -no MOUNTPOINT "$part")
|
||||
# Check if partition actually exists before querying
|
||||
if [ ! -b "$part" ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
local size=$(lsblk -no SIZE "$part" 2>/dev/null || echo "unknown")
|
||||
local fstype=$(lsblk -no FSTYPE "$part" 2>/dev/null || echo "")
|
||||
local label=$(lsblk -no LABEL "$part" 2>/dev/null || echo "")
|
||||
local mountpoint=$(lsblk -no MOUNTPOINT "$part" 2>/dev/null || echo "")
|
||||
|
||||
echo " $part: $size, $fstype, ${label:-'no label'}"
|
||||
|
||||
@@ -193,6 +199,85 @@ analyze_source_system() {
|
||||
success "Source system analysis completed"
|
||||
}
|
||||
|
||||
calculate_partition_sizes() {
|
||||
log "Calculating required partition sizes..."
|
||||
|
||||
# Get actual source partition sizes
|
||||
local source_root_gb=0
|
||||
local source_home_gb=0
|
||||
|
||||
# Check actual partition sizes from the source
|
||||
for part_name in "${!INTERNAL_PARTITIONS[@]}"; do
|
||||
local part_device="${INTERNAL_PARTITIONS[$part_name]}"
|
||||
if [ -b "$part_device" ]; then
|
||||
local size_bytes=$(lsblk -bno SIZE "$part_device" 2>/dev/null | head -1 | tr -d ' ')
|
||||
if [ -n "$size_bytes" ]; then
|
||||
local size_gb=$((size_bytes / 1024 / 1024 / 1024))
|
||||
|
||||
case "$part_name" in
|
||||
"root")
|
||||
source_root_gb=$size_gb
|
||||
log "Source root: ${size_gb}GB"
|
||||
;;
|
||||
"home"|"encrypted_home")
|
||||
source_home_gb=$size_gb
|
||||
log "Source home: ${size_gb}GB"
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# Get target drive total space
|
||||
local total_space_bytes=$(lsblk -bno SIZE "$EXTERNAL_DRIVE" | head -1 | tr -d ' ')
|
||||
local total_space_gb=$((total_space_bytes / 1024 / 1024 / 1024))
|
||||
|
||||
log "Target drive total space: ${total_space_gb}GB"
|
||||
|
||||
# Fixed sizes for system partitions
|
||||
local swap_size="8G"
|
||||
local boot_size="2G"
|
||||
|
||||
# Calculate available space for data partitions (leave 2GB for overhead/EFI)
|
||||
local available_for_data=$((total_space_gb - 8 - 2 - 2)) # 464GB available
|
||||
|
||||
# For same-size drives, distribute space proportionally to source
|
||||
local total_source_data=$((source_root_gb + source_home_gb))
|
||||
|
||||
if [ "$total_source_data" -gt "$available_for_data" ]; then
|
||||
# Source is larger than target, scale down proportionally
|
||||
local scale_factor_percent=$((available_for_data * 100 / total_source_data))
|
||||
local root_size="$((source_root_gb * scale_factor_percent / 100))G"
|
||||
local home_size="$((source_home_gb * scale_factor_percent / 100))G"
|
||||
|
||||
warning "Scaling down partitions to fit target drive:"
|
||||
warning " Scale factor: ${scale_factor_percent}%"
|
||||
else
|
||||
# Target has enough space, use source sizes with small buffers
|
||||
local root_size="$((source_root_gb + 5))G" # 5GB buffer for root
|
||||
local remaining_space=$((available_for_data - source_root_gb - 5))
|
||||
local home_size="${remaining_space}G" # Use all remaining space for home
|
||||
fi
|
||||
|
||||
# Export calculated sizes
|
||||
CALCULATED_ROOT_SIZE="$root_size"
|
||||
CALCULATED_HOME_SIZE="$home_size"
|
||||
CALCULATED_SWAP_SIZE="$swap_size"
|
||||
CALCULATED_BOOT_SIZE="$boot_size"
|
||||
|
||||
log "Final calculated sizes:"
|
||||
log " Root: $CALCULATED_ROOT_SIZE"
|
||||
log " Home: $CALCULATED_HOME_SIZE"
|
||||
log " Swap: $CALCULATED_SWAP_SIZE"
|
||||
log " Boot: $CALCULATED_BOOT_SIZE"
|
||||
|
||||
# Verify total fits
|
||||
local total_allocated=$((${CALCULATED_ROOT_SIZE%G} + ${CALCULATED_HOME_SIZE%G} + ${CALCULATED_SWAP_SIZE%G} + ${CALCULATED_BOOT_SIZE%G}))
|
||||
log "Total allocated: ${total_allocated}GB of ${total_space_gb}GB"
|
||||
|
||||
success "Partition sizes calculated"
|
||||
}
|
||||
|
||||
check_prerequisites() {
|
||||
log "Checking prerequisites and installing required tools..."
|
||||
|
||||
@@ -220,56 +305,124 @@ check_prerequisites() {
|
||||
create_lvm_layout() {
|
||||
log "Creating LVM layout on external drive..."
|
||||
|
||||
# Calculate sizes based on source
|
||||
local root_size="70G"
|
||||
local home_size="100G"
|
||||
local swap_size="8G"
|
||||
local boot_size="2G"
|
||||
# Use the calculated sizes
|
||||
local root_size="$CALCULATED_ROOT_SIZE"
|
||||
local home_size="$CALCULATED_HOME_SIZE"
|
||||
local swap_size="$CALCULATED_SWAP_SIZE"
|
||||
local boot_size="$CALCULATED_BOOT_SIZE"
|
||||
|
||||
# Adjust based on available space
|
||||
local total_space_gb=$(lsblk -bno SIZE "$EXTERNAL_DRIVE" | awk '{print int($1/1024/1024/1024)}')
|
||||
if [ "$total_space_gb" -gt 500 ]; then
|
||||
home_size="200G"
|
||||
root_size="100G"
|
||||
log "Using calculated sizes: Root=$root_size, Home=$home_size, Swap=$swap_size, Boot=$boot_size"
|
||||
|
||||
# Properly unmount and deactivate any existing LVM on the target drive
|
||||
log "Cleaning up existing LVM on target drive..."
|
||||
|
||||
# Check if target drive partitions are currently mounted or in use
|
||||
local partitions_in_use=false
|
||||
for part in "${EXTERNAL_DRIVE}"*; do
|
||||
if [ -b "$part" ]; then
|
||||
if mount | grep -q "$part"; then
|
||||
log "Unmounting $part..."
|
||||
umount "$part" 2>/dev/null || {
|
||||
warning "Could not unmount $part - it may be in use"
|
||||
partitions_in_use=true
|
||||
}
|
||||
fi
|
||||
|
||||
# Check if this partition has a VG on it
|
||||
local vg_on_part=$(pvs --noheadings -o vg_name "$part" 2>/dev/null | tr -d ' ')
|
||||
if [ -n "$vg_on_part" ]; then
|
||||
log "Deactivating VG '$vg_on_part' on $part"
|
||||
vgchange -an "$vg_on_part" 2>/dev/null || true
|
||||
vgremove -f "$vg_on_part" 2>/dev/null || true
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
if [ "$partitions_in_use" = true ]; then
|
||||
warning "Some partitions are in use. Continuing anyway..."
|
||||
fi
|
||||
|
||||
log "LVM sizes: Root=$root_size, Home=$home_size, Swap=$swap_size, Boot=$boot_size"
|
||||
# Remove any existing LVM structures with force (only on target drive)
|
||||
log "Removing existing PV structures on target drive..."
|
||||
for part in "${EXTERNAL_DRIVE}"*; do
|
||||
if [ -b "$part" ]; then
|
||||
pvremove -ff "$part" 2>/dev/null || true
|
||||
fi
|
||||
done
|
||||
|
||||
# Wipe and create partition table
|
||||
wipefs -a "$EXTERNAL_DRIVE"
|
||||
# Wipe filesystem signatures and partition table
|
||||
log "Wiping drive signatures..."
|
||||
wipefs -af "$EXTERNAL_DRIVE" 2>/dev/null || true
|
||||
dd if=/dev/zero of="$EXTERNAL_DRIVE" bs=1M count=100 2>/dev/null || true
|
||||
|
||||
# Force kernel to re-read partition table
|
||||
partprobe "$EXTERNAL_DRIVE" 2>/dev/null || true
|
||||
sleep 2
|
||||
|
||||
# Create new partition table
|
||||
log "Creating new partition table..."
|
||||
parted -s "$EXTERNAL_DRIVE" mklabel gpt
|
||||
|
||||
# Create EFI partition (512MB)
|
||||
log "Creating EFI partition..."
|
||||
parted -s "$EXTERNAL_DRIVE" mkpart primary fat32 1MiB 513MiB
|
||||
parted -s "$EXTERNAL_DRIVE" set 1 boot on
|
||||
parted -s "$EXTERNAL_DRIVE" set 1 esp on
|
||||
|
||||
# Create LVM partition (rest of disk)
|
||||
log "Creating LVM partition..."
|
||||
parted -s "$EXTERNAL_DRIVE" mkpart primary 513MiB 100%
|
||||
parted -s "$EXTERNAL_DRIVE" set 2 lvm on
|
||||
|
||||
# Wait for partitions
|
||||
sleep 3
|
||||
partprobe "$EXTERNAL_DRIVE"
|
||||
sleep 2
|
||||
# Force kernel to re-read the new partition table
|
||||
log "Refreshing partition table..."
|
||||
partprobe "$EXTERNAL_DRIVE" || {
|
||||
warning "partprobe failed, trying alternative methods..."
|
||||
echo 1 > /sys/block/$(basename "$EXTERNAL_DRIVE")/device/rescan 2>/dev/null || true
|
||||
hdparm -z "$EXTERNAL_DRIVE" 2>/dev/null || true
|
||||
}
|
||||
|
||||
# Verify partitions exist
|
||||
if [ ! -b "${EXTERNAL_DRIVE}1" ] || [ ! -b "${EXTERNAL_DRIVE}2" ]; then
|
||||
error "Partitions not created properly"
|
||||
fi
|
||||
# Wait for partitions to appear
|
||||
local retry_count=0
|
||||
while [ ! -b "${EXTERNAL_DRIVE}1" ] || [ ! -b "${EXTERNAL_DRIVE}2" ]; do
|
||||
sleep 2
|
||||
retry_count=$((retry_count + 1))
|
||||
if [ $retry_count -gt 10 ]; then
|
||||
error "Partitions not appearing after 20 seconds. Please reboot and try again."
|
||||
fi
|
||||
log "Waiting for partitions to appear... ($retry_count/10)"
|
||||
done
|
||||
|
||||
log "Partitions created successfully"
|
||||
|
||||
# Create filesystems
|
||||
mkfs.fat -F32 "${EXTERNAL_DRIVE}1" || error "Failed to create EFI filesystem"
|
||||
|
||||
# Setup LVM
|
||||
pvcreate "${EXTERNAL_DRIVE}2" || error "Failed to create physical volume"
|
||||
# Setup LVM with force flag to handle existing signatures
|
||||
log "Creating physical volume..."
|
||||
pvcreate -ff "${EXTERNAL_DRIVE}2" || error "Failed to create physical volume"
|
||||
|
||||
# Check if VG name already exists and handle it
|
||||
if vgs "$VG_NAME" >/dev/null 2>&1; then
|
||||
log "Volume group $VG_NAME already exists, removing it first..."
|
||||
vgremove -f "$VG_NAME" 2>/dev/null || true
|
||||
# Wait a moment for cleanup
|
||||
sleep 2
|
||||
fi
|
||||
|
||||
log "Creating volume group..."
|
||||
vgcreate "$VG_NAME" "${EXTERNAL_DRIVE}2" || error "Failed to create volume group"
|
||||
|
||||
# Verify VG creation
|
||||
if ! vgs "$VG_NAME" >/dev/null 2>&1; then
|
||||
error "Volume group $VG_NAME was not created successfully"
|
||||
fi
|
||||
|
||||
# Create logical volumes
|
||||
lvcreate -L "$ROOT_SIZE" -n "$ROOT_LV" "$VG_NAME" || error "Failed to create root LV"
|
||||
lvcreate -L "$HOME_SIZE" -n "$HOME_LV" "$VG_NAME" || error "Failed to create home LV"
|
||||
lvcreate -L "$SWAP_SIZE" -n "$SWAP_LV" "$VG_NAME" || error "Failed to create swap LV"
|
||||
lvcreate -L "$BOOT_SIZE" -n "$BOOT_LV" "$VG_NAME" || error "Failed to create boot LV"
|
||||
lvcreate -L "$root_size" -n "$ROOT_LV" "$VG_NAME" || error "Failed to create root LV"
|
||||
lvcreate -L "$home_size" -n "$HOME_LV" "$VG_NAME" || error "Failed to create home LV"
|
||||
lvcreate -L "$swap_size" -n "$SWAP_LV" "$VG_NAME" || error "Failed to create swap LV"
|
||||
lvcreate -L "$boot_size" -n "$BOOT_LV" "$VG_NAME" || error "Failed to create boot LV"
|
||||
|
||||
# Create filesystems on LVM volumes
|
||||
mkfs.ext4 -L "root" "/dev/$VG_NAME/$ROOT_LV" || error "Failed to create root filesystem"
|
||||
@@ -482,14 +635,23 @@ verify_lvm_boot() {
|
||||
cleanup() {
|
||||
log "Cleaning up..."
|
||||
|
||||
# Unmount filesystems
|
||||
# Unmount filesystems in reverse order
|
||||
umount "$WORK_DIR/external_root/boot/efi" 2>/dev/null || true
|
||||
umount "$WORK_DIR/external_root/dev" 2>/dev/null || true
|
||||
umount "$WORK_DIR/external_root/proc" 2>/dev/null || true
|
||||
umount "$WORK_DIR/external_root/sys" 2>/dev/null || true
|
||||
umount "$WORK_DIR/external_root/run" 2>/dev/null || true
|
||||
umount "$WORK_DIR/external_root" 2>/dev/null || true
|
||||
umount "$WORK_DIR/external_home" 2>/dev/null || true
|
||||
umount "$WORK_DIR/external_boot" 2>/dev/null || true
|
||||
umount "$WORK_DIR/internal_root" 2>/dev/null || true
|
||||
umount "$WORK_DIR/internal_home" 2>/dev/null || true
|
||||
|
||||
# Deactivate LVM volumes
|
||||
if [ -n "$VG_NAME" ]; then
|
||||
vgchange -an "$VG_NAME" 2>/dev/null || true
|
||||
fi
|
||||
|
||||
# Close encrypted partitions
|
||||
for mapper in /dev/mapper/migration_*; do
|
||||
if [ -b "$mapper" ]; then
|
||||
@@ -497,6 +659,9 @@ cleanup() {
|
||||
fi
|
||||
done
|
||||
|
||||
# Remove work directory
|
||||
rm -rf "$WORK_DIR" 2>/dev/null || true
|
||||
|
||||
success "Cleanup completed"
|
||||
}
|
||||
|
||||
@@ -509,12 +674,18 @@ main() {
|
||||
check_prerequisites
|
||||
detect_drives
|
||||
analyze_source_system
|
||||
calculate_partition_sizes
|
||||
|
||||
echo
|
||||
echo "Migration Summary:"
|
||||
echo " Source: $INTERNAL_DRIVE (current system)"
|
||||
echo " Target: $EXTERNAL_DRIVE (new LVM system)"
|
||||
echo " VG Name: $VG_NAME"
|
||||
echo " Planned sizes:"
|
||||
echo " Root: $CALCULATED_ROOT_SIZE"
|
||||
echo " Home: $CALCULATED_HOME_SIZE"
|
||||
echo " Swap: $CALCULATED_SWAP_SIZE"
|
||||
echo " Boot: $CALCULATED_BOOT_SIZE"
|
||||
echo
|
||||
|
||||
confirm_action "Start LVM migration?"
|
||||
|
||||
Reference in New Issue
Block a user