refactor(nextcloud): improve version and trash cleanup script
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
This commit is contained in:
1 parent
3168e3928b
commit
0d8ae83626
2 files changed
+84
-49
No files matched your search
@@ -1,49 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
names_container=("lucas" "multimedia" "mimi")
|
|
||||||
clean_versions() {
|
|
||||||
VERSION_DIRS=("Music" "Pictures" "Stuff" "SW")
|
|
||||||
for dir in ${VERSION_DIRS[@]}; do
|
|
||||||
if [ -d /nextcloud/nextcloud-data/lucas/files_versions/${dir} ]; then
|
|
||||||
echo "Delete /nextcloud/nextcloud-data/lucas/files_versions/${dir}? (y/*)"
|
|
||||||
read -n 4 choice
|
|
||||||
if [[ "${choice}" == "y" ]]; then
|
|
||||||
echo "Delete"
|
|
||||||
rm -dr /nextcloud/nextcloud-data/lucas/files_versions/${dir}
|
|
||||||
else
|
|
||||||
echo "Skip"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
echo "${dir} not present - skipping"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
clean_trashbin() {
|
|
||||||
container=("avi" "mkv" "mp4" "m4v" "srt")
|
|
||||||
IFS=$'\n'; set -f
|
|
||||||
find /nextcloud/nextcloud-data/multimedia/files_trashbin/files/ -name *\.* -delete
|
|
||||||
for name in ${names_container[@]}; do
|
|
||||||
for cont in ${container[@]}; do
|
|
||||||
FILE_ARR=("${FILE_ARR[@]}" $(find /nextcloud/nextcloud-data/${name}/files_trashbin/files/ -type f -name "*${cont}*"))
|
|
||||||
done
|
|
||||||
done
|
|
||||||
if [ ${#FILE_ARR[@]} -eq 0 ]; then
|
|
||||||
echo "No Media Containers Present"
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
for file in ${FILE_ARR[@]}; do
|
|
||||||
echo "Delete $( echo $file | rev | cut -f1 -d'/' | rev)"
|
|
||||||
rm "${file}"
|
|
||||||
done
|
|
||||||
}
|
|
||||||
check_root(){
|
|
||||||
if [ $(id -u) -ne 0 ]; then
|
|
||||||
echo "Execute as root: $(id)"
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
check_root
|
|
||||||
service apache2 stop
|
|
||||||
clean_versions
|
|
||||||
clean_trashbin
|
|
||||||
service apache2 start
|
|
||||||
+84
@@ -0,0 +1,84 @@
|
|||||||
|
#!/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."
|
||||||
Reference in new issue
Block a user