From 06bbe1cf7d3b47b8053da0fe36fe15d8ce8f49fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Sol=C3=ADs?= Date: Fri, 4 Jul 2025 16:43:10 +0000 Subject: [PATCH] feat: Remove only necessary data, optimize display --- friendica-delete-old-users.sh | 180 ++++++++++++++++++++++------------ 1 file changed, 119 insertions(+), 61 deletions(-) diff --git a/friendica-delete-old-users.sh b/friendica-delete-old-users.sh index 16c3763..e5b810d 100755 --- a/friendica-delete-old-users.sh +++ b/friendica-delete-old-users.sh @@ -13,30 +13,15 @@ else fi intense_optimizations=${1:-"0"} period_amount=${2:-"12"} +starterid=${3:-"0"} db="friendica" period="${period_amount} MONTH" tmpfile=/tmp/friendica-delete-old-users.csv -url=friendica.example.net -avatarfolder=/var/www/friendica/avatar -avatarfolderescaped=${avatarfolder////\\/} +tmpreadlock=/tmp/friendica-delete-old-users.read.tmp +tmpwritelock=/tmp/friendica-delete-old-users.write.tmp loop() { baseurltrimmed=$(echo "${baseurl}" | sed -e "s/http[s]*:\/\///g") - #Find the pictures in the avatar folders and delete them - picturecount=0 - while read -r photo thumb micro; do - #If stored in avatar folder - if grep -v -q "${url}/avatar" <(echo "${photo}"); then - #if [[ -z "${isavatar}" ]] - phototrimmed=$(echo "${photo}" | sed -e "s/https:\/\/${url}\/avatar/${avatarfolderescaped}/g" -e "s/\?ts.*//g") - rm -rfv "${phototrimmed}" - thumbtrimmed=$(echo "${thumb}" | sed -e "s/https:\/\/${url}\/avatar/${avatarfolderescaped}/g" -e "s/\?ts.*//g") - rm -rfv "${thumbtrimmed}" - microtrimmed=$(echo "${micro}" | sed -e "s/https:\/\/${url}\/avatar/${avatarfolderescaped}/g" -e "s/\?ts.*//g") - rm -rfv "${microtrimmed}" - picturecount=1 - fi - done < <("${dbengine}" "${db}" -N -B -q -e "select \`photo\`, \`thumb\`, \`micro\` from \`contact\` where \`id\` = ${id}") postthreadcount=$("${dbengine}" "${db}" -N -B -q -e "create temporary table tmp_post_thread (select \`uri-id\` from \`post-thread\` where \`owner-id\` = ${id} or \`author-id\` = ${id} or \`causer-id\` = ${id}); delete h.* from \`post-thread\` h inner join \`tmp_post_thread\` t where h.\`uri-id\` = t.\`uri-id\`; select row_count();" || echo 0) postthreadusercount=$("${dbengine}" "${db}" -N -B -q -e "create temporary table tmp_post_thread_user (select \`uri-id\` from \`post-thread-user\` where \`owner-id\` = ${id} or \`author-id\` = ${id} or \`causer-id\` = ${id}); delete r.* from \`post-thread-user\` r inner join \`tmp_post_thread_user\` t where r.\`uri-id\` = t.\`uri-id\`; select row_count();" || echo 0) postusercount=$("${dbengine}" "${db}" -N -B -q -e "create temporary table tmp_post_user (select \`id\` from \`post-user\` where \`owner-id\` = ${id} or \`author-id\` = ${id} or \`causer-id\` = ${id}); delete u.* from \`post-user\` u inner join \`tmp_post_user\` t where u.\`id\` = t.\`id\`; select row_count();" || echo 0) @@ -44,30 +29,86 @@ loop() { postcontentcount=$("${dbengine}" "${db}" -N -B -q -e "create temporary table tmp_post (select \`uri-id\` from \`post\` where \`owner-id\` = ${id} or \`author-id\` = ${id} or \`causer-id\` = ${id}); delete p.* from \`post-content\` p inner join \`tmp_post\` t where p.\`uri-id\` = t.\`uri-id\`; select row_count();" || echo 0) postcount=$("${dbengine}" "${db}" -N -B -q -e "create temporary table tmp_post (select \`uri-id\` from \`post\` where \`owner-id\` = ${id} or \`author-id\` = ${id} or \`causer-id\` = ${id}); delete p.* from \`post\` p inner join \`tmp_post\` t where p.\`uri-id\` = t.\`uri-id\`; select row_count();" || echo 0) photocount=$("${dbengine}" "${db}" -N -B -q -e "delete from \`photo\` where \`contact-id\` = ${id}; select row_count();" || echo 0) - contactcount=$("${dbengine}" "${db}" -N -B -q -e "delete from \`contact\` where \`id\` = ${id}; select row_count();" || echo 0) - apcontactcount=$("${dbengine}" "${db}" -N -B -q -e "delete from \`apcontact\` where \`uri-id\` = ${id}; select row_count();" || echo 0) - diasporacontactcount=$("${dbengine}" "${db}" -N -B -q -e "delete from \`diaspora-contact\` where \`uri-id\` = ${id}; select row_count();" || echo 0) - while read -r tmp_counter tmp_picturecount tmp_postthreadcount tmp_postthreadusercount tmp_postusercount tmp_posttagcount tmp_postcontentcount tmp_postcount tmp_photocount tmp_contactcount tmp_apcontactcount tmp_diasporacontactcount; do - if [[ -n "${tmp_counter}" && -n "${tmp_picturecount}" && -n "${tmp_postthreadcount}" && -n "${tmp_postthreadusercount}" && -n "${tmp_postusercount}" && -n "${tmp_posttagcount}" && -n "${tmp_postcontentcount}" && -n "${tmp_postcount}" && -n "${tmp_photocount}" && -n "${tmp_contactcount}" && -n "${tmp_apcontactcount}" && -n "${tmp_diasporacontactcount}" ]]; then - picturecount=$((picturecount + tmp_picturecount)) - postthreadcount=$((postthreadcount + tmp_postthreadcount)) - postthreadusercount=$((postthreadusercount + tmp_postthreadusercount)) - postusercount=$((postusercount + tmp_postusercount)) - posttagcount=$((posttagcount + tmp_posttagcount)) - postcontentcount=$((postcontentcount + tmp_postcontentcount)) - postcount=$((postcount + tmp_postcount)) - photocount=$((photocount + tmp_photocount)) - contactcount=$((contactcount + tmp_contactcount)) - apcontactcount=$((apcontactcount + tmp_apcontactcount)) - diasporacontactcount=$((diasporacontactcount + tmp_diasporacontactcount)) + lastitemid="${id}" + if [[ -n $(type flock) ]]; then + isreadlocked=0 + while [[ "${isreadlocked}" -eq 0 ]]; do + exec 9>"${tmpreadlock}" + if flock -n -e 9; then + isreadlocked=1 + if [[ -f "${tmpfile}" ]]; then + while read -r tmp_counter tmp_lastitemid tmp_postthreadcount tmp_postthreadusercount tmp_postusercount tmp_posttagcount tmp_postcontentcount tmp_postcount tmp_photocount; do + if [[ "${id}" -gt "${lastitemid}" ]]; then + lastitemid="${id}" + fi + if [[ -n "${tmp_postthreadcount}" ]]; then + postthreadcount=$((postthreadcount + tmp_postthreadcount)) + fi + if [[ -n "${tmp_postthreadusercount}" ]]; then + postthreadusercount=$((postthreadusercount + tmp_postthreadusercount)) + fi + if [[ -n "${tmp_postusercount}" ]]; then + postusercount=$((postusercount + tmp_postusercount)) + fi + if [[ -n "${tmp_posttagcount}" ]]; then + posttagcount=$((posttagcount + tmp_posttagcount)) + fi + if [[ -n "${tmp_postcontentcount}" ]]; then + postcontentcount=$((postcontentcount + tmp_postcontentcount)) + fi + if [[ -n "${tmp_postcount}" ]]; then + postcount=$((postcount + tmp_postcount)) + fi + if [[ -n "${tmp_photocount}" ]]; then + photocount=$((photocount + tmp_photocount)) + fi + done <"${tmpfile}" + flock -u 9 + iswritelocked=0 + while [[ "${iswritelocked}" -eq 0 ]]; do + exec 8>"${tmpwritelock}" + if flock -n -e 8; then + iswritelocked=1 + echo "${counter} ${lastitemid} ${postthreadcount} ${postthreadusercount} ${postusercount} ${posttagcount} ${postcontentcount} ${postcount} ${photocount}" >"${tmpfile}" + flock -u 8 + fi + done + fi + fi + done + else + if [[ -f "${tmpfile}" ]]; then + while read -r tmp_counter tmp_lastitemid tmp_postthreadcount tmp_postthreadusercount tmp_postusercount tmp_posttagcount tmp_postcontentcount tmp_postcount tmp_photocount; do + if [[ "${id}" -gt "${lastitemid}" ]]; then + lastitemid="${id}" + fi + if [[ -n "${tmp_postthreadcount}" ]]; then + postthreadcount=$((postthreadcount + tmp_postthreadcount)) + fi + if [[ -n "${tmp_postthreadusercount}" ]]; then + postthreadusercount=$((postthreadusercount + tmp_postthreadusercount)) + fi + if [[ -n "${tmp_postusercount}" ]]; then + postusercount=$((postusercount + tmp_postusercount)) + fi + if [[ -n "${tmp_posttagcount}" ]]; then + posttagcount=$((posttagcount + tmp_posttagcount)) + fi + if [[ -n "${tmp_postcontentcount}" ]]; then + postcontentcount=$((postcontentcount + tmp_postcontentcount)) + fi + if [[ -n "${tmp_postcount}" ]]; then + postcount=$((postcount + tmp_postcount)) + fi + if [[ -n "${tmp_photocount}" ]]; then + photocount=$((photocount + tmp_photocount)) + fi + done <"${tmpfile}" + echo "${counter} ${lastitemid} ${postthreadcount} ${postthreadusercount} ${postusercount} ${posttagcount} ${postcontentcount} ${postcount} ${photocount}" >"${tmpfile}" fi - done <"${tmpfile}" - if [[ -n "${counter}" && -n "${picturecount}" && -n "${postthreadcount}" && -n "${postthreadusercount}" && -n "${postusercount}" && -n "${posttagcount}" && -n "${postcontentcount}" && -n "${postcount}" && -n "${photocount}" && -n "${contactcount}" && -n "${apcontactcount}" && -n "${diasporacontactcount}" ]]; then - echo "${counter}" "${picturecount}" "${postthreadcount} ${postthreadusercount} ${postusercount} ${posttagcount} ${postcontentcount} ${postcount} ${photocount} ${contactcount} ${apcontactcount} ${diasporacontactcount}" >"${tmpfile}" fi if [[ -n "${lastitem}" && "${#lastitem}" -gt 9 ]]; then response_left=$(printf "%s %s %s %s@%s " "${counter}" "${id}" "${lastitem::-9}" "${nick}" "${baseurltrimmed}") - response=$(printf "%spicture:%s " "${response}" "${picturecount}") response=$(printf "%spost-thread:%s " "${response}" "${postthreadcount}") response=$(printf "%spost-thread-user:%s " "${response}" "${postthreadusercount}") response=$(printf "%spost-user:%s " "${response}" "${postusercount}") @@ -75,9 +116,6 @@ loop() { response=$(printf "%spost-content:%s " "${response}" "${postcontentcount}") response=$(printf "%spost:%s " "${response}" "${postcount}") response=$(printf "%sphoto:%s " "${response}" "${photocount}") - response=$(printf "%scontact:%s " "${response}" "${contactcount}") - response=$(printf "%sapcontact:%s " "${response}" "${apcontactcount}") - response=$(printf "%sdiaspora-contact:%s " "${response}" "${diasporacontactcount}") #Previous line clearance #Measure length of string, blank only the excess #Since this string is panned to both sides, we will need to account for two lengths @@ -108,8 +146,17 @@ loop() { #Check if our dependencies are installed if [[ -n $(type curl) && -n "${dbengine}" && -n $(type "${dbengine}") && -n $(type date) ]]; then date + if [[ -f "${tmpfile}" ]]; then + rm -rf "${tmpfile}" + fi + if [[ -f "${tmpreadlock}" ]]; then + rm -rf "${tmpreadlock}" + fi + if [[ -f "${tmpwritelock}" ]]; then + rm -rf "${tmpwritelock}" + fi touch "${tmpfile}" - echo "0 0 0 0 0 0 0 0 0 0 0 0" >"${tmpfile}" + echo "0 0 0 0 0 0 0 0 0" >"${tmpfile}" if [[ "${intense_optimizations}" -gt 0 ]]; then "${dbengine}" "${db}" -v -e "\ alter table \`contact\` add index if not exists \`tmp_contact_baseurl_addr\` (baseurl, addr); \ @@ -118,19 +165,18 @@ if [[ -n $(type curl) && -n "${dbengine}" && -n $(type "${dbengine}") && -n $(ty alter table \`post-user\` add index if not exists \`tmp_post_user_id\` (\`owner-id\`, \`author-id\`, \`causer-id\`); \ alter table \`post\` add index if not exists \`tmp_post_id\` (\`owner-id\`, \`author-id\`, \`causer-id\`); \ alter table \`photo\` add index if not exists \`tmp_photo_id\` (\`contact-id\`); \ - select count(\`id\`) from contact c where \ - c.\`addr\` not in (select \`addr\` from \`contact\` where \`id\` in (select \`cid\` from \`user-contact\`)) and \ - c.\`addr\` not in (select \`addr\` from \`contact\` where \`id\` in (select \`uid\` from \`user\`)) and \ - c.\`addr\` not in (select \`addr\` from \`contact\` where \`id\` in (select \`contact-id\` from \`group_member\`)) and \ - c.\`contact-type\` != 4 and not pending and \`last-item\` < CURDATE() - INTERVAL ${period} and \ - c.\`nick\` not in ('threads.sys', 'relay', 'friendica', 'sharkey', 'bot', 'catodon', \ - 'flipboard', 'lemmy', 'mitra', 'mstdn_bot', 'peertube', 'piefed', 'admin'); " fi counter=0 was_empty=0 while [[ "${was_empty}" -eq 0 ]]; do current_counter=0 + currentid="${starterid}" + while read -r tmp_counter tmp_lastitemid tmp_postthreadcount tmp_postthreadusercount tmp_postusercount tmp_posttagcount tmp_postcontentcount tmp_postcount tmp_photocount; do + if [[ -n "${tmp_counter}" && -n "${tmp_lastitemid}" && "${currentid}" -lt "${tmp_lastitemid}" ]]; then + currentid="${tmp_lastitemid}" + fi + done <"${tmpfile}" while read -r id nick baseurl lastitem; do counter=$((counter + 1)) current_counter=$((current_counter + 1)) @@ -145,9 +191,8 @@ if [[ -n $(type curl) && -n "${dbengine}" && -n $(type "${dbengine}") && -n $(ty c.\`addr\` not in (select \`addr\` from \`contact\` where \`id\` in (select \`contact-id\` from \`group_member\`)) and \ c.\`contact-type\` != 4 and not pending and \`last-item\` < CURDATE() - INTERVAL ${period} and \ c.\`nick\` not in ('threads.sys', 'relay', 'friendica', 'sharkey', 'bot', 'catodon', \ - 'flipboard', 'lemmy', 'mitra', 'mstdn_bot', 'peertube', 'piefed', 'admin') \ - limit 1000") - #not regexp_replace(c.\`nick\`, '[[:punct:]].*', '') like regexp_replace( replace(c.\`baseurl\`, 'https://', ''), '[[:punct:]].+', '') + 'flipboard', 'lemmy', 'mitra', 'mstdn_bot', 'peertube', 'piefed', 'admin') and \ + c.\`id\` > ${currentid} limit 1000") wait if [[ "${current_counter}" -eq 0 ]]; then was_empty=1 @@ -161,17 +206,30 @@ if [[ -n $(type curl) && -n "${dbengine}" && -n $(type "${dbengine}") && -n $(ty alter table \`post-user\` auto_increment = 1; \ alter table \`post-tag\` auto_increment = 1; \ alter table \`post\` auto_increment = 1; \ - alter table \`photo\` auto_increment = 1; \ - alter table \`contact\` auto_increment = 1; \ - alter table \`contact\` drop index \`tmp_contact_baseurl_addr\`; \ - alter table \`post-thread\` drop index \`tmp_post_thread_id\`; \ - alter table \`post-thread-user\` drop index \`tmp_post_thread_user_id\`; \ - alter table \`post-user\` drop index \`tmp_post_user_id\`; \ - alter table \`post\` drop index \`tmp_post_id\`; \ - alter table \`photo\` drop index\`tmp_photo_id\`; \ " "${dboptimizeengine}" "${db}" fi - rm -rf "${tmpfile}" + "${dbengine}" "${db}" -v -e "\ + alter table \`contact\` drop index \`tmp_contact_baseurl_addr\`; \ + alter table \`post-thread\` drop index \`tmp_post_thread_id\`; \ + alter table \`post-thread-user\` drop index \`tmp_post_thread_user_id\`; \ + alter table \`post-user\` drop index \`tmp_post_user_id\`; \ + alter table \`post\` drop index \`tmp_post_id\`; \ + alter table \`photo\` drop index \`tmp_photo_id\`; \ + " + "${dboptimizeengine}" "${db}" + if [[ -n $(type flock) ]]; then + flock -u 9 + flock -u 8 + fi + if [[ -f "${tmpfile}" ]]; then + rm -rf "${tmpfile}" + fi + if [[ -f "${tmpreadlock}" ]]; then + rm -rf "${tmpreadlock}" + fi + if [[ -f "${tmpwritelock}" ]]; then + rm -rf "${tmpwritelock}" + fi date fi