fix: Implement proper block-level Borg backups

BREAKING CHANGE: Borg backups now store raw block devices instead of files

Changes:
- LV→Borg: Stores snapshot as raw block device (.img file) in Borg
- VG→Borg: Stores each LV as separate .img files in Borg repository
- No more file-level mounting - preserves exact block-level state
- Uses dd | borg create --stdin-name for LV backups
- Creates temporary .img files for VG backups
- Maintains all filesystem metadata, boot sectors, etc.
- Better deduplication for similar block patterns

Benefits:
- Exact block-level restoration possible
- Preserves all filesystem metadata
- Better suited for system/boot volume backups
- Still gets Borg's compression, deduplication, encryption
- Clear difference between LV and VG modes

Now LV→Borg and VG→Borg have distinct, useful purposes:
- LV→Borg: Single logical volume as one block device
- VG→Borg: All logical volumes as separate block devices
This commit is contained in:
root
2025-10-09 00:45:22 +02:00
parent 179a84e442
commit aee3d5019c
4 changed files with 113 additions and 137 deletions

View File

@@ -330,26 +330,18 @@ case "$MODE" in
log "Creating snapshot of source LV"
lvcreate -L1G -s -n "$SNAPSHOT_NAME" "$SOURCE" || error "Failed to create snapshot"
# Create temporary mount point
TEMP_MOUNT=$(mktemp -d -t borg_backup_XXXXXX)
# Mount snapshot
log "Mounting snapshot to $TEMP_MOUNT"
mount "$SNAPSHOT_PATH" "$TEMP_MOUNT" || error "Failed to mount snapshot"
# Create Borg archive
ARCHIVE_NAME="lv_${LV_NAME}_$(date +%Y%m%d_%H%M%S)"
log "Creating Borg archive: $ARCHIVE_NAME"
log "This may take a long time depending on data size..."
log "Creating Borg archive (block-level): $ARCHIVE_NAME"
log "Backing up raw snapshot block device to Borg..."
log "This preserves exact block-level state including filesystem metadata"
borg create --progress --stats "$TARGET::$ARCHIVE_NAME" "$TEMP_MOUNT" || error "Borg backup failed"
dd if="$SNAPSHOT_PATH" bs=4M | borg create --stdin-name "${LV_NAME}.img" --progress --stats "$TARGET::$ARCHIVE_NAME" - || error "Borg backup failed"
log "Borg backup completed successfully"
log "Block-level Borg backup completed successfully"
# Cleanup
log "Cleaning up mount point and snapshot"
umount "$TEMP_MOUNT" || warn "Failed to unmount"
rmdir "$TEMP_MOUNT"
log "Cleaning up snapshot"
lvremove -f "$SNAPSHOT_PATH" || warn "Failed to remove snapshot"
SNAPSHOT_PATH=""
@@ -400,16 +392,16 @@ case "$MODE" in
log "Found logical volumes: $(echo $LV_LIST | tr '\n' ' ')"
# Create base temp directory
# Create base temp directory for block images
BASE_TEMP_DIR=$(mktemp -d -t borg_vg_backup_XXXXXX)
SNAPSHOTS_CREATED=""
# Create snapshots and mount them
# Create snapshots and copy them to temporary block files
for LV_NAME in $LV_LIST; do
SNAPSHOT_NAME="${LV_NAME}_borg_snap"
SNAPSHOT_PATH="/dev/$SOURCE/$SNAPSHOT_NAME"
LV_PATH="/dev/$SOURCE/$LV_NAME"
MOUNT_POINT="$BASE_TEMP_DIR/$LV_NAME"
TEMP_IMAGE="$BASE_TEMP_DIR/${LV_NAME}.img"
log "Creating snapshot: $SNAPSHOT_NAME"
lvcreate -L500M -s -n "$SNAPSHOT_NAME" "$LV_PATH" || {
@@ -418,32 +410,25 @@ case "$MODE" in
}
SNAPSHOTS_CREATED="$SNAPSHOTS_CREATED $SNAPSHOT_PATH"
# Create mount point and mount
mkdir -p "$MOUNT_POINT"
log "Mounting $SNAPSHOT_PATH to $MOUNT_POINT"
mount "$SNAPSHOT_PATH" "$MOUNT_POINT" || {
warn "Failed to mount $SNAPSHOT_PATH"
# Copy snapshot to temporary image file
log "Creating block image for $LV_NAME"
dd if="$SNAPSHOT_PATH" of="$TEMP_IMAGE" bs=4M || {
warn "Failed to create block image for $LV_NAME"
continue
}
done
# Create Borg archive
ARCHIVE_NAME="vg_${SOURCE}_$(date +%Y%m%d_%H%M%S)"
log "Creating Borg archive: $ARCHIVE_NAME"
log "This may take a long time depending on data size..."
log "Creating Borg archive (block-level): $ARCHIVE_NAME"
log "Backing up all LV snapshots as raw block devices..."
borg create --progress --stats "$TARGET::$ARCHIVE_NAME" "$BASE_TEMP_DIR" || error "Borg backup failed"
log "Borg backup of entire VG completed successfully"
log "Block-level VG Borg backup completed successfully"
# Cleanup
log "Cleaning up mount points and snapshots"
for MOUNT_POINT in "$BASE_TEMP_DIR"/*; do
if mountpoint -q "$MOUNT_POINT" 2>/dev/null; then
umount "$MOUNT_POINT" || warn "Failed to unmount $MOUNT_POINT"
fi
done
log "Cleaning up temporary files and snapshots"
rm -rf "$BASE_TEMP_DIR"
for SNAPSHOT_PATH in $SNAPSHOTS_CREATED; do