#!/bin/bash
set -e

# ========= 配置区 =========
# 6T 备份盘 ext4 分区的 UUID
BACKUP_UUID="4a4cce30-e224-4513-8fec-49b0636d96d2"

# 挂载点（Clonezilla 默认镜像目录）
BACKUP_MNT="/home/partimag"

# 源磁盘设备名
SYS_DISK="nvme0n1"   # 系统盘（Windows + 引导）
DATA_DISK="nvme1n1"  # 2TB 数据盘（ext4）

# 镜像名称（也是 /home/partimag 下的目录名）
SYS_IMG="system_disk_backup"
DATA_IMG="data_2t_backup"
# =========================

echo "=== [1] 挂载备份盘 ==="
DEV_PATH="/dev/disk/by-uuid/${BACKUP_UUID}"

if [ ! -e "$DEV_PATH" ]; then
  echo "错误：找不到设备 $DEV_PATH"
  exit 1
fi

mkdir -p "$BACKUP_MNT"

if mount | grep -q " $BACKUP_MNT "; then
  echo "提示：$BACKUP_MNT 已经挂载，跳过挂载步骤。"
else
  mount "$DEV_PATH" "$BACKUP_MNT"
  echo "已将 $DEV_PATH 挂载到 $BACKUP_MNT"
fi

cd "$BACKUP_MNT"

echo
echo "=== [2] 备份系统盘：$SYS_DISK → $SYS_IMG（加密） ==="
echo "注意：接下来会要求输入加密密码，请务必记牢。"
echo

/usr/sbin/ocs-sr -q2 -batch -j2 -edio -z9p -i 0 -sfsck -enc -p true \
  savedisk "$SYS_IMG" "$SYS_DISK"

echo
echo "=== 系统盘备份完成 ==="
echo

echo "=== [3] 备份数据盘：$DATA_DISK → $DATA_IMG（不加密，含 fsck-y） ==="
echo

/usr/sbin/ocs-sr -q2 -batch -j2 -edio -z9p -i 0 -fsck-y -senc -p true \
  savedisk "$DATA_IMG" "$DATA_DISK"

echo
echo "=== 数据盘备份完成 ==="

echo "=== [4] 同步缓存并卸载备份盘 ==="
sync

# 尝试卸载备份盘
umount "$BACKUP_MNT" || echo "警告：无法卸载 $BACKUP_MNT，请稍后手动检查。"

echo "=== 所有备份任务完成 ==="

