Update Proxmox/pbs_logrotate.sh

This commit is contained in:
2025-01-26 08:51:25 +00:00
parent 95b402aa0c
commit 23a5e5eb5a

View File

@@ -36,14 +36,35 @@ fi
log_directory="/var/log/proxmox-backup" log_directory="/var/log/proxmox-backup"
backup_directory="/var/log/proxmox-backup-backup" backup_directory="/var/log/proxmox-backup-backup"
mkdir -p "$backup_directory" mkdir -p "$backup_directory"
cron_command="/usr/bin/find $log_directory -mtime +$days_to_keep -type f -exec mv {} $backup_directory/ \; && /usr/bin/find $backup_directory -mtime +30 -type f -exec rm {} +" cron_command_base="/usr/bin/find $log_directory -mtime +"
cron_command_suffix=" -type f -exec mv {} $backup_directory/ \; && /usr/bin/find $backup_directory -mtime +30 -type f -exec rm {} +"
cron_command="$cron_command_base$days_to_keep$cron_command_suffix"
cron_job="0 3 * * * $cron_command" cron_job="0 3 * * * $cron_command"
# Check if the cron job already exists # Check if a similar cron job exists with a different number of days
existing_cron=$(crontab -l 2>/dev/null | grep -F "$cron_command") existing_cron=$(crontab -l 2>/dev/null | grep -F "$cron_command_base")
if [[ -n "$existing_cron" ]]; then if [[ -n "$existing_cron" ]]; then
color_echo "33" "A cron job for this command already exists. No new cron job will be added." existing_days=$(echo "$existing_cron" | grep -oP "\+\d+" | tr -d '+')
exit 0 if [[ "$existing_days" != "$days_to_keep" ]]; then
color_echo "33" "A cron job already exists with $existing_days days."
read -p "Do you want to update it to $days_to_keep days? (y/n): " update_confirm
if [[ "$update_confirm" == "y" || "$update_confirm" == "Y" ]]; then
updated_crontab=$(crontab -l 2>/dev/null | sed "s|$existing_cron|$cron_job|")
echo "$updated_crontab" | crontab -
if [[ $? -eq 0 ]]; then
color_echo "32" "Cron job updated successfully to $days_to_keep days."
else
color_echo "31" "Failed to update the cron job."
exit 1
fi
else
color_echo "33" "No changes made to the existing cron job."
exit 0
fi
else
color_echo "33" "A cron job with the same configuration already exists. No new job added."
exit 0
fi
fi fi
# Preserve existing crontab and add the new job at the bottom # Preserve existing crontab and add the new job at the bottom