Skip to content

Commit

Permalink
also cleanup catalogs
Browse files Browse the repository at this point in the history
  • Loading branch information
per2jensen committed Feb 27, 2024
1 parent e11a213 commit e90c62a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
65 changes: 58 additions & 7 deletions bin/cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#
# will only work on DIFF and INC, not FULL archives
#
# do not call manager if cleanup is performed in a non-standard directory, (--alternate-archive-dir))
#
# DIFF_AGE & INC_AGE is defined in the conf file

LOCAL_BACKUP_DIR=""
Expand All @@ -34,6 +36,23 @@ SCRIPTNAME=$(basename "$0")

VERSION=@@DEV-VERSION@@

CLEANUP_OK=0

call_manager () {
local ARCHIVE="$1"
if [[ "$LOCAL_BACKUP_DIR" == "1" ]]; then
"$SCRIPTDIRPATH"/manager.sh --remove-specific-archive "$ARCHIVE" --local-backup-dir
if [[ $? != "0" ]]; then
CLEANUP_OK=1
fi
else
"$SCRIPTDIRPATH"/manager.sh --remove-specific-archive "$ARCHIVE"
if [[ $? != "0" ]]; then
CLEANUP_OK=1
fi
fi
}

# Get the options
while [ -n "$1" ]; do
case "$1" in
Expand Down Expand Up @@ -112,15 +131,15 @@ fi
# make sure mounts are in order
mountPrereqs

# check if type and date given for the --cleanup-archive seems reasonable, and then delete files
# check if type and date given for the --cleanup-specific-archive seems reasonable, and then delete files
if [[ $SPECIFIC_ARCHIVE != "" ]]; then
TODAYS_SECS=$(date +%s --date $(date -I))
REGEX_END_OF_DATE_SECS=$(date +%s --date "2029-12-31")
if (( $TODAYS_SECS > $REGEX_END_OF_DATE_SECS )); then
log "ERROR regex date checker no longer valid, please modify it, exiting"
exit 1
fi
CLEANUP_DATE=$(echo "$SPECIFIC_ARCHIVE"|grep -E -o "202[2-9]-(0[1-9]|1[012])-([0-2][0-9]|3[01])")
CLEANUP_DATE=$(echo "$SPECIFIC_ARCHIVE"|grep -E -o "20[2-9][0-9]-(0[1-9]|1[012])-(10|20|[0-2][1-9]|3[01])")
if [[ $CLEANUP_DATE == "" ]]; then
log "ERROR archive date is bad, exiting"
exit 1
Expand All @@ -133,12 +152,23 @@ if [[ $SPECIFIC_ARCHIVE != "" ]]; then
exit 1
fi

while IFS= read -r -d "" file
while IFS= read -r -d "" file
do
rm -f "${file}" && log "clean up: \"${file}\""
if [[ $? != "0" ]]; then
CLEANUP_OK=1
fi
done < <(find "$MOUNT_POINT" -type f -name "${SPECIFIC_ARCHIVE}*.dar*" -print0)

log "$SCRIPTNAME ended normally"
echo "ARKIV som skal slettes: $SPECIFIC_ARCHIVE"
call_manager "$SPECIFIC_ARCHIVE"
echo "tilbage fra manager"

if [[ "$CLEANUP_OK" == "0" ]]; then
log "$SCRIPTNAME ended normally"
else
log_error "$SCRIPTNAME ended with errors"
fi
exit
fi

Expand All @@ -156,12 +186,22 @@ do
FILE_DATE=$(echo $FILE_DATE|grep -o -E "[0-9]{4}-[0-9]{2}-[0-9]{2}")
FILE_DATE_SECS=$(date +%s --date "$FILE_DATE")
if (( DIFF_AGE_SECS >= FILE_DATE_SECS )); then
rm -f "${file}" && log "clean up: \"${file}\""
rm -f "${file}"
if [[ $? == "0" ]]; then
log "clean up: \"${file}\""
# do not call manager if cleanup is performed in a non-standard directory
if [[ "$ALTERNATE_ARCHIVE_DIR" == "" ]]; then
ARCHIVE=$(echo $file|grep -E -o "^.*20[2-9][0-9]-(0[1-9]|1[012])-(10|20|[0-2][1-9]|3[01])")
echo "ARKIV som skal slettes: $ARCHIVE"
call_manager "$ARCHIVE"
fi
else
CLEANUP_OK=1
fi
fi
done < <(find "$MOUNT_POINT" -type f -name "*_DIFF_*.dar*" -print0)



