implement backup to home directory of user executing the script

This commit is contained in:
Meliox 2025-08-07 10:41:54 +02:00
parent 5feca17208
commit 762f0af980

View File

@ -16,19 +16,8 @@ HDD_ITEMS_PER_ROW=0
# Should new ones be added, also update logic in configure() function. # Should new ones be added, also update logic in configure() function.
KNOWN_CPU_SENSORS=("coretemp-isa-" "k10temp-pci-") KNOWN_CPU_SENSORS=("coretemp-isa-" "k10temp-pci-")
# This script's working directory # Overwrite default backup location
SCRIPT_CWD="$(dirname "$(readlink -f "$0")")" BACKUP_DIR=""
# Files backup location
BACKUP_DIR="$SCRIPT_CWD/backup"
# File paths
PVE_MANAGER_LIB_JS_FILE="/usr/share/pve-manager/js/pvemanagerlib.js"
NODES_PM_FILE="/usr/share/perl5/PVE/API2/Nodes.pm"
# Debug location
DEBUG_SAVE_PATH="$SCRIPT_CWD"
DEBUG_SAVE_FILENAME="sensorsdata.json"
##################### DO NOT EDIT BELOW ####################### ##################### DO NOT EDIT BELOW #######################
# Only to be used to debug on other systems. Save the "sensor -j" output into a json file. # Only to be used to debug on other systems. Save the "sensor -j" output into a json file.
@ -39,6 +28,17 @@ DEBUG_SAVE_FILENAME="sensorsdata.json"
DEBUG_REMOTE=false DEBUG_REMOTE=false
DEBUG_JSON_FILE="/tmp/sensordata.json" DEBUG_JSON_FILE="/tmp/sensordata.json"
# This script's working directory
SCRIPT_CWD="$(dirname "$(readlink -f "$0")")"
# Debug location
JSON_EXPORT_DIRECTORY="$SCRIPT_CWD"
JSON_EXPORT_FILENAME="sensorsdata.json"
# File paths
PVE_MANAGER_LIB_JS_FILE="/usr/share/pve-manager/js/pvemanagerlib.js"
NODES_PM_FILE="/usr/share/perl5/PVE/API2/Nodes.pm"
# Helper functions # Helper functions
function msg { function msg {
echo -e "\e[0m$1\e[0m" echo -e "\e[0m$1\e[0m"
@ -278,43 +278,9 @@ function install_mod {
# Provide sensor configuration # Provide sensor configuration
configure configure
# Create backup of original files # Make backup directory and perform backup
mkdir -p "$BACKUP_DIR" makeBackupDirectory
performBackup
local timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
# Perform backup
# Create backup of original file
cp "$NODES_PM_FILE" "$BACKUP_DIR/Nodes.pm.$timestamp"
# Verify backup file was created and is identical
if [ -f "$BACKUP_DIR/Nodes.pm.$timestamp" ]; then
if cmp -s "$NODES_PM_FILE" "$BACKUP_DIR/Nodes.pm.$timestamp"; then
msg "Backup of \"$NODES_PM_FILE\" saved to \"$BACKUP_DIR/Nodes.pm.$timestamp\" and verified."
else
msg "WARNING: Backup file \"$BACKUP_DIR/Nodes.pm.$timestamp\" differs from original. Exiting..."
exit 1
fi
else
msg "ERROR: Failed to create backup \"$BACKUP_DIR/Nodes.pm.$timestamp\". Exiting..."
exit 1
fi
# Create backup of original file
cp "$PVE_MANAGER_LIB_JS_FILE" "$BACKUP_DIR/pvemanagerlib.js.$timestamp"
# Verify backup file was created and is identical
if [ -f "$BACKUP_DIR/pvemanagerlib.js.$timestamp" ]; then
if cmp -s "$PVE_MANAGER_LIB_JS_FILE" "$BACKUP_DIR/pvemanagerlib.js.$timestamp"; then
msg "Backup of \"$PVE_MANAGER_LIB_JS_FILE\" saved to \"$BACKUP_DIR/pvemanagerlib.js.$timestamp\" and verified."
else
msg "WARNING: Backup file \"$BACKUP_DIR/pvemanagerlib.js.$timestamp\" differs from original. Exiting..."
exit 1
fi
else
msg "ERROR: Failed to create backup \"$BACKUP_DIR/pvemanagerlib.js.$timestamp\". Exiting..."
exit 1
fi
if [ $SENSORS_DETECTED = true ]; then if [ $SENSORS_DETECTED = true ]; then
local sensorsCmd local sensorsCmd
@ -994,16 +960,16 @@ function restart_proxy {
} }
function save_sensors_data { function save_sensors_data {
# Check if DEBUG_SAVE_PATH exists and is writable # Check if JSON_EXPORT_DIRECTORY exists and is writable
if [[ ! -d "$DEBUG_SAVE_PATH" || ! -w "$DEBUG_SAVE_PATH" ]]; then if [[ ! -d "$JSON_EXPORT_DIRECTORY" || ! -w "$JSON_EXPORT_DIRECTORY" ]]; then
err "Directory $DEBUG_SAVE_PATH does not exist or is not writable. No file could be saved." err "Directory $JSON_EXPORT_DIRECTORY does not exist or is not writable. No file could be saved."
return return
fi fi
# Check if command exists # Check if command exists
if (command -v sensors &>/dev/null); then if (command -v sensors &>/dev/null); then
# Save sensors output # Save sensors output
local filepath="${DEBUG_SAVE_PATH}/${DEBUG_SAVE_FILENAME}" local filepath="${JSON_EXPORT_DIRECTORY}/${DEBUG_SAVE_FILENAME}"
msg "Sensors data will be saved in $filepath" msg "Sensors data will be saved in $filepath"
# Prompt user for confirmation # Prompt user for confirmation
@ -1022,6 +988,50 @@ function save_sensors_data {
fi fi
} }
function makeBackupDirectory {
# Check if the BACKUP_DIR variable is set, if not, use the default backup
if [[ -z "$BACKUP_DIR" ]]; then
# If not set, use the default backup directory, which is based on the home directory and PVE-MODS
local DEFAULT_BACKUP_DIR="$HOME/PVE-MODS"
msg "Backup directory not set. Using default: $DEFAULT_BACKUP_DIR"
BACKUP_DIR="$DEFAULT_BACKUP_DIR"
fi
# Create the backup directory if it does not exist
if [[ ! -d "$BACKUP_DIR" ]]; then
mkdir -p "$BACKUP_DIR" 2>/dev/null || {
err "Failed to create backup directory: $BACKUP_DIR. Please check permissions."
}
msg "Created backup directory: $BACKUP_DIR"
else
msg "Backup directory already exists: $BACKUP_DIR"
fi
}
function performBackup {
local timestamp=$(date +%Y%m%d_%H%M%S)
# Create backup of original file
cp "$NODES_PM_FILE" "$BACKUP_DIR/Nodes.pm.$timestamp"
cp "$NODES_PM_FILE" "$BACKUP_DIR/Nodes.pm.$timestamp" || {
err "Failed to create backup file: \"$BACKUP_DIR/Nodes.pm.$timestamp\""
}
if [[ $(md5sum "$NODES_PM_FILE" | awk '{ print $1 }') != $(md5sum "$BACKUP_DIR/Nodes.pm.$timestamp" | awk '{ print $1 }') ]]; then
err "Backup is not identical to original: \"$BACKUP_DIR/Nodes.pm.$timestamp\""
fi
msg "Backup of \"$NODES_PM_FILE\" saved to \"$BACKUP_DIR/Nodes.pm.$timestamp\"."
# Create backup of original file
cp "$PVE_MANAGER_LIB_JS_FILE" "$BACKUP_DIR/pvemanagerlib.js.$timestamp"
cp "$PVE_MANAGER_LIB_JS_FILE" "$BACKUP_DIR/pvemanagerlib.js.$timestamp" | {
err "Failed to create backup file: \"$BACKUP_DIR/Nodes.pm.$timestamp\""
}
if [[ $(md5sum "$PVE_MANAGER_LIB_JS_FILE" | awk '{ print $1 }') != $(md5sum "$BACKUP_DIR/pvemanagerlib.js.$timestamp" | awk '{ print $1 }') ]]; then
err "Backup is not identical to original: \"$BACKUP_DIR/pvemanagerlib.js.$timestamp\""
fi
msg "Backup of \"$PVE_MANAGER_LIB_JS_FILE\" saved to \"$BACKUP_DIR/pvemanagerlib.js.$timestamp\"."
}
# Process the arguments using a while loop and a case statement # Process the arguments using a while loop and a case statement
executed=0 executed=0
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do