feat: Perform storage eviction and compression in a single pass
This commit is contained in:
parent
af0e32eb68
commit
11d3594121
1 changed files with 47 additions and 28 deletions
|
@ -2,10 +2,22 @@
|
||||||
IFS="
|
IFS="
|
||||||
"
|
"
|
||||||
#Set your parameters here
|
#Set your parameters here
|
||||||
folder=/var/www/friendica
|
#Name of the database
|
||||||
storagefolder=storage
|
db=friendica
|
||||||
|
#User of the database
|
||||||
|
user=root
|
||||||
|
#Folder with the storage files to check
|
||||||
|
storagefolder=/var/www/friendica/storage
|
||||||
|
#The folder storage name, with slashes escaped to work through sed
|
||||||
|
folderescaped=${storagefolder////\\/}
|
||||||
|
|
||||||
loop_1() {
|
loop_1() {
|
||||||
|
ks=$(echo "${p}" | sed -e "s/${folderescaped}//g" -e "s/\///g")
|
||||||
|
e=$(sudo -u "${user}" mariadb "${db}" -N -B -q -e "select \`backend-ref\` from photo where \`backend-ref\` = '${ks}'")
|
||||||
|
#If the file was not found in the database, but still exists in the filesystem, delete it
|
||||||
|
if [[ -z "${e}" && -f "${p}" ]]; then
|
||||||
|
sudo rm -rfv "${p}" #&> /dev/null
|
||||||
|
else
|
||||||
t=$(file "${p}")
|
t=$(file "${p}")
|
||||||
if [[ "${t}" =~ JPEG ]]; then
|
if [[ "${t}" =~ JPEG ]]; then
|
||||||
nice -n 10 jpegoptim -m 76 "${p}" #&> /dev/null
|
nice -n 10 jpegoptim -m 76 "${p}" #&> /dev/null
|
||||||
|
@ -28,9 +40,10 @@ loop_1() {
|
||||||
size_new=$(stat -c%s "${tmppic}" 2>/dev/null || echo 0)
|
size_new=$(stat -c%s "${tmppic}" 2>/dev/null || echo 0)
|
||||||
size_original=$(stat -c%s "${p}" 2>/dev/null || echo 0)
|
size_original=$(stat -c%s "${p}" 2>/dev/null || echo 0)
|
||||||
if [[ "${size_original}" -gt "${size_new}" ]]; then
|
if [[ "${size_original}" -gt "${size_new}" ]]; then
|
||||||
mv "${tmppic}" "${p}" #&> /dev/null
|
mv -v "${tmppic}" "${p}" #&> /dev/null
|
||||||
else
|
else
|
||||||
rm "${tmppic}" #&> /dev/null
|
rm -v "${tmppic}" #&> /dev/null
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
@ -38,7 +51,11 @@ loop_1() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
find "${folder}/${storagefolder}" -depth -mindepth 2 -type f -size +50k -atime -8 -not -iname "index.html" | (
|
#Generate an index to make searches faster
|
||||||
|
echo "Generating photo index..." #&> /dev/null
|
||||||
|
sudo mariadb "${db}" -e "alter table photo add index if not exists backend_index (\`backend-ref\`)" #&> /dev/null
|
||||||
|
echo "Generating list of files..." #&> /dev/null
|
||||||
|
find "${storagefolder}" -depth -mindepth 2 -type f -size +50k -mtime -8 -not -iname "index.html" | (
|
||||||
while read -r p; do
|
while read -r p; do
|
||||||
loop_1 "${p}" &
|
loop_1 "${p}" &
|
||||||
until [[ $(jobs -r -p | wc -l) -lt $(($(getconf _NPROCESSORS_ONLN) / 2)) ]]; do
|
until [[ $(jobs -r -p | wc -l) -lt $(($(getconf _NPROCESSORS_ONLN) / 2)) ]]; do
|
||||||
|
@ -47,3 +64,5 @@ find "${folder}/${storagefolder}" -depth -mindepth 2 -type f -size +50k -atime -
|
||||||
done
|
done
|
||||||
)
|
)
|
||||||
wait
|
wait
|
||||||
|
#Drop the index in the end to save storage
|
||||||
|
sudo mariadb "${db}" -e "alter table photo drop index backend_index" #&> /dev/null
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue