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
462 lines
15 KiB
Bash
Executable File
462 lines
15 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Enhanced Simple LVM Backup Script
|
|
# Supports three backup modes:
|
|
# 1. LV → LV: Update existing logical volume backup
|
|
# 2. LV → Raw: Create fresh backup on raw device
|
|
# 3. VG → Raw: Clone entire volume group to raw device
|
|
|
|
set -e
|
|
|
|
# Colors
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m'
|
|
|
|
usage() {
|
|
echo "Enhanced Simple LVM Backup Script with Borg Support"
|
|
echo ""
|
|
echo "Usage:"
|
|
echo " $0 lv-to-lv SOURCE_LV TARGET_LV"
|
|
echo " $0 lv-to-raw SOURCE_LV TARGET_DEVICE"
|
|
echo " $0 vg-to-raw SOURCE_VG TARGET_DEVICE"
|
|
echo " $0 lv-to-borg SOURCE_LV REPO_PATH [--new-repo] [--encryption MODE] [--passphrase PASS]"
|
|
echo " $0 vg-to-borg SOURCE_VG REPO_PATH [--new-repo] [--encryption MODE] [--passphrase PASS]"
|
|
echo ""
|
|
echo "Modes:"
|
|
echo " lv-to-lv - Update existing LV backup (SOURCE_LV → TARGET_LV)"
|
|
echo " lv-to-raw - Create fresh backup (SOURCE_LV → raw device)"
|
|
echo " vg-to-raw - Clone entire VG (SOURCE_VG → raw device)"
|
|
echo " lv-to-borg - Backup LV to Borg repository"
|
|
echo " vg-to-borg - Backup entire VG to Borg repository"
|
|
echo ""
|
|
echo "Borg Options:"
|
|
echo " --new-repo Create new repository"
|
|
echo " --encryption MODE Encryption mode: none, repokey, keyfile (default: repokey)"
|
|
echo " --passphrase PASS Repository passphrase"
|
|
echo ""
|
|
echo "Examples:"
|
|
echo " $0 lv-to-lv /dev/internal-vg/root /dev/backup-vg/root"
|
|
echo " $0 lv-to-raw /dev/internal-vg/root /dev/sdb"
|
|
echo " $0 vg-to-raw internal-vg /dev/sdb"
|
|
echo " $0 lv-to-borg /dev/internal-vg/root /path/to/borg/repo --new-repo"
|
|
echo " $0 vg-to-borg internal-vg /path/to/borg/repo --encryption repokey --passphrase mypass"
|
|
echo ""
|
|
echo "List available sources/targets:"
|
|
echo " ./list_drives.sh"
|
|
echo ""
|
|
exit 1
|
|
}
|
|
|
|
log() {
|
|
echo -e "${GREEN}[$(date '+%H:%M:%S')] $1${NC}"
|
|
}
|
|
|
|
error() {
|
|
echo -e "${RED}[ERROR] $1${NC}" >&2
|
|
cleanup_and_exit 1
|
|
}
|
|
|
|
warn() {
|
|
echo -e "${YELLOW}[WARNING] $1${NC}"
|
|
}
|
|
|
|
info() {
|
|
echo -e "${BLUE}[INFO] $1${NC}"
|
|
}
|
|
|
|
cleanup_and_exit() {
|
|
local exit_code=${1:-0}
|
|
|
|
if [ -n "$SNAPSHOT_PATH" ] && lvs "$SNAPSHOT_PATH" >/dev/null 2>&1; then
|
|
warn "Cleaning up snapshot: $SNAPSHOT_PATH"
|
|
lvremove -f "$SNAPSHOT_PATH" || warn "Failed to remove snapshot"
|
|
fi
|
|
|
|
exit $exit_code
|
|
}
|
|
|
|
# Trap for cleanup
|
|
trap 'cleanup_and_exit 130' INT TERM
|
|
|
|
# Parse arguments and options
|
|
if [ $# -lt 3 ]; then
|
|
usage
|
|
fi
|
|
|
|
MODE="$1"
|
|
SOURCE="$2"
|
|
TARGET="$3"
|
|
shift 3
|
|
|
|
# Parse Borg-specific options
|
|
NEW_REPO=false
|
|
ENCRYPTION="repokey"
|
|
PASSPHRASE=""
|
|
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--new-repo)
|
|
NEW_REPO=true
|
|
shift
|
|
;;
|
|
--encryption)
|
|
ENCRYPTION="$2"
|
|
shift 2
|
|
;;
|
|
--passphrase)
|
|
PASSPHRASE="$2"
|
|
shift 2
|
|
;;
|
|
*)
|
|
error "Unknown option: $1"
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Check if running as root
|
|
if [ "$EUID" -ne 0 ]; then
|
|
error "This script must be run as root"
|
|
fi
|
|
|
|
# Validate mode
|
|
case "$MODE" in
|
|
"lv-to-lv"|"lv-to-raw"|"vg-to-raw"|"lv-to-borg"|"vg-to-borg")
|
|
;;
|
|
*)
|
|
error "Invalid mode: $MODE"
|
|
;;
|
|
esac
|
|
|
|
# Check Borg requirements for Borg modes
|
|
if [[ "$MODE" == *"-to-borg" ]]; then
|
|
if ! command -v borg >/dev/null 2>&1; then
|
|
error "Borg Backup is not installed. Please install it: sudo apt install borgbackup"
|
|
fi
|
|
|
|
# Set up Borg environment
|
|
if [ -n "$PASSPHRASE" ]; then
|
|
export BORG_PASSPHRASE="$PASSPHRASE"
|
|
fi
|
|
fi
|
|
|
|
log "Enhanced Simple LVM Backup"
|
|
log "Mode: $MODE"
|
|
log "Source: $SOURCE"
|
|
log "Target: $TARGET"
|
|
|
|
# Mode-specific validation and execution
|
|
case "$MODE" in
|
|
"lv-to-lv")
|
|
# LV to LV backup
|
|
if [ ! -e "$SOURCE" ]; then
|
|
error "Source LV does not exist: $SOURCE"
|
|
fi
|
|
|
|
if [ ! -e "$TARGET" ]; then
|
|
error "Target LV does not exist: $TARGET"
|
|
fi
|
|
|
|
# Extract VG and LV names
|
|
VG_NAME=$(lvs --noheadings -o vg_name "$SOURCE" | tr -d ' ')
|
|
LV_NAME=$(lvs --noheadings -o lv_name "$SOURCE" | tr -d ' ')
|
|
SNAPSHOT_NAME="${LV_NAME}_backup_snap"
|
|
SNAPSHOT_PATH="/dev/$VG_NAME/$SNAPSHOT_NAME"
|
|
|
|
info "This will update the existing backup LV"
|
|
echo ""
|
|
echo -e "${YELLOW}Source LV: $SOURCE${NC}"
|
|
echo -e "${YELLOW}Target LV: $TARGET${NC}"
|
|
echo -e "${YELLOW}The target LV will be overwritten with current source data${NC}"
|
|
echo ""
|
|
read -p "Continue? (yes/no): " confirm
|
|
|
|
if [ "$confirm" != "yes" ]; then
|
|
echo "Backup cancelled."
|
|
exit 0
|
|
fi
|
|
|
|
log "Creating snapshot of source LV"
|
|
lvcreate -L1G -s -n "$SNAPSHOT_NAME" "$SOURCE" || error "Failed to create snapshot"
|
|
|
|
log "Copying snapshot to target LV"
|
|
if command -v pv >/dev/null 2>&1; then
|
|
pv "$SNAPSHOT_PATH" | dd of="$TARGET" bs=4M || error "Copy failed"
|
|
else
|
|
dd if="$SNAPSHOT_PATH" of="$TARGET" bs=4M status=progress || error "Copy failed"
|
|
fi
|
|
|
|
log "Cleaning up snapshot"
|
|
lvremove -f "$SNAPSHOT_PATH" || warn "Failed to remove snapshot"
|
|
SNAPSHOT_PATH=""
|
|
|
|
log "LV to LV backup completed successfully"
|
|
;;
|
|
|
|
"lv-to-raw")
|
|
# LV to raw device backup
|
|
if [ ! -e "$SOURCE" ]; then
|
|
error "Source LV does not exist: $SOURCE"
|
|
fi
|
|
|
|
if [ ! -e "$TARGET" ]; then
|
|
error "Target device does not exist: $TARGET"
|
|
fi
|
|
|
|
# Extract VG and LV names
|
|
VG_NAME=$(lvs --noheadings -o vg_name "$SOURCE" | tr -d ' ')
|
|
LV_NAME=$(lvs --noheadings -o lv_name "$SOURCE" | tr -d ' ')
|
|
SNAPSHOT_NAME="${LV_NAME}_backup_snap"
|
|
SNAPSHOT_PATH="/dev/$VG_NAME/$SNAPSHOT_NAME"
|
|
|
|
info "This will create a fresh backup on raw device"
|
|
echo ""
|
|
echo -e "${YELLOW}Source LV: $SOURCE${NC}"
|
|
echo -e "${YELLOW}Target Device: $TARGET${NC}"
|
|
echo -e "${RED}WARNING: All data on $TARGET will be lost!${NC}"
|
|
echo ""
|
|
read -p "Continue? (yes/no): " confirm
|
|
|
|
if [ "$confirm" != "yes" ]; then
|
|
echo "Backup cancelled."
|
|
exit 0
|
|
fi
|
|
|
|
log "Creating snapshot of source LV"
|
|
lvcreate -L1G -s -n "$SNAPSHOT_NAME" "$SOURCE" || error "Failed to create snapshot"
|
|
|
|
log "Copying snapshot to target device"
|
|
if command -v pv >/dev/null 2>&1; then
|
|
pv "$SNAPSHOT_PATH" | dd of="$TARGET" bs=4M || error "Copy failed"
|
|
else
|
|
dd if="$SNAPSHOT_PATH" of="$TARGET" bs=4M status=progress || error "Copy failed"
|
|
fi
|
|
|
|
log "Cleaning up snapshot"
|
|
lvremove -f "$SNAPSHOT_PATH" || warn "Failed to remove snapshot"
|
|
SNAPSHOT_PATH=""
|
|
|
|
log "LV to raw device backup completed successfully"
|
|
;;
|
|
|
|
"vg-to-raw")
|
|
# VG to raw device backup
|
|
if ! vgs "$SOURCE" >/dev/null 2>&1; then
|
|
error "Source VG does not exist: $SOURCE"
|
|
fi
|
|
|
|
if [ ! -e "$TARGET" ]; then
|
|
error "Target device does not exist: $TARGET"
|
|
fi
|
|
|
|
# Get the first PV of the source VG
|
|
SOURCE_PV=$(vgs --noheadings -o pv_name "$SOURCE" | head -n1 | tr -d ' ')
|
|
|
|
if [ -z "$SOURCE_PV" ]; then
|
|
error "No physical volumes found for VG: $SOURCE"
|
|
fi
|
|
|
|
info "This will clone the entire volume group"
|
|
echo ""
|
|
echo -e "${YELLOW}Source VG: $SOURCE${NC}"
|
|
echo -e "${YELLOW}Source PV: $SOURCE_PV${NC}"
|
|
echo -e "${YELLOW}Target Device: $TARGET${NC}"
|
|
echo -e "${RED}WARNING: All data on $TARGET will be lost!${NC}"
|
|
echo -e "${BLUE}This preserves LVM metadata and all logical volumes${NC}"
|
|
echo ""
|
|
read -p "Continue? (yes/no): " confirm
|
|
|
|
if [ "$confirm" != "yes" ]; then
|
|
echo "Backup cancelled."
|
|
exit 0
|
|
fi
|
|
|
|
log "Copying entire PV to target device"
|
|
log "This preserves LVM structure and all LVs"
|
|
|
|
if command -v pv >/dev/null 2>&1; then
|
|
pv "$SOURCE_PV" | dd of="$TARGET" bs=4M || error "Copy failed"
|
|
else
|
|
dd if="$SOURCE_PV" of="$TARGET" bs=4M status=progress || error "Copy failed"
|
|
fi
|
|
|
|
log "VG to raw device backup completed successfully"
|
|
log "Target device now contains complete LVM structure"
|
|
;;
|
|
|
|
"lv-to-borg")
|
|
# LV to Borg repository backup
|
|
if [ ! -e "$SOURCE" ]; then
|
|
error "Source LV does not exist: $SOURCE"
|
|
fi
|
|
|
|
# Extract VG and LV names
|
|
VG_NAME=$(lvs --noheadings -o vg_name "$SOURCE" | tr -d ' ')
|
|
LV_NAME=$(lvs --noheadings -o lv_name "$SOURCE" | tr -d ' ')
|
|
SNAPSHOT_NAME="${LV_NAME}_borg_snap"
|
|
SNAPSHOT_PATH="/dev/$VG_NAME/$SNAPSHOT_NAME"
|
|
|
|
info "This will backup LV to Borg repository"
|
|
echo ""
|
|
echo -e "${YELLOW}Source LV: $SOURCE${NC}"
|
|
echo -e "${YELLOW}Repository: $TARGET${NC}"
|
|
if [ "$NEW_REPO" = true ]; then
|
|
echo -e "${BLUE}Will create new repository${NC}"
|
|
else
|
|
echo -e "${BLUE}Will add to existing repository${NC}"
|
|
fi
|
|
echo -e "${BLUE}Encryption: $ENCRYPTION${NC}"
|
|
echo ""
|
|
read -p "Continue? (yes/no): " confirm
|
|
|
|
if [ "$confirm" != "yes" ]; then
|
|
echo "Backup cancelled."
|
|
exit 0
|
|
fi
|
|
|
|
# Initialize repository if needed
|
|
if [ "$NEW_REPO" = true ]; then
|
|
log "Creating new Borg repository: $TARGET"
|
|
if [ "$ENCRYPTION" = "none" ]; then
|
|
borg init --encryption=none "$TARGET" || error "Failed to initialize repository"
|
|
else
|
|
borg init --encryption="$ENCRYPTION" "$TARGET" || error "Failed to initialize repository"
|
|
fi
|
|
log "Repository initialized successfully"
|
|
fi
|
|
|
|
log "Creating snapshot of source LV"
|
|
lvcreate -L1G -s -n "$SNAPSHOT_NAME" "$SOURCE" || error "Failed to create snapshot"
|
|
|
|
# Create Borg archive
|
|
ARCHIVE_NAME="lv_${LV_NAME}_$(date +%Y%m%d_%H%M%S)"
|
|
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"
|
|
|
|
dd if="$SNAPSHOT_PATH" bs=4M | borg create --stdin-name "${LV_NAME}.img" --progress --stats "$TARGET::$ARCHIVE_NAME" - || error "Borg backup failed"
|
|
|
|
log "Block-level Borg backup completed successfully"
|
|
|
|
# Cleanup
|
|
log "Cleaning up snapshot"
|
|
lvremove -f "$SNAPSHOT_PATH" || warn "Failed to remove snapshot"
|
|
SNAPSHOT_PATH=""
|
|
|
|
log "LV to Borg backup completed successfully"
|
|
;;
|
|
|
|
"vg-to-borg")
|
|
# VG to Borg repository backup
|
|
if ! vgs "$SOURCE" >/dev/null 2>&1; then
|
|
error "Source VG does not exist: $SOURCE"
|
|
fi
|
|
|
|
info "This will backup entire VG to Borg repository"
|
|
echo ""
|
|
echo -e "${YELLOW}Source VG: $SOURCE${NC}"
|
|
echo -e "${YELLOW}Repository: $TARGET${NC}"
|
|
if [ "$NEW_REPO" = true ]; then
|
|
echo -e "${BLUE}Will create new repository${NC}"
|
|
else
|
|
echo -e "${BLUE}Will add to existing repository${NC}"
|
|
fi
|
|
echo -e "${BLUE}Encryption: $ENCRYPTION${NC}"
|
|
echo -e "${BLUE}This will backup all logical volumes in the VG${NC}"
|
|
echo ""
|
|
read -p "Continue? (yes/no): " confirm
|
|
|
|
if [ "$confirm" != "yes" ]; then
|
|
echo "Backup cancelled."
|
|
exit 0
|
|
fi
|
|
|
|
# Initialize repository if needed
|
|
if [ "$NEW_REPO" = true ]; then
|
|
log "Creating new Borg repository: $TARGET"
|
|
if [ "$ENCRYPTION" = "none" ]; then
|
|
borg init --encryption=none "$TARGET" || error "Failed to initialize repository"
|
|
else
|
|
borg init --encryption="$ENCRYPTION" "$TARGET" || error "Failed to initialize repository"
|
|
fi
|
|
log "Repository initialized successfully"
|
|
fi
|
|
|
|
# Get all LVs in VG
|
|
LV_LIST=$(lvs --noheadings -o lv_name "$SOURCE" | tr -d ' ')
|
|
if [ -z "$LV_LIST" ]; then
|
|
error "No logical volumes found in VG: $SOURCE"
|
|
fi
|
|
|
|
log "Found logical volumes: $(echo $LV_LIST | tr '\n' ' ')"
|
|
|
|
# Create base temp directory for block images
|
|
BASE_TEMP_DIR=$(mktemp -d -t borg_vg_backup_XXXXXX)
|
|
SNAPSHOTS_CREATED=""
|
|
|
|
# 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"
|
|
TEMP_IMAGE="$BASE_TEMP_DIR/${LV_NAME}.img"
|
|
|
|
log "Creating snapshot: $SNAPSHOT_NAME"
|
|
lvcreate -L500M -s -n "$SNAPSHOT_NAME" "$LV_PATH" || {
|
|
warn "Failed to create snapshot for $LV_NAME"
|
|
continue
|
|
}
|
|
SNAPSHOTS_CREATED="$SNAPSHOTS_CREATED $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 (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 "Block-level VG Borg backup completed successfully"
|
|
|
|
# Cleanup
|
|
log "Cleaning up temporary files and snapshots"
|
|
rm -rf "$BASE_TEMP_DIR"
|
|
|
|
for SNAPSHOT_PATH in $SNAPSHOTS_CREATED; do
|
|
log "Removing snapshot $SNAPSHOT_PATH"
|
|
lvremove -f "$SNAPSHOT_PATH" || warn "Failed to remove snapshot $SNAPSHOT_PATH"
|
|
done
|
|
|
|
log "VG to Borg backup completed successfully"
|
|
;;
|
|
esac
|
|
|
|
echo ""
|
|
echo -e "${GREEN}SUCCESS: Backup completed!${NC}"
|
|
echo ""
|
|
echo "Next steps:"
|
|
case "$MODE" in
|
|
"lv-to-lv")
|
|
echo "- Your backup LV has been updated"
|
|
echo "- You can mount $TARGET to verify the backup"
|
|
;;
|
|
"lv-to-raw")
|
|
echo "- Your backup device contains a raw copy of the LV"
|
|
echo "- You can mount $TARGET directly or restore from it"
|
|
;;
|
|
"vg-to-raw")
|
|
echo "- Your backup device contains the complete LVM structure"
|
|
echo "- You can boot from $TARGET or import the VG for recovery"
|
|
echo "- To access: vgimport or boot directly from the device"
|
|
;;
|
|
esac
|
|
echo "" |