Direkt zum Hauptinhalt

überflüssige Bilder löschen

Nachfolgendes Skript löscht Bilder, die nicht mehr referenziert sind:

Skript

/usr/local/bin/bs-cleanup-img.sh:

# !/bin/bash
# set -x
#
# Cleanup images and drawings
# Also delete images that are only used in old revisions
# depending on amount of images cleanup will take several minutes
#
# Version 2024-02-29

APP_FOLDER=/var/www/bookstack
IMG_FOLDER=${APP_FOLDER}/public/uploads/images
LOG=/var/log/`echo $(basename "$0") | rev | cut -d. -f2- | rev`.log

echo "$(date '+%Y-%m-%d %H:%M:%S') === START ===" >> $LOG
echo "working on ${APP_FOLDER}" >> $LOG
DU_BEFORE=`du -s --block-size=1 $IMG_FOLDER | cut -f1`
DU_BEFORE_K=$(($DU_BEFORE/1024))
FILES_BEFORE=`ls -lqR ${IMG_FOLDER} | grep -iE ".*\.(jpg|jpeg|png)$" | wc -l`

php ${APP_FOLDER}/artisan bookstack:cleanup-images -f -n -v >> $LOG

DU_AFTER=`du -s --block-size=1 $IMG_FOLDER | cut -f1`
DU_AFTER_K=$(($DU_AFTER/1024))
FILES_AFTER=`ls -lqR ${IMG_FOLDER} | grep -iE ".*\.(jpg|jpeg|png)$" | wc -l`

if [ $DU_AFTER -lt $DU_BEFORE ]; then
  DELETED_K=$((DU_BEFORE_K-$DU_AFTER_K))
  FILES_DELETED=$((FILES_BEFORE-$FILES_AFTER))
  echo "${FILES_BEFORE} - ${FILES_AFTER} = ${FILES_DELETED} deleted" >> $LOG
  echo "${DU_BEFORE_K} - ${DU_AFTER_K} = ${DELETED_K}k deleted" >> $LOG
else
  echo "nothing to do, ${FILES_AFTER} files, ${DU_AFTER_K}k" >> $LOG
fi
echo "$(date '+%Y-%m-%d %H:%M:%S') === STOP ===" >> $LOG

Auszug aus dem Log:

2024-02-29 16:31:19 === START ===
working on /var/www/bookstack
This operation is destructive and is not guaranteed to be fully accurate.
Ensure you have a backup of your images.

Image(s) to delete:
/uploads/images/gallery/2023-07/img-20220630-132119.jpg
/uploads/images/gallery/2023-08/PGBimage.png
[..]
/uploads/images/gallery/2024-01/vRZgrafik.png
/uploads/images/drawio/2024-02/drawing-7-1706817605.png
/uploads/images/drawio/2024-02/drawing-21-1708611127.png
325 image(s) deleted
8934 - 8000 = 934 files deleted
315784k - 256552k = 59232k deleted
2024-02-29 16:32:37 === STOP ===
2024-02-29 17:09:58 === START ===
working on /var/www/bookstack
This operation is destructive and is not guaranteed to be fully accurate.
Ensure you have a backup of your images.

0 image(s) deleted
nothing to do, 8000 files, 256552k
2024-02-29 17:24:18 === STOP ===

Cronjob

Skript sonntags 3:15 Uhr ausführen:

15 3 * * 7 /usr/local/bin/bs-cleanup-img.sh

Logrotate

/etc/logrotate.d/bs-cleanup-img

/var/log/bs-cleanup-img.log
{
        rotate 4
        weekly
        missingok
        notifempty
        compress
        delaycompress
        sharedscripts
}

Service neustarten, um Konfig zu aktivieren

systemctl restart rsyslog.service