Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fn_expire_backups fix final #148

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions rsync_tmbackup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ fn_expire_backups() {
local current_timestamp=$EPOCH
local last_kept_timestamp=9999999999

# Process each backup dir from most recent to oldest
for backup_dir in $(fn_find_backups | sort -r); do
# Process each backup dir from oldest to most recent
for backup_dir in $(fn_find_backups | sort); do

local backup_date=$(basename "$backup_dir")
local backup_timestamp=$(fn_parse_date "$backup_date")

Expand All @@ -106,6 +107,13 @@ fn_expire_backups() {
continue
fi

# If this is the first "for" iteration backup_dir points to the oldest backup
if [ "$last_kept_timestamp" == "9999999999" ]; then
last_kept_timestamp=$backup_timestamp
# Never delete the oldest backup (unless no space - then auto-expire wil kick in). We skip processing it and go to the next one
continue
fi

# Find which strategy token applies to this particular backup
for strategy_token in $(echo $EXPIRATION_STRATEGY | tr " " "\n" | sort -r -n); do
IFS=':' read -r -a t <<< "$strategy_token"
Expand All @@ -125,17 +133,30 @@ fn_expire_backups() {
break
fi

# for backup expiration condition we use absolute day values
local last_kept_timestamp_days=$((last_kept_timestamp / 86400))
local backup_timestamp_days=$((backup_timestamp / 86400))
local interval_since_last_kept_days=$((backup_timestamp_days - last_kept_timestamp_days))

local cut_off_interval_days=$((cut_off_interval / 86400))

# Check if the current backup is in the interval between
# the last backup that was kept and Y
local interval_since_last_kept=$((last_kept_timestamp - backup_timestamp))
if [ "$interval_since_last_kept" -lt "$cut_off_interval" ]; then
# to determine what to keep/delete we use days difference
if [ "$interval_since_last_kept_days" -lt "$cut_off_interval_days" ]; then

# Yes: Delete that one
fn_expire_backup "$backup_dir"
# backup deleted no point to check shorter timespan strateggies - go to the next backup
break

else
# No: Keep it

# No: Keep it.
last_kept_timestamp=$backup_timestamp
# and go to the next backup
break
fi
break
fi
done
done
Expand Down Expand Up @@ -397,7 +418,8 @@ if [ -n "$(fn_find "$INPROGRESS_FILE")" ]; then
fi
else
RUNNINGPID="$(fn_run_cmd "cat $INPROGRESS_FILE")"
if [ "$RUNNINGPID" = "$(pgrep -o -f "$APPNAME")" ]; then
if ps -p "$RUNNINGPID" -o command | grep "$APPNAME"
then
fn_log_error "Previous backup task is still active - aborting."
exit 1
fi
Expand Down