ops/utils/prune_data.sh
2024-07-19 10:45:46 -04:00

58 lines
1.0 KiB
Bash
Executable File

#!/bin/bash
Src=${1}
Days=${2}
if [ "${Src}" == "" ]
then
echo "Usage: $0 <source_dir> [<num_days>]"
exit 1
fi
if [ "${Days}" == "" ]
then
Days=30
fi
declare -A Settings=()
Settings[Src]=${Src}
Settings[PruneDate]=$(date -d "${Days} days ago" '+%Y-%m-%d')
src=${Settings[Src]}
prune_date=${Settings[PruneDate]}
echo "Finding files older than ${prune_date}..."
Cmd="find ${src} -type f ! -newermt \"${prune_date}\""
echo ${Cmd}
files=($(eval ${Cmd}))
total_files= ${#files[*]}
if [[ ${total} == 0 ]]
then
echo "No files found to be pruned. Bye..."
exit 0
fi
echo Before Pruning....
duf ${src}
echo "The following files will be removed:"
echo "===================================="
for f in $files ; do echo $f; done
echo "===================================="
echo "Total files to be pruned: ${total_files}"
Cmd="${Cmd} -delete"
echo ${Cmd}
eval ${Cmd}
echo "Removing empty directories..."
Cmd="find ${src} -type d -empty -print -delete"
echo ${Cmd}
eval ${Cmd}
echo After Pruning....
duf ${src}
echo $0 Done