ops/utils/prune_data.sh
2024-07-19 11:04:30 -04:00

58 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
Src=${1}
Days=${2}
if [ -z "$Days" ] || [ -z "$Src" ]; then
echo "Usage: $0 <source_dir> <num_days>"
exit 1
fi
# Add / if Src does not have it (symlinks)
if [ "${Src: -1}" != "/" ]; then
Src="${Src}/"
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_files} == 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 ls -l $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