#!/bin/bash # Combined installation script for Proxmox task history size modification # This script installs both the update logic and the cron job set -euo pipefail # Exit on error, undefined vars, pipe failures # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Error handling function error_exit() { echo -e "${RED}ERROR: $1${NC}" >&2 exit 1 } # Success message function success_msg() { echo -e "${GREEN}✓${NC} $1" } # Warning message function warning_msg() { echo -e "${YELLOW}⚠${NC} $1" } # Check if running as root if [[ $EUID -ne 0 ]]; then error_exit "This script must be run as root (use sudo)" fi # Check if Proxmox is installed if [[ ! -f "/usr/share/perl5/PVE/RESTEnvironment.pm" ]]; then warning_msg "Proxmox VE may not be installed (RESTEnvironment.pm not found)" warning_msg "The script will still be installed, but it may not work until Proxmox is installed" fi echo "Installing Proxmox task history size modification..." # ============================================================================ # Part 1: Install the update logic script # ============================================================================ # Create parent directory if it does not exist if ! mkdir -p /etc/cronscripts/change_pve_task_history_size; then error_exit "Failed to create directory /etc/cronscripts/change_pve_task_history_size" fi # Path to the target script TARGET_SCRIPT="/etc/cronscripts/change_pve_task_history_size/update_pve_max_task_history_size.sh" # Backup existing script if it exists if [[ -f "$TARGET_SCRIPT" ]]; then BACKUP_SCRIPT="${TARGET_SCRIPT}.backup.$(date +%Y%m%d_%H%M%S)" if cp "$TARGET_SCRIPT" "$BACKUP_SCRIPT"; then warning_msg "Existing script backed up to: $BACKUP_SCRIPT" else error_exit "Failed to backup existing script" fi fi # Write the script contents if ! cat << 'EOF' > "$TARGET_SCRIPT" #!/bin/bash # /etc/cronscripts/change_pve_task_history_size/update_pve_max_task_history_size.sh # Path to the RESTEnvironment.pm file FILE="/usr/share/perl5/PVE/RESTEnvironment.pm" # Search and replacement strings SEARCH="my \$maxsize = 50000; # about 1000 entries" REPLACE="my \$maxsize = 1000000; # about 20000 entries, around 1 year task history" # Check if the file exists if [[ -f "$FILE" ]]; then # Check if the target string exists in the file if grep -qF "$SEARCH" "$FILE"; then logger -t change_pve_task_history_size "String found in $FILE. Replacing '$SEARCH' with '$REPLACE'." # Create a backup of the file BACKUP_FILE="$FILE.$(date +%Y%m%d_%H%M%S)" cp "$FILE" "$BACKUP_FILE" logger -t change_pve_task_history_size "Backup created: $BACKUP_FILE" # Replace the target string in the file sed -i "s|$SEARCH|$REPLACE|" "$FILE" logger -t change_pve_task_history_size "String replaced successfully in $FILE." # Restart Proxmox services systemctl restart pvedaemon.service pveproxy.service logger -t change_pve_task_history_size "Services restarted." else logger -t change_pve_task_history_size "Search string not found in $FILE. No changes made." fi else logger -t change_pve_task_history_size "File $FILE does not exist!" fi EOF then error_exit "Failed to write script to $TARGET_SCRIPT" fi # Make script executable if ! chmod +x "$TARGET_SCRIPT"; then error_exit "Failed to make script executable" fi # Verify script was created and is executable if [[ ! -f "$TARGET_SCRIPT" ]] || [[ ! -x "$TARGET_SCRIPT" ]]; then error_exit "Script verification failed: $TARGET_SCRIPT" fi success_msg "Update script created at $TARGET_SCRIPT" # ============================================================================ # Part 2: Install the cron job # ============================================================================ # Ensure parent directory exists if ! mkdir -p /etc/cron.d; then error_exit "Failed to create directory /etc/cron.d" fi # Target cron file TARGET_CRON="/etc/cron.d/change_pve_task_history_size" # Backup existing cron file if it exists if [[ -f "$TARGET_CRON" ]]; then BACKUP_CRON="${TARGET_CRON}.backup.$(date +%Y%m%d_%H%M%S)" if cp "$TARGET_CRON" "$BACKUP_CRON"; then warning_msg "Existing cron file backed up to: $BACKUP_CRON" else error_exit "Failed to backup existing cron file" fi fi # Write cron contents if ! cat << 'EOF' > "$TARGET_CRON" # Cron job for updating Proxmox task history size # /etc/cron.d/change_pve_task_history_size @reboot root /etc/cronscripts/change_pve_task_history_size/update_pve_max_task_history_size.sh 0 * * * * root /etc/cronscripts/change_pve_task_history_size/update_pve_max_task_history_size.sh EOF then error_exit "Failed to write cron file to $TARGET_CRON" fi # Correct permissions for cron.d files if ! chmod 644 "$TARGET_CRON"; then error_exit "Failed to set permissions on cron file" fi # Verify cron file was created if [[ ! -f "$TARGET_CRON" ]]; then error_exit "Cron file verification failed: $TARGET_CRON" fi success_msg "Cron file created at $TARGET_CRON" # ============================================================================ # Installation complete # ============================================================================ echo "" echo -e "${GREEN}Installation complete!${NC}" echo " - Update script: $TARGET_SCRIPT" echo " - Cron job: $TARGET_CRON" echo "" echo "The script will run on system reboot and every hour." echo "" echo "To verify installation, check the logs with:" echo " journalctl -t change_pve_task_history_size"