refactor(nextcloud): improve version and trash cleanup script* - add strict bash error handling - use trap to always restart apache- fix unsafe array expansion - improve file deletion handling - suppoot filenames containing spaces - replace service with systemctl - simpmify trashbin cleanup logic - improve user prompts and output - add proper root privilege verification - reduce code duplication
85 lines
1.5 KiB
Bash
Executable File
85 lines
1.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
set -Eeuo pipefail
|
|
|
|
USERS=("lucas" "multimedia" "mimi")
|
|
VERSION_DIRS=("Music" "Pictures" "Stuff" "SW")
|
|
|
|
check_root() {
|
|
[[ $EUID -eq 0 ]] || {
|
|
echo "Run as root."
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
cleanup() {
|
|
echo
|
|
echo "Starting Apache..."
|
|
systemctl start apache2
|
|
}
|
|
|
|
clean_versions() {
|
|
|
|
for dir in "${VERSION_DIRS[@]}"; do
|
|
|
|
TARGET="/nextcloud/nextcloud-data/lucas/files_versions/${dir}"
|
|
|
|
if [[ ! -d "$TARGET" ]]; then
|
|
echo "$dir not present"
|
|
continue
|
|
fi
|
|
|
|
read -rp "Delete $TARGET ? (y/N): " choice
|
|
|
|
if [[ "$choice" =~ ^[Yy]$ ]]; then
|
|
rm -rf -- "$TARGET"
|
|
echo "Deleted"
|
|
else
|
|
echo "Skipped"
|
|
fi
|
|
done
|
|
}
|
|
|
|
clean_trashbin() {
|
|
|
|
local found=0
|
|
|
|
for user in "${USERS[@]}"; do
|
|
|
|
TRASH="/nextcloud/nextcloud-data/${user}/files_trashbin/files"
|
|
|
|
[[ -d "$TRASH" ]] || continue
|
|
|
|
while IFS= read -r file; do
|
|
|
|
found=1
|
|
|
|
echo "Deleting $(basename "$file")"
|
|
|
|
rm -f -- "$file"
|
|
|
|
done < <(
|
|
find "$TRASH" \
|
|
-type f \
|
|
\( \
|
|
-iname "*.avi" -o \
|
|
-iname "*.mkv" -o \
|
|
-iname "*.mp4" -o \
|
|
-iname "*.m4v" -o \
|
|
-iname "*.srt" \
|
|
\)
|
|
* )
|
|
done
|
|
|
|
(( found == 0 ))*&& echo "No media trash found."
|
|
}
|
|
*check_root
|
|
|
|
trap cleanup EXIT
|
|
|
|
sys*emctl stop apache2
|
|
|
|
clean_versions*clean_trashbin
|
|
|
|
echo "Cleanup fini*hed."
|