Proxmox Backup Script

#!/bin/bash


# --- Configuration Settings ---

# Directory where backup archives will be stored.

# IMPORTANT: Ensure this path has enough free space and proper permissions.

# This path is now set to a mount point typically used for Proxmox storage.

BACKUP_DIR="/mnt/pve/hostbackup/dump"


# Timestamp format for the backup filename (YearMonthDay_HourMinuteSecond).

DATE_FORMAT=$(date +%Y%m%d_%H%M%S)


# Full filename of the backup archive.

BACKUP_FILE="proxmox_config_${DATE_FORMAT}.tar.zst"


# Log file for recording script execution details.

LOG_FILE="${BACKUP_DIR}/backup_log_${DATE_FORMAT}.log"


# List of critical configuration paths to back up.

# !!! IMPORTANT: Verify these paths exist on your Proxmox host. !!!

CONFIG_PATHS=(

    "/etc/pve"                 # Proxmox cluster configuration, VM/LXC configs, storage.cfg, etc. (MOST IMPORTANT)

    "/etc/network/interfaces"  # Network configuration of the Proxmox host.

    "/etc/hosts"               # Hostname to IP address mappings.

    "/etc/resolv.conf"         # DNS resolver configuration.

    "/etc/hostname"            # Hostname of the Proxmox server.

    # Add the following paths if you have customized them:

    # "/etc/fstab"             # Filesystem table (if custom mounts are configured).

    # "/etc/vzdump.conf"       # vzdump backup configuration (if customized).

)


# --- Script Start ---

echo "--- Proxmox Configuration Backup Script (using tar.zst) ---" | tee -a "$LOG_FILE"

echo "Backup started at: $(date)" | tee -a "$LOG_FILE"

echo "-----------------------------------------" | tee -a "$LOG_FILE"


# Check if 'zstd' command is available

if ! command -v zstd &> /dev/null

then

    echo "ERROR: 'zstd' command not found. Please install it (e.g., 'apt install zstd')." | tee -a "$LOG_FILE"

    exit 1

fi


# 1. Check and create the backup directory

# It's crucial that /mnt/pve/hostbackup/dump is correctly mounted and accessible.

if [ ! -d "$BACKUP_DIR" ]; then

    echo "Creating backup directory: $BACKUP_DIR" | tee -a "$LOG_FILE"

    mkdir -p "$BACKUP_DIR"

    if [ $? -ne 0 ]; then

        echo "ERROR: Failed to create backup directory $BACKUP_DIR. Please ensure the mount point is accessible and writable." | tee -a "$LOG_FILE"

        exit 1

    fi

else

    echo "Using existing backup directory: $BACKUP_DIR" | tee -a "$LOG_FILE"

fi


# 2. Create a temporary directory for copying config files

TEMP_CONFIG_DIR="${BACKUP_DIR}/temp_config_${DATE_FORMAT}"

echo "Creating temporary directory: $TEMP_CONFIG_DIR" | tee -a "$LOG_FILE"

mkdir -p "$TEMP_CONFIG_DIR"

if [ $? -ne 0 ]; then

    echo "ERROR: Failed to create temporary directory." | tee -a "$LOG_FILE"

    exit 1

fi


# Loop through each path and copy it to the temporary directory.

for path in "${CONFIG_PATHS[@]}"; do

    if [ -e "$path" ]; then # Check if the path exists

        echo "Copying: $path" | tee -a "$LOG_FILE"

        cp -r "$path" "$TEMP_CONFIG_DIR/"

        if [ $? -ne 0 ]; then

            echo "WARNING: Failed to copy $path." | tee -a "$LOG_FILE"

        fi

    else

        echo "WARNING: Path '$path' not found, skipping." | tee -a "$LOG_FILE"

    fi

done


# 3. Compress the copied configuration files into a .tar.zst archive

echo "Compressing configuration files to: ${BACKUP_DIR}/${BACKUP_FILE}" | tee -a "$LOG_FILE"

# Use 'tar' with '--use-compress-program=zstd' for zstd compression.

# -c: create, -v: verbose, -f: file (specify filename)

# Change directory to TEMP_CONFIG_DIR before archiving to ensure relative paths within the tar.

pushd "$TEMP_CONFIG_DIR" > /dev/null

tar -cvf "${BACKUP_DIR}/${BACKUP_FILE}" --use-compress-program=zstd ./*

if [ $? -ne 0 ]; then

    echo "ERROR: Failed to compress backup file using 'tar' and 'zstd'." | tee -a "$LOG_FILE"

    popd > /dev/null # Go back to the original directory

    rm -rf "$TEMP_CONFIG_DIR" # Clean up temp directory even on error

    exit 1

fi

popd > /dev/null # Go back to the original directory


# 4. Remove the temporary directory

echo "Removing temporary directory: $TEMP_CONFIG_DIR" | tee -a "$LOG_FILE"

rm -rf "$TEMP_CONFIG_DIR"

if [ $? -ne 0 ]; then

    echo "WARNING: Failed to remove temporary directory $TEMP_CONFIG_DIR." | tee -a "$LOG_FILE"

fi


# 5. Summarize results

echo "-----------------------------------------" | tee -a "$LOG_FILE"

echo "Backup completed at: $(date)" | tee -a "$LOG_FILE"

echo "Backup file: ${BACKUP_DIR}/${BACKUP_FILE}" | tee -a "$LOG_FILE"

echo "Log file: $LOG_FILE" | tee -a "$LOG_FILE"

echo "--- Script End ---" | tee -a "$LOG_FILE"


exit 0


หมวดหมู่คอร์สเรียนออนไลน์