0% found this document useful (0 votes)
4 views1 page

Script Suse

This Bash script automates the cleanup of backup directories by deleting old backups that exceed a specified retention period while ensuring a minimum number of backups are retained. It logs the process, including skipped backups and deletions, to a designated log file. The script operates on a specified backup root directory and processes each database directory individually.

Uploaded by

it wii
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Script Suse

This Bash script automates the cleanup of backup directories by deleting old backups that exceed a specified retention period while ensuring a minimum number of backups are retained. It logs the process, including skipped backups and deletions, to a designated log file. The script operates on a specified backup root directory and processes each database directory individually.

Uploaded by

it wii
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#!

/bin/bash

BACKUP_ROOT="/hana/shared/backup_service/backups/HDB_10.12.9.101_30013/HDB"
RETENTION_DAYS=7
MIN_BACKUP_KEEP=2
LOG_FILE="/var/log/hana_backup_cleanup.log"
echo "Backup Cleanup Start : $(date)" >> $LOG_FILE
find "$BACKUP_ROOT" -mindepth 1 -maxdepth 1 -type d | while read DB_DIR
do

echo "Processing DB: $DB_DIR" >> $LOG_FILE

TOTAL=$(find "$DB_DIR" -mindepth 1 -maxdepth 1 | wc -l)

if [ "$TOTAL" -le "$MIN_BACKUP_KEEP" ]; then


echo "Skip - backup <= $MIN_BACKUP_KEEP" >> $LOG_FILE
continue
fi

# mengambil backup paling baru


NEWEST=$(find "$DB_DIR" -mindepth 1 -maxdepth 1 -printf "%T@ %p\n" \
| sort -nr \
| head -n $MIN_BACKUP_KEEP \
| awk '{print $2}')

# untuk mencari backup lama


find "$DB_DIR" -mindepth 1 -maxdepth 1 -mtime +$RETENTION_DAYS | while read
OLD_FILE
do

SKIP=0

for KEEP in $NEWEST


do
if [ "$OLD_FILE" = "$KEEP" ]; then
SKIP=1
break
fi
done

if [ "$SKIP" -eq 0 ]; then


echo "Deleting : $OLD_FILE" >> $LOG_FILE
rm -rf "$OLD_FILE"
else
echo "Keep newest : $OLD_FILE" >> $LOG_FILE
fi

done

done
echo "Backup Cleanup Finish : $(date)" >> $LOG_FILE

You might also like