2025-02-06 14:36:19 +00:00
|
|
|
#!/bin/bash
|
2025-02-12 21:27:45 +00:00
|
|
|
#Check for mariadb vs. mysql
|
|
|
|
dbengine=""
|
2025-03-11 09:58:00 -06:00
|
|
|
dboptimizeengine=""
|
2025-02-12 21:27:45 +00:00
|
|
|
if [[ -n $(type mariadb) ]]; then
|
|
|
|
dbengine="mariadb"
|
2025-03-11 09:58:00 -06:00
|
|
|
dboptimizeengine="mariadb-optimize"
|
2025-02-12 21:27:45 +00:00
|
|
|
elif [[ -n $(type mysql) ]]; then
|
|
|
|
dbengine="mysql"
|
2025-03-11 09:58:00 -06:00
|
|
|
dboptimizeengine="mysqloptimize"
|
2025-02-12 21:27:45 +00:00
|
|
|
else
|
|
|
|
exit
|
|
|
|
fi
|
2025-03-05 09:21:37 -06:00
|
|
|
intense_optimizations=${1:-"0"}
|
2025-03-11 09:58:00 -06:00
|
|
|
period_amount=${2:-"12"}
|
2025-07-04 16:43:10 +00:00
|
|
|
starterid=${3:-"0"}
|
2025-02-06 14:36:19 +00:00
|
|
|
db="friendica"
|
2025-03-11 09:58:00 -06:00
|
|
|
period="${period_amount} MONTH"
|
2025-03-05 09:21:37 -06:00
|
|
|
tmpfile=/tmp/friendica-delete-old-users.csv
|
2025-07-04 16:43:10 +00:00
|
|
|
tmpreadlock=/tmp/friendica-delete-old-users.read.tmp
|
|
|
|
tmpwritelock=/tmp/friendica-delete-old-users.write.tmp
|
2025-02-06 14:36:19 +00:00
|
|
|
|
|
|
|
loop() {
|
|
|
|
baseurltrimmed=$(echo "${baseurl}" | sed -e "s/http[s]*:\/\///g")
|
2025-03-05 09:21:37 -06:00
|
|
|
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)
|
|
|
|
posttagcount=$("${dbengine}" "${db}" -N -B -q -e "delete from \`post-tag\` where cid = ${id}; select row_count();" || echo 0)
|
|
|
|
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)
|
2025-07-04 16:43:10 +00:00
|
|
|
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}"
|
2025-03-07 16:51:00 -06:00
|
|
|
fi
|
2025-03-11 09:58:00 -06:00
|
|
|
fi
|
|
|
|
if [[ -n "${lastitem}" && "${#lastitem}" -gt 9 ]]; then
|
|
|
|
response_left=$(printf "%s %s %s %s@%s " "${counter}" "${id}" "${lastitem::-9}" "${nick}" "${baseurltrimmed}")
|
|
|
|
response=$(printf "%spost-thread:%s " "${response}" "${postthreadcount}")
|
|
|
|
response=$(printf "%spost-thread-user:%s " "${response}" "${postthreadusercount}")
|
|
|
|
response=$(printf "%spost-user:%s " "${response}" "${postusercount}")
|
|
|
|
response=$(printf "%spost-tag:%s " "${response}" "${posttagcount}")
|
|
|
|
response=$(printf "%spost-content:%s " "${response}" "${postcontentcount}")
|
|
|
|
response=$(printf "%spost:%s " "${response}" "${postcount}")
|
|
|
|
response=$(printf "%sphoto:%s " "${response}" "${photocount}")
|
|
|
|
#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
|
|
|
|
final_string_length_left="${#response_left}"
|
|
|
|
final_string_length_right="${#response}"
|
|
|
|
final_string_length=$((final_string_length_left + final_string_length_right))
|
|
|
|
#The string that will be used to insert the blanks
|
|
|
|
blank_string=""
|
|
|
|
columns_length="${COLUMNS}"
|
|
|
|
#Account for the case where the string is more than a terminal line long
|
|
|
|
while [[ "${final_string_length}" -gt "${columns_length}" ]]; do
|
|
|
|
columns_length=$((columns_length + COLUMNS))
|
|
|
|
done
|
|
|
|
blank_string_length=$((columns_length - final_string_length))
|
|
|
|
#Add enough blank spaces to fill the rest of the line
|
|
|
|
for ((count = 0; count < "${blank_string_length}"; count++)); do
|
|
|
|
blank_string=$(printf "%s " "${blank_string}")
|
|
|
|
done
|
|
|
|
#Add backspaces to align the next output
|
|
|
|
for ((count = 0; count < $((final_string_length + blank_string_length)); count++)); do
|
|
|
|
response_left=$(printf "\b%s" "${response_left}")
|
|
|
|
done
|
|
|
|
response=$(printf "%s%s%s" "${response_left}" "${blank_string}" "${response}")
|
|
|
|
printf "%s\r" "${response}"
|
|
|
|
fi
|
2025-02-06 14:36:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#Check if our dependencies are installed
|
|
|
|
if [[ -n $(type curl) && -n "${dbengine}" && -n $(type "${dbengine}") && -n $(type date) ]]; then
|
|
|
|
date
|
2025-07-04 16:43:10 +00:00
|
|
|
if [[ -f "${tmpfile}" ]]; then
|
|
|
|
rm -rf "${tmpfile}"
|
|
|
|
fi
|
|
|
|
if [[ -f "${tmpreadlock}" ]]; then
|
|
|
|
rm -rf "${tmpreadlock}"
|
|
|
|
fi
|
|
|
|
if [[ -f "${tmpwritelock}" ]]; then
|
|
|
|
rm -rf "${tmpwritelock}"
|
|
|
|
fi
|
2025-03-05 09:21:37 -06:00
|
|
|
touch "${tmpfile}"
|
2025-07-04 16:43:10 +00:00
|
|
|
echo "0 0 0 0 0 0 0 0 0" >"${tmpfile}"
|
2025-03-05 09:21:37 -06:00
|
|
|
if [[ "${intense_optimizations}" -gt 0 ]]; then
|
2025-03-11 09:58:00 -06:00
|
|
|
"${dbengine}" "${db}" -v -e "\
|
2025-06-24 13:40:30 -06:00
|
|
|
alter table \`contact\` add index if not exists \`tmp_contact_baseurl_addr\` (baseurl, addr); \
|
2025-03-11 09:58:00 -06:00
|
|
|
alter table \`post-thread\` add index if not exists \`tmp_post_thread_id\` (\`owner-id\`, \`author-id\`, \`causer-id\`); \
|
|
|
|
alter table \`post-thread-user\` add index if not exists \`tmp_post_thread_user_id\` (\`owner-id\`, \`author-id\`, \`causer-id\`); \
|
|
|
|
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\`); \
|
|
|
|
"
|
2025-03-05 09:21:37 -06:00
|
|
|
fi
|
2025-03-07 16:51:00 -06:00
|
|
|
counter=0
|
|
|
|
was_empty=0
|
|
|
|
while [[ "${was_empty}" -eq 0 ]]; do
|
|
|
|
current_counter=0
|
2025-07-04 16:43:10 +00:00
|
|
|
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}"
|
2025-03-07 16:51:00 -06:00
|
|
|
while read -r id nick baseurl lastitem; do
|
|
|
|
counter=$((counter + 1))
|
|
|
|
current_counter=$((current_counter + 1))
|
|
|
|
loop "${id}" "${nick}" "${baseurl}" "${lastitem}" "${counter}" &
|
2025-03-11 09:58:00 -06:00
|
|
|
if [[ $(jobs -r -p | wc -l) -ge $(($(getconf _NPROCESSORS_ONLN) * 2)) ]]; then
|
2025-03-07 16:51:00 -06:00
|
|
|
wait -n
|
|
|
|
fi
|
|
|
|
done < <("${dbengine}" "${db}" -N -B -q -e \
|
|
|
|
"select \`id\`, \`nick\`, \`baseurl\`, \`last-item\` from contact c where \
|
2025-06-24 13:40:30 -06:00
|
|
|
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 \
|
2025-03-11 09:58:00 -06:00
|
|
|
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', \
|
2025-07-04 16:43:10 +00:00
|
|
|
'flipboard', 'lemmy', 'mitra', 'mstdn_bot', 'peertube', 'piefed', 'admin') and \
|
|
|
|
c.\`id\` > ${currentid} limit 1000")
|
2025-03-07 16:51:00 -06:00
|
|
|
wait
|
|
|
|
if [[ "${current_counter}" -eq 0 ]]; then
|
|
|
|
was_empty=1
|
2025-03-07 12:35:48 -06:00
|
|
|
fi
|
2025-03-07 16:51:00 -06:00
|
|
|
done
|
2025-03-06 09:31:04 -06:00
|
|
|
printf "\n\r"
|
2025-03-05 09:21:37 -06:00
|
|
|
if [[ "${intense_optimizations}" -gt 0 ]]; then
|
2025-03-11 09:58:00 -06:00
|
|
|
"${dbengine}" "${db}" -v -e "\
|
|
|
|
alter table \`post-thread\` auto_increment = 1; \
|
2025-03-07 12:35:48 -06:00
|
|
|
alter table \`post-thread-user\` auto_increment = 1; \
|
|
|
|
alter table \`post-user\` auto_increment = 1; \
|
|
|
|
alter table \`post-tag\` auto_increment = 1; \
|
|
|
|
alter table \`post\` auto_increment = 1; \
|
2025-03-11 09:58:00 -06:00
|
|
|
"
|
|
|
|
"${dboptimizeengine}" "${db}"
|
2025-03-05 09:21:37 -06:00
|
|
|
fi
|
2025-07-04 16:43:10 +00:00
|
|
|
"${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
|
2025-02-06 14:36:19 +00:00
|
|
|
date
|
|
|
|
fi
|