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

scale days to clean by usage percentage #4

Merged
merged 9 commits into from
May 17, 2024
Merged

Conversation

fordmcdonald
Copy link
Member

@fordmcdonald fordmcdonald commented Apr 17, 2024

Incorporate Disk Usage to Improve Cleanup Cronjob

This PR adjusts the timeframe of files to delete according to the current disk usage. It takes the output of df -h /data/xnat/cache and scales the -mtime parameter of find by the percentage of disk space used. If disk usage exceeds 75% for truenas mounted directories, DAYS_TO_CLEAN will be scaled accordingly.

NOTE: df -h /data/xnat/cache will print the disk usage percentage of the entire truenas mount, not only the cache directory.

Bash division will resolve to an integer, so DAYS_TO_CLEAN will always be a valid input to find.

I also added -mindepth 1 to the find commands, so that the top level directory would not be deleted if empty.

counter=0
df -h /$PARENT_PATH/cache | while IFS= read -r line; do
        ((counter++))
        if [ $counter -eq 2 ]; then
                usage=$(echo "$line" | awk '{print $5}')
                usage=${usage%\%}
                if [ $usage -gt 75 ]; then
                    DAYS_TO_CLEAN=$((DAYS_TO_CLEAN / usage))
                fi
        fi
done

@mirestrepo
Copy link

if df -h /data/xnat/cache is the same as df -h /data/xnat/ than why not just use the later one?

also, should we also clean other directories like /prearchive and /build?

Another way is to get usage, remove iterate (if the logic of keeping file count across directories gets messy)

Copy link
Collaborator

@kmilo9999 kmilo9999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Ford,
I left a commend in the bash script in case you want to apply it.

cleanup.sh Outdated Show resolved Hide resolved
@fordmcdonald fordmcdonald linked an issue May 15, 2024 that may be closed by this pull request
@fordmcdonald fordmcdonald requested a review from kmilo9999 May 15, 2024 21:45
@fordmcdonald fordmcdonald merged commit 5e23fad into main May 17, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

XNAT Cleanup Cronjob
3 participants