38 lines
6.1 KiB
Bash
38 lines
6.1 KiB
Bash
#!/bin/bash
|
|
set -euo pipefail
|
|
R='\033[0;31m';G='\033[0;32m';Y='\033[1;33m';N='\033[0m'
|
|
e(){ echo -e "${R}ERROR: $1${N}" >&2;exit 1; }
|
|
s(){ echo -e "${G}✓${N} $1"; }
|
|
w(){ echo -e "${Y}⚠${N} $1"; }
|
|
[[ $EUID -ne 0 ]] && e "This script must be run as root (use sudo)"
|
|
[[ ! -f "/usr/share/perl5/PVE/RESTEnvironment.pm" ]] && w "Proxmox VE may not be installed (RESTEnvironment.pm not found)" && w "The script will still be installed, but it may not work until Proxmox is installed"
|
|
f(){ local n=$1;printf "%'d" "$n" 2>/dev/null||echo "$n"|sed ':a;s/\B[0-9]\{3\}\>/,&/;ta';}
|
|
c(){ local m=$1;local a=$((m/50));local d=$((m*365/1000000));local w=$((m*52/1000000));local mo=$((m*12/1000000));local y=$((m/1000000));local t="";if [[ $y -gt 0 ]];then if [[ $y -eq 1 ]];then local rm=$((mo%12));[[ $rm -eq 0 ]] && t="~1 year" || [[ $rm -eq 1 ]] && t="~1 year 1 month" || t="~1 year $rm months";else local rm=$((mo%12));[[ $rm -eq 0 ]] && t="~$y years" || [[ $rm -eq 1 ]] && t="~$y years 1 month" || t="~$y years $rm months";fi;elif [[ $mo -gt 0 ]];then [[ $mo -eq 1 ]] && t="~1 month" || t="~$mo months";elif [[ $w -gt 0 ]];then [[ $w -eq 1 ]] && t="~1 week" || [[ $w -lt 4 ]] && t="~$w weeks" || t="~$((w/4)) months";elif [[ $d -gt 0 ]];then [[ $d -eq 1 ]] && t="~1 day" || t="~$d days";else t="<1 day";fi;echo "about $a entries ($t)";}
|
|
echo -e "\nSelect the maximum task history size (in increments of 50,000):\n================================================================"
|
|
echo -e "${Y}Note: 1,000,000 maxsize is roughly equivalent to 1 year of task history${N}\n (Time estimates are based on this baseline)\n================================================================"
|
|
M=();O=1;for ((s=50000;s<=4000000;s+=50000));do I=$(c $s);F=$(f $s);M+=("$s");printf " %2d) %12s - %s\n" "$O" "$F" "$I";O=$((O+1));done
|
|
echo "================================================================"
|
|
while true;do read -p "Enter your choice [1-$((O-1))] (default: 20 for 1,000,000): " ch;[[ -z "$ch" ]] && ch=20;if [[ "$ch" =~ ^[0-9]+$ ]] && [[ $ch -ge 1 ]] && [[ $ch -le $((O-1)) ]];then S=${M[$((ch-1))]};I=$(c $S);break;else echo -e "${R}Invalid choice. Please enter a number between 1 and $((O-1)).${N}";fi;done
|
|
F=$(f $S);echo "";s "Selected: $F maxsize - $I";echo -e "\nInstalling Proxmox task history size modification..."
|
|
mkdir -p /etc/cronscripts/change_pve_task_history_size || e "Failed to create directory /etc/cronscripts/change_pve_task_history_size"
|
|
T="/etc/cronscripts/change_pve_task_history_size/update_pve_max_task_history_size.sh"
|
|
[[ -f "$T" ]] && B="${T}.backup.$(date +%Y%m%d_%H%M%S)" && (cp "$T" "$B" && w "Existing script backed up to: $B" || e "Failed to backup existing script")
|
|
X="IyEvYmluL2Jhc2gKRklMRT0iL3Vzci9zaGFyZS9wZXJsNS9QVkUvUkVTVEVudmlyb25tZW50LnBtIgpTRUFSQ0g9Im15IFwkbWF4c2l6ZSA9IDUwMDAwOyAjIGFib3V0IDEwMDAgZW50cmllcyIKUkVQTEFDRT0ibXkgXCRtYXhzaXplID0gUkVQTEFDRV9TSVpFOyAjIFJFUExBQ0VfSU5GTyIKaWYgW1sgLWYgIiRGSUxFIiBdXTt0aGVuIGlmIGdyZXAgLXFGICIkU0VBUkNIIiAiJEZJTEUiO3RoZW4gbG9nZ2VyIC10IGNoYW5nZV9wdmVfdGFza19oaXN0b3J5X3NpemUgIlN0cmluZyBmb3VuZCBpbiAkRklMRS4gUmVwbGFjaW5nICckU0VBUkNIJyB3aXRoICckUkVQTEFDRScuIjtCQUNLVVBfRklMRT0iJEZJTEUuJChkYXRlICslWSVtJWRfJUglTSVTKSI7Y3AgIiRGSUxFIiAiJEJBQ0tVUF9GSUxFIjtsb2dnZXIgLXQgY2hhbmdlX3B2ZV90YXNrX2hpc3Rvcnlfc2l6ZSAiQmFja3VwIGNyZWF0ZWQ6ICRCQUNLVVBfRklMRSI7c2VkIC1pICJzfCRTRUFSQ0h8JFJFUExBQ0V8IiAiJEZJTEUiO2xvZ2dlciAtdCBjaGFuZ2VfcHZlX3Rhc2tfaGlzdG9yeV9zaXplICJTdHJpbmcgcmVwbGFjZWQgc3VjY2Vzc2Z1bGx5IGluICRGSUxFLiI7c3lzdGVtY3RsIHJlc3RhcnQgcHZlZGFlbW9uLnNlcnZpY2UgcHZlcHJveHkuc2VydmljZTtsb2dnZXIgLXQgY2hhbmdlX3B2ZV90YXNrX2hpc3Rvcnlfc2l6ZSAiU2VydmljZXMgcmVzdGFydGVkLiI7ZWxzZSBsb2dnZXIgLXQgY2hhbmdlX3B2ZV90YXNrX2hpc3Rvcnlfc2l6ZSAiU2VhcmNoIHN0cmluZyBub3QgZm91bmQgaW4gJEZJTEUuIE5vIGNoYW5nZXMgbWFkZS4iO2ZpO2Vsc2UgbG9nZ2VyIC10IGNoYW5nZV9wdmVfdGFza19oaXN0b3J5X3NpemUgIkZpbGUgJEZJTEUgZG9lcyBub3QgZXhpc3QhIjtmaQ=="
|
|
echo "$X"|base64 -d|sed "s/REPLACE_SIZE/$S/g;s/REPLACE_INFO/$I/g" > "$T" || e "Failed to write script to $T"
|
|
chmod +x "$T" || e "Failed to make script executable"
|
|
[[ ! -f "$T" ]] || [[ ! -x "$T" ]] && e "Script verification failed: $T"
|
|
s "Update script created at $T"
|
|
mkdir -p /etc/cron.d || e "Failed to create directory /etc/cron.d"
|
|
C="/etc/cron.d/change_pve_task_history_size"
|
|
[[ -f "$C" ]] && B="${C}.backup.$(date +%Y%m%d_%H%M%S)" && (cp "$C" "$B" && w "Existing cron file backed up to: $B" || e "Failed to backup existing cron file")
|
|
Y="QHJlYm9vdCByb290IC9ldGMvY3JvbnNjcmlwdHMvY2hhbmdlX3B2ZV90YXNrX2hpc3Rvcnlfc2l6ZS91cGRhdGVfcHZlX21heF90YXNrX2hpc3Rvcnlfc2l6ZS5zaAowICogKiAqICogcm9vdCAvZXRjL2Nyb25zY3JpcHRzL2NoYW5nZV9wdmVfdGFza19oaXN0b3J5X3NpemUvdXBkYXRlX3B2ZV9tYXhfdGFza19oaXN0b3J5X3NpemUuc2g="
|
|
echo "$Y"|base64 -d > "$C" || e "Failed to write cron file to $C"
|
|
chmod 644 "$C" || e "Failed to set permissions on cron file"
|
|
[[ ! -f "$C" ]] && e "Cron file verification failed: $C"
|
|
s "Cron file created at $C"
|
|
echo -e "\nVerifying installation..."
|
|
[[ -f "$T" ]] && [[ -x "$T" ]] && s "Update script verified: $T" || e "Update script verification failed: $T"
|
|
[[ -f "$C" ]] && s "Cron file verified: $C" || e "Cron file verification failed: $C"
|
|
if [[ -f "/usr/share/perl5/PVE/RESTEnvironment.pm" ]];then echo -e "\nProxmox detected. Running update script to test installation...";if "$T";then s "Update script executed successfully";sleep 1;echo -e "\nRecent log entries:\n----------------------------------------";L=$(journalctl -t change_pve_task_history_size -n 10 --no-pager 2>/dev/null||true);if [[ -n "$L" ]];then echo "$L";echo "----------------------------------------";s "Logs are being written correctly";else echo "(No log entries found yet - this is normal for a fresh installation)";echo "----------------------------------------";w "No log entries found (logs will appear after the script runs)";fi;else w "Update script execution returned non-zero exit code";w "Check logs manually: journalctl -t change_pve_task_history_size";fi;else w "Proxmox not detected - skipping test run";w "The script will run automatically once Proxmox is installed";fi
|
|
echo -e "\n${G}Installation complete!${N}\n - Update script: $T\n - Cron job: $C\n\nThe script will run on system reboot and every hour.\n\nTo check logs manually, use:\n journalctl -t change_pve_task_history_size"
|