INC_AGE_DATE=$(date --date="-${INC_AGE} days" -I)
if [[ $? != "0" ]]; then
log "ERROR \"INC_AGE_DATE\" calc error"
Expand All @@ -175,7 +215,18 @@ do
FILE_DATE=$(echo $FILE_DATE|grep -o -E "[0-9]{4}-[0-9]{2}-[0-9]{2}")
FILE_DATE_SECS=$(date +%s --date "$FILE_DATE")
if (( INC_AGE_SECS >= FILE_DATE_SECS )); then
rm -f "${file}" && log "clean up: \"${file}\""
rm -f "${file}"
if [[ $? == "0" ]]; then
log "clean up: \"${file}\""
# do not call manager if cleanup is performed in a non-standard directory
if [[ "$ALTERNATE_ARCHIVE_DIR" == "" ]]; then
ARCHIVE=$(echo $file|grep -E -o "^.*20[2-9][0-9]-(0[1-9]|1[012])-(10|20|[0-2][1-9]|3[01])")
echo "ARKIV som skal slettes: $ARCHIVE"
call_manager "$ARCHIVE"
fi
else
CLEANUP_OK=1
fi
fi
done < <(find "$MOUNT_POINT" -type f -name "*_INC_*.dar*" -print0)

Expand Down
2 changes: 1 addition & 1 deletion test/test-cleanup-alternate-dir.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! /bin/bash

# verify cleanup.sh works on the directory given in the --alternate-archive-dir option
# that option requires --local-backup-dir to be givesn also
# that option requires --local-backup-dir


TEST_RESULT=0
Expand Down
10 changes: 9 additions & 1 deletion test/test-cleanup-specific-archive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ touch "$TESTDIR"/archives/TEST_INC_${DAY_1_OLD}.2.dar
cp -R "$TESTDIR"/archives "$TESTDIR"/archives2

# test 1
echo "test 1"
"$TESTDIR"/bin/cleanup.sh --local-backup-dir --cleanup-specific-archive TEST_DIFF_${DAY_1_OLD}
COUNT=$(grep -c -E "clean up:.*_DIFF_" "$TESTDIR"/archives/dar-backup.log)
echo "COUNT: $COUNT"
Expand All @@ -42,6 +43,7 @@ if [[ "$COUNT" != "0" ]]; then
fi

# test 2
echo "test 2"
"$TESTDIR"/bin/cleanup.sh --local-backup-dir --cleanup-specific-archive "TEST_INC_${DAY_1_OLD}"
COUNT=$(grep -c -E "clean up:.*_INC_" "$TESTDIR"/archives/dar-backup.log)
echo "COUNT: $COUNT"
Expand All @@ -51,6 +53,7 @@ if [[ "$COUNT" != "2" ]]; then
fi

# test3 check alternate-archive-dir also works
echo "test 3"
"$TESTDIR"/bin/cleanup.sh --local-backup-dir --alternate-archive-dir "$TESTDIR/archives2" --cleanup-specific-archive "TEST_DIFF_${DAY_1_OLD}"
COUNT=$(grep -c -E "clean up:.*_DIFF_" "$TESTDIR"/archives/dar-backup.log)
echo "COUNT: $COUNT"
Expand All @@ -61,6 +64,7 @@ fi


#test 4 fail on date
echo "test 4"
TEST_DATE=2022-13-01
touch "$TESTDIR"/archives/TEST_DIFF_${TEST_DATE}.1.dar
"$TESTDIR"/bin/cleanup.sh --local-backup-dir --cleanup-specific-archive "TEST_DIFF_${TEST_DATE}"
Expand All @@ -70,6 +74,7 @@ if [[ $? != "1" ]]; then
fi

# test 5 fail on date
echo "test 5"
TEST_DATE=2023-04-32
touch "$TESTDIR"/archives/TEST_DIFF_${TEST_DATE}.1.dar
"$TESTDIR"/bin/cleanup.sh --local-backup-dir --cleanup-specific-archive "TEST_DIFF_${TEST_DATE}"
Expand All @@ -79,7 +84,8 @@ if [[ $? != "1" ]]; then
fi

# test 6 fail on date
TEST_DATE=2021-01-01
echo "test 6"
TEST_DATE=2019-12-31
touch "$TESTDIR"/archives/TEST_DIFF_${TEST_DATE}.1.dar
"$TESTDIR"/bin/cleanup.sh --local-backup-dir --cleanup-specific-archive "TEST_DIFF_${TEST_DATE}"
if [[ $? != "1" ]]; then
Expand All @@ -88,6 +94,7 @@ if [[ $? != "1" ]]; then
fi

# test 7 fail on date
echo "test 7"
TEST_DATE=2022-011-01
touch "$TESTDIR"/archives/TEST_DIFF_${TEST_DATE}.1.dar
"$TESTDIR"/bin/cleanup.sh --local-backup-dir --cleanup-specific-archive "TEST_DIFF_${TEST_DATE}"
Expand All @@ -97,6 +104,7 @@ if [[ $? != "1" ]]; then
fi

# test 8 fail on date
echo "test 8"
TEST_DATE=2022-01-1
touch "$TESTDIR"/archives/TEST_DIFF_${TEST_DATE}.1.dar
"$TESTDIR"/bin/cleanup.sh --local-backup-dir --cleanup-specific-archive "TEST_DIFF_${TEST_DATE}"
Expand Down

0 comments on commit e90c62a

Please sign in to comment.