#!/bin/bash EXPIRE_DAYS=14 SCAN_DIR=/backups if [ "$(id -u)" != "0" ]; then echo -e "\033[31mThis script requires superuser rights.\033[0m" exit 0 fi trap 'echo -e "\033[31mSomething went wrong\033[0m"; exit 1' EXIT set -e export DEBIAN_FRONTEND=noninteractive FILES=$(find $SCAN_DIR -type f) for file in $FILES; do timestamp=$(date -r $file +%Y%m%d); echo "Processing $file file.."; date1yrs=$(date -d "$timestamp" +%Y); date1days=$(date -d "$timestamp" +%j); date2yrs=$(date +%Y); date2days=$(date +%j); diffYear=$(expr "$date2yrs" - "$date1yrs"); diffYear2days=$(expr "$diffYear" \* 365); diffDays=$(expr "$date2days" - "$date1days"); DAYS=$(expr "$diffYear2days" + "$diffDays"); if [ "$DAYS" -ge $EXPIRE_DAYS ] then echo "Deleting $file file..."; rm "$file"; fi done trap - EXIT echo "All backups older than $EXPIRE_DAYS days are deleted."