mirror of
https://codeberg.org/Windfluechter/cleanup-mastodon-users.sh.git
synced 2025-06-16 09:15:36 +00:00
Some more changes, added subroutine to create config
This commit is contained in:
parent
246212c8e1
commit
c3bd7d2d5f
@ -2,9 +2,8 @@
|
|||||||
|
|
||||||
set -f
|
set -f
|
||||||
|
|
||||||
if [ -f ${HOME}/.cleanup-mastodon-users.conf ]; then
|
# create a default config when started with --init
|
||||||
. ${HOME}/.cleanup-mastodon-users.conf
|
initConfig() {
|
||||||
else
|
|
||||||
(
|
(
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
LOWERLIMIT="6 months" # all accounts below 6 months inactivity are safe, start notifiying them when >6 months
|
LOWERLIMIT="6 months" # all accounts below 6 months inactivity are safe, start notifiying them when >6 months
|
||||||
@ -19,25 +18,59 @@ SQLPROTECTEDUSERS="'user', 'foo', 'bar', 'baz'"
|
|||||||
LIMIT_DELETE="2"
|
LIMIT_DELETE="2"
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
) > ${HOME}/.cleanup-mastodon-users.conf
|
) > "${HOME}"/.cleanup-mastodon-users.conf
|
||||||
sleep 1
|
echo "✅ - Configuration file created. You can now edit "${HOME}"/.cleanup-mastodon-users.conf"
|
||||||
. ${HOME}/.cleanup-mastodon-users.conf
|
}
|
||||||
fi
|
|
||||||
|
|
||||||
#LOWERLIMIT="${LOWERLIMIT:-'6 months'}" # all accounts below 6 months inactivity are safe, start notifiying them when >6 months
|
|
||||||
#UPPERLIMIT="${UPPERLIMIT:-'7 months'}" # inactive accounts older than 7 months will be deactivated
|
case $1 in
|
||||||
#DELDELIMIT="${DELDELIMIT:-'1 year'}" # all accounts not used within a year will get deleted
|
"--init")
|
||||||
#LIVE_PATH="${LIVE_PATH:-${HOME}/live/}" # Path to live data from mastodon
|
initConfig
|
||||||
#TOOTCTL="${TOOTCTL:-${HOME}/bin/tootctl}"
|
exit 0
|
||||||
#SITE="${SITE:-$LOCAL_DOMAIN}"
|
;;
|
||||||
#SITEADMIN="${SITEADMIN:-root@localhost}"
|
"--dry-run")
|
||||||
#LIMIT_DELETE="${LIMIT_DELETE:-2}"
|
mode="dryrun"
|
||||||
|
;;
|
||||||
|
"--cron")
|
||||||
|
mode="cron"
|
||||||
|
;;
|
||||||
|
"--dowhatimean")
|
||||||
|
mode="hotrun"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Usage: "
|
||||||
|
echo "--init \t \t: create $0/.cleanup-mastodon-users.conf config file."
|
||||||
|
echo " --dry-run \t: make a dry-run, no deletion will be done, no mails are sent."
|
||||||
|
echo " --cron \t: delete deactivated users in a regularly cron run step by step to avoid mass flooding."
|
||||||
|
echo " --dowhatimean \t: add this option if you really want to delete users."
|
||||||
|
echo ""
|
||||||
|
if [ ! -f "${HOME}"/.cleanup-mastodon-users.conf ]; then
|
||||||
|
echo "❌ - No configuration file found!"
|
||||||
|
echo "Please start with \"$0 --init\" to create config files"
|
||||||
|
echo "and edit the file \"${HOME}/.cleanup-mastodon-users.conf\" to your needs."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
|
||||||
|
. "${HOME}"/.cleanup-mastodon-users.conf
|
||||||
|
|
||||||
|
LOWERLIMIT="${LOWERLIMIT:-'6 months'}" # all accounts below 6 months inactivity are safe, start notifiying them when >6 months
|
||||||
|
UPPERLIMIT="${UPPERLIMIT:-'7 months'}" # inactive accounts older than 7 months will be deactivated
|
||||||
|
DELDELIMIT="${DELDELIMIT:-'1 year'}" # all accounts not used within a year will get deleted
|
||||||
|
LIVE_PATH="${LIVE_PATH:-${HOME}/live/}" # Path to live data from mastodon
|
||||||
|
TOOTCTL="${TOOTCTL:-${HOME}/bin/tootctl}"
|
||||||
|
SITE="${SITE:-$LOCAL_DOMAIN}"
|
||||||
|
SITEADMIN="${SITEADMIN:-root@localhost}"
|
||||||
|
LIMIT_DELETE="${LIMIT_DELETE:-2}"
|
||||||
|
|
||||||
|
|
||||||
cd "${LIVE_PATH}" || exit
|
cd "${LIVE_PATH}" || exit
|
||||||
|
|
||||||
# shellcheck source=/dev/null
|
# shellcheck source=/dev/null
|
||||||
source "${LIVE_PATH}/.env.production"
|
. "${LIVE_PATH}/.env.production" 2>/dev/null
|
||||||
|
|
||||||
run_tootctl() {
|
run_tootctl() {
|
||||||
"${TOOTCTL}" "$@"
|
"${TOOTCTL}" "$@"
|
||||||
@ -50,29 +83,11 @@ num_deleted=0
|
|||||||
|
|
||||||
STARTDATE=$(date +"%d.%m.%Y %H:%M:%S")
|
STARTDATE=$(date +"%d.%m.%Y %H:%M:%S")
|
||||||
|
|
||||||
case $1 in
|
|
||||||
"--dry-run")
|
|
||||||
mode="dryrun"
|
|
||||||
;;
|
|
||||||
"--cron")
|
|
||||||
mode="cron"
|
|
||||||
;;
|
|
||||||
"--dowhatimean")
|
|
||||||
mode="hotrun"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
echo "Usage: "
|
|
||||||
echo " --dry-run \t: make a dry-run, no deletion will be done, no mails are sent."
|
|
||||||
echo " --cron \t: delete deactivated users in a regularly cron run step by step to avoid mass flooding."
|
|
||||||
echo " --dowhatimean \t: add this option if you really want to delete users."
|
|
||||||
exit 0
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
# make a list to be used for grep -E
|
# make a list to be used for grep -E
|
||||||
PROTECTED=$(echo "$PROTECTEDUSERS" | sed 's/\"//g' | sed 's/\ /\\\|/g')
|
PROTECTED=$(echo "$PROTECTEDUSERS" | sed 's/\"//g' | sed 's/\ /\\\|/g')
|
||||||
#echo $PROTECTED
|
#echo $PROTECTED
|
||||||
|
|
||||||
|
|
||||||
# notify the user that s/he needs to re-login after 6 months to prevent account deletion
|
# notify the user that s/he needs to re-login after 6 months to prevent account deletion
|
||||||
notifyUser() {
|
notifyUser() {
|
||||||
(
|
(
|
||||||
@ -139,10 +154,13 @@ EOF
|
|||||||
# select a.id, username, email, current_sign_in_at from accounts a, users u where domain is null and a.id=u.account_id and current_sign_in_at <'2019-01-01'
|
# select a.id, username, email, current_sign_in_at from accounts a, users u where domain is null and a.id=u.account_id and current_sign_in_at <'2019-01-01'
|
||||||
|
|
||||||
case ${mode} in
|
case ${mode} in
|
||||||
|
"init")
|
||||||
|
initConfig
|
||||||
|
;;
|
||||||
"cron")
|
"cron")
|
||||||
# get the total number of deactivated accounts
|
# get the total number of deactivated accounts
|
||||||
# the intention is that you can better judge how often you need to invoke the cron option
|
# the intention is that you can better judge how often you need to invoke the cron option
|
||||||
# or by increasing the limit_delete variable
|
# or by increasing the LIMIT_DELETE variable
|
||||||
# the backlog queue shouldn't pile up but also not run empty to fast to reduce the load
|
# the backlog queue shouldn't pile up but also not run empty to fast to reduce the load
|
||||||
num_deactivated_overgrace=$(psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select count(username) from accounts a, users u where disabled is true and a.id=u.account_id and current_sign_in_at < now()-'${DELDELIMIT}'::interval and username not in ($SQLPROTECTEDUSERS)" | tr -d " " )
|
num_deactivated_overgrace=$(psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select count(username) from accounts a, users u where disabled is true and a.id=u.account_id and current_sign_in_at < now()-'${DELDELIMIT}'::interval and username not in ($SQLPROTECTEDUSERS)" | tr -d " " )
|
||||||
num_deactivated_total=$(psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select count(username) from accounts a, users u where disabled is true and a.id=u.account_id and username not in ($SQLPROTECTEDUSERS)" | tr -d " " )
|
num_deactivated_total=$(psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select count(username) from accounts a, users u where disabled is true and a.id=u.account_id and username not in ($SQLPROTECTEDUSERS)" | tr -d " " )
|
||||||
@ -153,9 +171,9 @@ case ${mode} in
|
|||||||
echo "==================================="
|
echo "==================================="
|
||||||
echo "Total deactivated accounts: ${num_deactivated_total}"
|
echo "Total deactivated accounts: ${num_deactivated_total}"
|
||||||
echo "Number deactivated accounts over grace: ${num_deactivated_overgrace}"
|
echo "Number deactivated accounts over grace: ${num_deactivated_overgrace}"
|
||||||
echo "Deleting this many accounts: ${limit_delete}"
|
echo "Deleting this many accounts: ${LIMIT_DELETE}"
|
||||||
echo "==================================="
|
echo "==================================="
|
||||||
for u in $(psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select concat(username||';'||display_name||';'||email||';'||to_char(a.created_at, 'YYYY-MM-DD')||';'||to_char(current_sign_in_at,'YYYY-MM-DD')) from accounts a, users u where disabled is true and a.id=u.account_id and current_sign_in_at < now()-'${DELDELIMIT}'::interval and username not in ($SQLPROTECTEDUSERS) order by current_sign_in_at limit ${limit_delete}" | tr -d " " ); do
|
for u in $(psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select concat(username||';'||display_name||';'||email||';'||to_char(a.created_at, 'YYYY-MM-DD')||';'||to_char(current_sign_in_at,'YYYY-MM-DD')) from accounts a, users u where disabled is true and a.id=u.account_id and current_sign_in_at < now()-'${DELDELIMIT}'::interval and username not in ($SQLPROTECTEDUSERS) order by current_sign_in_at limit ${LIMIT_DELETE}" | tr -d " " ); do
|
||||||
#echo ${u}
|
#echo ${u}
|
||||||
username=$(echo "${u}" | awk -F ";" '{print $1}')
|
username=$(echo "${u}" | awk -F ";" '{print $1}')
|
||||||
dispname=$(echo "${u}" | awk -F ";" '{print $2}')
|
dispname=$(echo "${u}" | awk -F ";" '{print $2}')
|
||||||
@ -180,7 +198,7 @@ case ${mode} in
|
|||||||
*)
|
*)
|
||||||
# find & notify users that didn't logged in >6 months and send mail to log in again#psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select concat(username||';'||email) from accounts a, users u where domain is null and a.id=u.account_id and current_sign_in_at is null and u.created_at < now()-'2 weeks'::interval" | tr -d " "
|
# find & notify users that didn't logged in >6 months and send mail to log in again#psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select concat(username||';'||email) from accounts a, users u where domain is null and a.id=u.account_id and current_sign_in_at is null and u.created_at < now()-'2 weeks'::interval" | tr -d " "
|
||||||
#for username in $(psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select a.id, username, email, current_sign_in_at from accounts a, users u where domain is null and a.id=u.account_id and current_sign_in_at <'2019-01-01'" )
|
#for username in $(psql -U ${DB_USER} -w -h ${DB_HOST} -p ${DB_PORT} -t ${DB_NAME} -c "select a.id, username, email, current_sign_in_at from accounts a, users u where domain is null and a.id=u.account_id and current_sign_in_at <'2019-01-01'" )
|
||||||
for line in $(psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select concat(username||','||email) from accounts a, users u where domain is null and a.id=u.account_id and current_sign_in_at is null and u.created_at < now()-'2 weeks'::interval and username not in (${SQLPROTECTEDUSERS})"| tr -d " "); do
|
for line in $(psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select concat(username||','||email) from accounts a, users u where domain is null and disabled is false and a.id=u.account_id and current_sign_in_at is null and u.created_at < now()-'2 weeks'::interval and username not in (${SQLPROTECTEDUSERS})"| tr -d " "); do
|
||||||
#echo ${line}
|
#echo ${line}
|
||||||
username=$(echo "${line}" | cut -f1 -d"," )
|
username=$(echo "${line}" | cut -f1 -d"," )
|
||||||
mail=$(echo "${line}" | cut -f2 -d"," )
|
mail=$(echo "${line}" | cut -f2 -d"," )
|
||||||
@ -212,7 +230,8 @@ case ${mode} in
|
|||||||
;;
|
;;
|
||||||
"disable")
|
"disable")
|
||||||
#echo "in disable"
|
#echo "in disable"
|
||||||
SQLSTATE="current_sign_in_at < now()-'${UPPERLIMIT}'::interval"
|
#SQLSTATE="current_sign_in_at < now()-'${UPPERLIMIT}'::interval"
|
||||||
|
SQLSTATE="current_sign_in_at between now()-'${UPPERLIMIT}'::interval and now()-'${UPPERLIMIT}'::interval-'1 week'::interval"
|
||||||
;;
|
;;
|
||||||
#"delete")
|
#"delete")
|
||||||
# #echo "in delete"
|
# #echo "in delete"
|
||||||
@ -220,7 +239,7 @@ case ${mode} in
|
|||||||
# ;;
|
# ;;
|
||||||
esac
|
esac
|
||||||
#echo "SQL: $SQLSTATE"
|
#echo "SQL: $SQLSTATE"
|
||||||
for u in $( psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select concat(username||';'||display_name||';'||email||';'||to_char(a.created_at, 'YYYY-MM-DD')||';'||to_char(current_sign_in_at,'YYYY-MM-DD')) from accounts a, users u where domain is null and a.id=u.account_id and ${SQLSTATE} and username not in ($SQLPROTECTEDUSERS) order by current_sign_in_at" | tr -d " " ); do
|
for u in $( psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select concat(username||';'||display_name||';'||email||';'||to_char(a.created_at, 'YYYY-MM-DD')||';'||to_char(current_sign_in_at,'YYYY-MM-DD')) from accounts a, users u where domain is null and a.id=u.account_id and ${SQLSTATE} and disabled is false and username not in ($SQLPROTECTEDUSERS) order by current_sign_in_at" | tr -d " " ); do
|
||||||
#echo ${u}
|
#echo ${u}
|
||||||
username=$(echo "${u}" | awk -F ";" '{print $1}')
|
username=$(echo "${u}" | awk -F ";" '{print $1}')
|
||||||
dispname=$(echo "${u}" | awk -F ";" '{print $2}')
|
dispname=$(echo "${u}" | awk -F ";" '{print $2}')
|
||||||
@ -255,6 +274,7 @@ case ${mode} in
|
|||||||
echo "will be disabled, but is skipped because of dryrun."
|
echo "will be disabled, but is skipped because of dryrun."
|
||||||
fi
|
fi
|
||||||
num_disabled=$((num_disabled+1))
|
num_disabled=$((num_disabled+1))
|
||||||
|
exit
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
RND=$(date +%s)
|
RND=$(date +%s)
|
||||||
@ -263,6 +283,7 @@ case ${mode} in
|
|||||||
sleep ${sec}.${ms}
|
sleep ${sec}.${ms}
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
num_deactivated_total=$(psql -U "${DB_USER}" -w -h "${DB_HOST}" -p "${DB_PORT}" -t "${DB_NAME}" -c "select count(username) from accounts a, users u where disabled is true and a.id=u.account_id and username not in ($SQLPROTECTEDUSERS)" | tr -d " " )
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
@ -273,5 +294,5 @@ echo "Starting time : ${STARTDATE}"
|
|||||||
echo "Ending time : ${ENDDATE}"
|
echo "Ending time : ${ENDDATE}"
|
||||||
echo "Notified Users: $num_notified"
|
echo "Notified Users: $num_notified"
|
||||||
echo "Disabled Users: $num_disabled"
|
echo "Disabled Users: $num_disabled"
|
||||||
echo "Deleted Users : $num_deleted"
|
echo "Deleted Users : $num_deactivated_total"
|
||||||
echo "==================================="
|
echo "==================================="
|
||||||
|
Loading…
x
Reference in New Issue
Block a user