Update Proxmox/pbs_logrotate.sh
This commit is contained in:
@@ -36,14 +36,35 @@ fi
|
||||
log_directory="/var/log/proxmox-backup"
|
||||
backup_directory="/var/log/proxmox-backup-backup"
|
||||
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"
|
||||
|
||||
# Check if the cron job already exists
|
||||
existing_cron=$(crontab -l 2>/dev/null | grep -F "$cron_command")
|
||||
# Check if a similar cron job exists with a different number of days
|
||||
existing_cron=$(crontab -l 2>/dev/null | grep -F "$cron_command_base")
|
||||
if [[ -n "$existing_cron" ]]; then
|
||||
color_echo "33" "A cron job for this command already exists. No new cron job will be added."
|
||||
exit 0
|
||||
existing_days=$(echo "$existing_cron" | grep -oP "\+\d+" | tr -d '+')
|
||||
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
|
||||
|
||||
# Preserve existing crontab and add the new job at the bottom
|
||||
|
||||
Reference in New Issue
Block a user