Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Will remove oldest unneeded backups one by one unitl there are KEEP_FREE_GIGS gigabytes of space free on device containing STORAGE_DIR.
- # Unneeded backups are picked regarding daily/weekly/monthly structure.
- echo_with_time()
- {
- cur_time=$( date --rfc-3339=ns )
- echo "$cur_time $1"
- }
- alldone()
- {
- echo_with_time "$THIS_SCRIPT_NAME completed"
- echo
- exit 0
- }
- THIS_SCRIPT_NAME=$( basename $0 )
- LOG_DIR='/opt/gpms/logs'
- #DATE_POSTFIX=$( date '+_%Y_%m_%d_%H_%M_%S' )
- LOG_FILE="${LOG_DIR}/${THIS_SCRIPT_NAME}${DATE_POSTFIX}.log"
- if [ ! -d "$LOG_DIR" ]; then
- mkdir -p "$LOG_DIR"
- fi
- exec 1>>"$LOG_FILE" 2>&1 # Redirect all output to log
- echo_with_time "$THIS_SCRIPT_NAME started ----------------"
- KEEP_FREE_GIGS='1200' # Amount of freee space we need to keep in gigabytes
- KEEP_FREE_BYTES=$(( KEEP_FREE_GIGS * 1024 * 1024 * 1024 ))
- STORAGE_DIR='/mnt/misc2/storage/backup'
- DAILY_APP_STORAGE_DIR="${STORAGE_DIR}/app/daily"
- WEEKLY_APP_STORAGE_DIR="${STORAGE_DIR}/app/weekly"
- MONTHLY_APP_STORAGE_DIR="${STORAGE_DIR}/app/monthly"
- DAILY_DB_STORAGE_DIR="${STORAGE_DIR}/db/daily"
- WEEKLY_DB_STORAGE_DIR="${STORAGE_DIR}/db/weekly"
- MONTHLY_DB_STORAGE_DIR="${STORAGE_DIR}/db/monthly"
- echo "Storage dir is $STORAGE_DIR"
- files_for_removal+=$( find "${DAILY_APP_STORAGE_DIR}" -mtime +2 -type f)
- files_for_removal+=$'\n'
- files_for_removal+=$( find "${WEEKLY_APP_STORAGE_DIR}" -mtime +8 -type f )
- files_for_removal+=$'\n'
- files_for_removal+=$( find "${MONTHLY_APP_STORAGE_DIR}" -mtime +366 -type f )
- files_for_removal+=$'\n'
- files_for_removal+=$( find "${DAILY_DB_STORAGE_DIR}" -mtime +2 -type f )
- files_for_removal+=$'\n'
- files_for_removal+=$( find "${WEEKLY_DB_STORAGE_DIR}" -mtime +8 -type f )
- files_for_removal+=$'\n'
- files_for_removal+=$( find "${MONTHLY_DB_STORAGE_DIR}" -mtime +366 -type f )
- files_for_removal+=$'\n'
- files_for_removal=$( echo "$files_for_removal" | xargs stat --format='%Y %s %n' | sort -n )
- #free_space_gigs=$( df -P --block-size=1G "$STORAGE_DIR" | tail -n +2 | tr -s ' ' | cut -d ' ' -f 4 )
- free_space_gigs=$( df -P --block-size=1G "$STORAGE_DIR" | sed '1d;s/^\(\([^ ]*\)\s\+\)\{4\}.*/\2/' )
- free_space_bytes=$( df -P --block-size=1 "$STORAGE_DIR" | sed '1d;s/^\(\([^ ]*\)\s\+\)\{4\}.*/\2/' )
- echo "There are $free_space_bytes bytes (${free_space_gigs}G) free on the device"
- echo "Should keep $KEEP_FREE_BYTES bytes (${KEEP_FREE_GIGS}G) free"
- if [ "$free_space_bytes" -ge "$KEEP_FREE_BYTES" ]; then
- echo "No actions needed"
- alldone
- fi
- should_free_bytes=$(( KEEP_FREE_BYTES - free_space_bytes ))
- echo "Should free $should_free_bytes bytes"
- numfiles=0
- numfiles=$( echo "$files_for_removal" | wc -l )
- numallfiles=$( find "$STORAGE_DIR" -type f | wc -l )
- echo "Have $numfiles of $numallfiles files available for deletion"
- freed=0
- enough=''
- numfiles=0
- while read timestamp size file; do
- echo "Removing \"$file\" size $size bytes"
- rm "$file"
- # mv "$file" /mnt/misc1/for_removal
- freed=$(( freed + size))
- numfiles=$(( numfiles + 1 ))
- if [ "$freed" -ge "$should_free_bytes" ]; then
- enough='yes'
- break
- fi
- done <<< "$files_for_removal"
- echo "$numfiles files deleted"
- echo "Freed $freed bytes"
- if [ -z "$enough" ]; then
- echo 'That is not enough'
- else
- echo 'That is enough'
- fi
- alldone
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement