mirror of
https://codeberg.org/Windfluechter/cleanup-mastodon-users.sh.git
synced 2026-02-01 08:25:35 +00:00
Compare commits
No commits in common. "main" and "Release" have entirely different histories.
11
README.md
11
README.md
@ -1,14 +1,13 @@
|
|||||||
# cleanup-mastodon-users.sh
|
# cleanup-mastodon-users.sh
|
||||||
Script to remove inactive users on your Mastodon server. This is a small cleanup script for Mastodon node admins to help with forgotten or dead accounts.
|
Script to remove inactive users on your Mastodon server. This is a small cleanup script for Friendica node admins to help with forgotten or dead accounts.
|
||||||
|
|
||||||
The script does two things:
|
The script does two things:
|
||||||
1. delete all users after 2 weeks that haven't logged in at all and didn't post anything.
|
1. delete all users after 2 weeks that haven't logged in at all and didn't post anything.
|
||||||
2. search for accounts that haven't logged in for 6 months, send them a reminder mail, deactivate accounts that haven't logged within 7 months and delete those deactivated users in a `--cron` run.
|
2. search for accounts that haven't logged in for 6 months, send them a reminder mail and delete accounts that haven't logged within 7 months.
|
||||||
|
|
||||||
# Installation
|
# Installation
|
||||||
* save the script in home directory of Mastodon (eg. ~/bin, not under /root, maybe /usr/local/bin)
|
* save the script in home directory of Mastodon (eg. ~/bin, not under /root, maybe /usr/local/bin)
|
||||||
* make the script executable (e.g. `chown mastodon:mastodon bin/cleanup-mastodon-users.sh && chmod u+rx bin/cleanup-mastodon-users.sh)`
|
* make the script executable (e.g. `chown mastodon:mastodon bin/cleanup-mastodon-users.sh && chmod u+rx bin/cleanup-mastodon-users.sh)`
|
||||||
* create a config file at `$HOME/.cleanup-mastodon-users.conf` by calling the script with `--init` argument and edit the file to fit your needs
|
|
||||||
* create a crontab to execute the script, e.g.: `20 8 * * 4 /home/mastodon/bin/cleanup-mastodon-users.sh --dowhatimean` to execute the script every week. In the example it is executed every Thursday (4th day of week) at 8:20 in the morning.
|
* create a crontab to execute the script, e.g.: `20 8 * * 4 /home/mastodon/bin/cleanup-mastodon-users.sh --dowhatimean` to execute the script every week. In the example it is executed every Thursday (4th day of week) at 8:20 in the morning.
|
||||||
* for the first runs you should set `UPPERLIMIT` to a high number and keep the deleting of the user commented out (`${mastodonpath}/bin/tootctl accounts delete "${username}"`). After the first few runs you can reduce `UPPERLIMIT` to a lower setting and repeat this until you reached the final value. You shouldn't have too many weeks between `LOWERLIMIT` and `UPPERLIMIT`. Sensible values might be 6 and 7 months or 12 and 13 months or so.
|
* for the first runs you should set `UPPERLIMIT` to a high number and keep the deleting of the user commented out (`${mastodonpath}/bin/tootctl accounts delete "${username}"`). After the first few runs you can reduce `UPPERLIMIT` to a lower setting and repeat this until you reached the final value. You shouldn't have too many weeks between `LOWERLIMIT` and `UPPERLIMIT`. Sensible values might be 6 and 7 months or 12 and 13 months or so.
|
||||||
* change the variables at the begin of the script to your site settings.
|
* change the variables at the begin of the script to your site settings.
|
||||||
@ -16,9 +15,7 @@ The script does two things:
|
|||||||
# Usage
|
# Usage
|
||||||
```
|
```
|
||||||
Usage:
|
Usage:
|
||||||
--init : create bin/cleanup-mastodon-users.sh/.cleanup-mastodon-users.conf config file.
|
|
||||||
--dry-run : make a dry-run, no deletion will be done, no mails are sent.
|
--dry-run : make a dry-run, no deletion will be done, no mails are sent.
|
||||||
--cron : delete deactivated users in a regularly cron run step by step to avoid mass flooding.
|
|
||||||
--dowhatimean : add this option if you really want to delete users.
|
--dowhatimean : add this option if you really want to delete users.
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -26,7 +23,3 @@ Usage:
|
|||||||
* test the script by using `--dry-run` argument with the script
|
* test the script by using `--dry-run` argument with the script
|
||||||
* you can change the mail command from `-b ${siteadmin} -- ${usermail}` to `-- ${siteadmin}` and comment out all lines with `bin/console user delete` statement for testing runs
|
* you can change the mail command from `-b ${siteadmin} -- ${usermail}` to `-- ${siteadmin}` and comment out all lines with `bin/console user delete` statement for testing runs
|
||||||
|
|
||||||
# Donations
|
|
||||||
If you want to support my work, you can donate via LiberaPay:
|
|
||||||
|
|
||||||
[](https://liberapay.com/Windfluechter/donate) or on [Patreon](https://patreon.com/nerdculture_de)
|
|
||||||
|
|||||||
365
cleanup-mastodon-users.sh
Normal file → Executable file
365
cleanup-mastodon-users.sh
Normal file → Executable file
@ -1,293 +1,164 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
|
# LOWERLIMIT="'6 months'" or "'24 weeks'"
|
||||||
|
# UPPERLIMIT="'7 months'" or "'28 weeks'"
|
||||||
|
# this defines the range of inactivity where users get notified
|
||||||
|
# before the account will be deleted when UPPERLIMIT is surpassed.
|
||||||
|
LOWERLIMIT="6 months"
|
||||||
|
UPPERLIMIT="7 months"
|
||||||
|
|
||||||
|
TOOTCTL="~/live/bin/tootctl"
|
||||||
|
|
||||||
set -f
|
set -f
|
||||||
|
|
||||||
# create a default config when started with --init
|
# get some settings from the Mastodon config and write it to another file, so that we can source it and make use of it
|
||||||
initConfig() {
|
grep -e ^"DB_" -e ^"LOCAL_DOMAIN" /home/mastodon/live/.env.production > /home/mastodon/bin/cleanup-mastodon-users.conf
|
||||||
(
|
. /home/mastodon/bin/cleanup-mastodon-users.conf
|
||||||
cat <<EOF
|
|
||||||
LOWERLIMIT="6 months" # all accounts below 6 months inactivity are safe, start notifiying them when >6 months
|
|
||||||
UPPERLIMIT="7 months" # inactive accounts older than 7 months will be deactivated
|
|
||||||
DELDELIMIT="1 year" # all accounts not used within a year will get deleted
|
|
||||||
LIVE_PATH="${HOME}/live/" # Path to live data from mastodon
|
|
||||||
TOOTCTL="${HOME}/live/bin/tootctl"
|
|
||||||
SITE="Mastodon"
|
|
||||||
SITEADMIN="root@localhost"
|
|
||||||
PROTECTEDUSERS="user foo bar baz"
|
|
||||||
SQLPROTECTEDUSERS="'user', 'foo', 'bar', 'baz'"
|
|
||||||
LIMIT_DELETE="2"
|
|
||||||
|
|
||||||
EOF
|
# the following lines should be moved to a config file, eg. /usr/local/etc/cleanup_friendica.conf
|
||||||
) > "${HOME}"/.cleanup-mastodon-users.conf
|
mastodonpath="/home/mastodon/live"
|
||||||
echo "✅ - Configuration file created. You can now edit "${HOME}"/.cleanup-mastodon-users.conf"
|
#DB_HOST=127.0.0.1
|
||||||
}
|
#DB_PORT=6432
|
||||||
|
#DB_NAME=mastodon
|
||||||
|
#DB_USER=mastodon
|
||||||
|
#DB_PASS=
|
||||||
|
|
||||||
|
site="<SITETITEL>"
|
||||||
|
siteurl="<SITEURL>"
|
||||||
|
siteadmin="<ADMINMAIL"
|
||||||
|
sitefrom="no-reply@<SITEDOMAIN>"
|
||||||
|
protectedusers="<space seperated list of protected accounts, eg: user1 user2>"
|
||||||
|
sqlprotectedusers="<same, but with delimiters for SQL, eg:'user1', 'user2'>"
|
||||||
|
|
||||||
case $1 in
|
case $1 in
|
||||||
"--init")
|
|
||||||
initConfig
|
|
||||||
exit 0
|
|
||||||
;;
|
|
||||||
"--dry-run")
|
"--dry-run")
|
||||||
mode="dryrun"
|
mode="dryrun"
|
||||||
;;
|
;;
|
||||||
"--cron")
|
|
||||||
mode="cron"
|
|
||||||
;;
|
|
||||||
"--dowhatimean")
|
"--dowhatimean")
|
||||||
mode="hotrun"
|
mode="hotrun"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "Usage: "
|
echo "Usage: "
|
||||||
echo " --init \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 " --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 " --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
|
exit 0
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
#. /usr/local/etc/cleanup_friendica.conf
|
||||||
|
|
||||||
. "${HOME}"/.cleanup-mastodon-users.conf
|
# make a list to be used for grep -E
|
||||||
|
protected=$(echo $protectedusers | sed 's/\"//g' | sed 's/\ /\\\|/g')
|
||||||
LOWERLIMIT="${LOWERLIMIT:-'6 months'}" # all accounts below 6 months inactivity are safe, start notifiying them when >6 months
|
echo $protected
|
||||||
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
|
|
||||||
|
|
||||||
# shellcheck source=/dev/null
|
|
||||||
. "${LIVE_PATH}/.env.production" 2>/dev/null
|
|
||||||
|
|
||||||
run_tootctl() {
|
|
||||||
"${TOOTCTL}" "$@"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
num_notified=0
|
|
||||||
num_disabled=0
|
|
||||||
num_deleted=0
|
|
||||||
|
|
||||||
STARTDATE=$(date +"%d.%m.%Y %H:%M:%S")
|
|
||||||
|
|
||||||
# make a list to be used for grep -E
|
|
||||||
PROTECTED=$(echo "$PROTECTEDUSERS" | sed 's/\"//g' | sed 's/\ /\\\|/g')
|
|
||||||
#echo $PROTECTED
|
|
||||||
|
|
||||||
|
cd ${mastodonpath} || exit 0
|
||||||
|
|
||||||
# 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 () {
|
||||||
(
|
( cat <<EOF
|
||||||
cat <<EOF
|
Dear ${dispname},
|
||||||
Dear ${dispname},
|
|
||||||
|
|
||||||
you have registered your account on ${LOCAL_DOMAIN} at ${registered} and last time you logged in was at ${lastlogin}.
|
you have registered your account on ${siteurl} at ${registered} and last time you logged in was at ${lastlogin}.
|
||||||
|
|
||||||
If you want to continue to keep your Mastodon account on Nerdculture then please log in at least every 6 months via web browser to keep your account alive. Otherwise we assume that you don't want to use it anymore and will deactivate your account 7 months after your last login (and delete it later).
|
If you want to continue to keep your Mastodon account on ${SITE} then please log in at least every 6 months via web browser to keep your account alive. Otherwise we assume that you don't want to use it anymore and will cancel your account 7 months after your last login.
|
||||||
|
|
||||||
You can access your profile at https://${LOCAL_DOMAIN}/@${username} or you can cancel your account on your own when logged in at https://${LOCAL_DOMAIN}/settings/delete - however we would like to see you become an active user again and contribute to the Fediverse, but of course it's up to you.
|
You can access your profile at ${profileurl} or you can cancel your account on your own when logged in at ${siteurl}removeme - however we would like to see you become an active user again and contribute to the Fediverse, but of course it's up to you.
|
||||||
|
|
||||||
Sincerely,
|
Sincerely,
|
||||||
your ${SITE} admins
|
your ${site} admins
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
) | sed 's/_/\ /g' | /usr/bin/mail -s "The Fediverse misses you, ${username}!" -r "${SMTP_FROM_ADDRESS}" -- "${usermail}"
|
) | sed 's/_/\ /g' | /usr/bin/mail -s "The Fediverse misses you, ${username}!" -r "${sitefrom}" -- "${usermail}"
|
||||||
# add '-b "$SITEADMIN"' before the "--" above to receive BCC mails
|
# add '-b "$siteadmin"' before the "--" above to receive BCC mails
|
||||||
}
|
}
|
||||||
|
|
||||||
# notify user that the account has been deleted because of inactivity
|
# notify user that the account has been deleted because of inactivity
|
||||||
notifyUserDisable() {
|
notifyUserDeletion () {
|
||||||
(
|
( cat <<EOF
|
||||||
cat <<EOF
|
Dear ${dispname},
|
||||||
Dear ${dispname},
|
|
||||||
|
|
||||||
you have registered your account on ${LOCAL_DOMAIN} at ${registered} and last time you logged in was at ${lastlogin}.
|
you have registered your accoutn on ${siteurl} at ${registered} and last time you logged in was at ${lastlogin}.
|
||||||
|
|
||||||
Since you haven't reacted to the previous mails and didn't login again, your account including all your data has now been deactivated and will be deleted at a (random) later time (after ${DELDELIMIT}). Until then you can still request reactivattion from the admins. See ${LOCAL_DOMAIN}/about for contact information.
|
Since you haven't reacted to the previous mails and didn't login again, your account including all your data has now been deleted.
|
||||||
|
|
||||||
You will just receive the final confirmation mail of account deletion and no further mails!
|
|
||||||
|
|
||||||
Sincerely,
|
Sincerely,
|
||||||
your ${SITE} admins
|
your ${site} admins
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
) | sed 's/_/\ /g' | /usr/bin/mail -s "Your account ${username} on ${SITE} has been deactivated!" -r "${SMTP_FROM_ADDRESS}" -- "${usermail}"
|
) | sed 's/_/\ /g' | /usr/bin/mail -s "Your account ${username} on ${site} has been deleted!" -r "${sitefrom}" -- "${usermail}"
|
||||||
# add '-b "$SITEADMIN"' before the "--" above to receive BCC mails
|
# add '-b "$siteadmin"' before the "--" above to receive BCC mails
|
||||||
}
|
}
|
||||||
|
|
||||||
# notify user that the account has been deleted because of inactivity
|
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 last_sign_in_at is null and u.created_at < now()-'2 weeks'::interval and username not in (${sqlprotectedusers})"| tr -d " "); do
|
||||||
notifyUserDeletion() {
|
#echo ${line}
|
||||||
(
|
username=$(echo ${line} | cut -f1 -d"," )
|
||||||
cat <<EOF
|
mail=$(echo ${line} | cut -f2 -d"," )
|
||||||
Dear ${dispname},
|
# if username is a protected user do nothing, else delete user
|
||||||
|
if [ -n "${protectedusers}" ]; then
|
||||||
you have registered your account on ${LOCAL_DOMAIN} at ${registered} and last time you logged in was at ${lastlogin}.
|
pcheck=0
|
||||||
|
for s in $(echo ${protectedusers}) ; do
|
||||||
Since you haven't reacted to the previous mails and didn't login again, your account including all your data has now been deleted.
|
if [ "${s}" = "${username}" ]; then
|
||||||
|
pcheck=1
|
||||||
This is the last and final mail about your account on ${LOCAL_DOMAIN}
|
|
||||||
|
|
||||||
Sincerely,
|
|
||||||
your ${SITE} admins
|
|
||||||
|
|
||||||
EOF
|
|
||||||
) | sed 's/_/\ /g' | /usr/bin/mail -s "Your account ${username} on ${SITE} has been deleted!" -r "${SMTP_FROM_ADDRESS}" -- "${usermail}"
|
|
||||||
# add '-b "$SITEADMIN"' before the "--" above to receive BCC mails
|
|
||||||
}
|
|
||||||
|
|
||||||
# main loop
|
|
||||||
case ${mode} in
|
|
||||||
"init")
|
|
||||||
initConfig
|
|
||||||
;;
|
|
||||||
"cron")
|
|
||||||
# get the total number of deactivated accounts
|
|
||||||
# the intention is that you can better judge how often you need to invoke the cron option
|
|
||||||
# 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
|
|
||||||
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 " " )
|
|
||||||
# when there is less then 1 user to delete (=0) then exit
|
|
||||||
if [ "${num_deactivated_overgrace}" -lt 1 ]; then
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
echo "==================================="
|
|
||||||
echo "Account summary:"
|
|
||||||
echo "Total deactivated (>${UPPERLIMIT}): ${num_deactivated_total}"
|
|
||||||
echo "Over grace (${DELDELIMIT}): ${num_deactivated_overgrace}"
|
|
||||||
echo "Deleting at once: ${LIMIT_DELETE}"
|
|
||||||
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
|
|
||||||
#echo ${u}
|
|
||||||
username=$(echo "${u}" | awk -F ";" '{print $1}')
|
|
||||||
dispname=$(echo "${u}" | awk -F ";" '{print $2}')
|
|
||||||
usermail=$(echo "${u}" | awk -F ";" '{print $3}')
|
|
||||||
registered=$(echo "${u}" | awk -F ";" '{print $4}')
|
|
||||||
lastlogin=$(echo "${u}" | awk -F ";" '{print $5}')
|
|
||||||
# delete account when last login is older than 7 months and send mail about deletion
|
|
||||||
# you should copy & paste the text from 6 months for the first runs of this script
|
|
||||||
# and later change the text to a notification that the account has been deleted.
|
|
||||||
# if username is a PROTECTED user do nothing, else delete user
|
|
||||||
echo -n "${username} : "
|
|
||||||
run_tootctl accounts delete "${username}"
|
|
||||||
notifyUserDeletion
|
|
||||||
num_deleted=$((num_deleted+1))
|
|
||||||
RND=$(date +%s)
|
|
||||||
sec=$(( $RND %60 ))
|
|
||||||
ms=$(( $RND %23 ))
|
|
||||||
sleep ${sec}.${ms}
|
|
||||||
done
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
# 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 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}
|
|
||||||
username=$(echo "${line}" | cut -f1 -d"," )
|
|
||||||
mail=$(echo "${line}" | cut -f2 -d"," )
|
|
||||||
# if username is a PROTECTED user do nothing, else delete user
|
|
||||||
if [ -n "${PROTECTEDUSERS}" ]; then
|
|
||||||
pcheck=0
|
|
||||||
for s in ${PROTECTEDUSERS} ; do
|
|
||||||
if [ "${s}" = "${username}" ]; then
|
|
||||||
pcheck=1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
if [ ${pcheck} -eq 0 ]; then
|
|
||||||
echo "Delete unconfirmed user ${username}"
|
|
||||||
if [ "${mode}" = "hotrun" ]; then
|
|
||||||
run_tootctl accounts delete "${username}"
|
|
||||||
elif [ "${mode}" = "dryrun" ]; then
|
|
||||||
echo "${username}: skipped because of dryrun."
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
#for u in $( ${LIVE_PATH}/bin/console user list active -c 10000 | grep -v '.*---.*' | sed 's/|/;/g' | tr -s "\ " | sed 's/^;\ //g' | sed 's/\ ;\ /;/g' | sed 's/\ /_/g' | tail -n +2 | grep -i -v -E ${PROTECTED} ); do
|
if [ ${pcheck} -eq 0 ]; then
|
||||||
for mode2 in $(echo "warn disable"); do
|
echo "Delete unconfirmed user ${username}"
|
||||||
#echo "mode2: $mode2"
|
if [ "${mode}" = "hotrun" ]; then
|
||||||
case ${mode2} in
|
${mastodonpath}/bin/tootctl accounts delete "${username}"
|
||||||
"warn")
|
elif [ "${mode}" = "dryrun" ]; then
|
||||||
#echo "in warn"
|
echo "${username}: skipped because of dryrun."
|
||||||
SQLSTATE="current_sign_in_at between now()-'${UPPERLIMIT}'::interval and now()-'${LOWERLIMIT}'::interval"
|
fi
|
||||||
;;
|
fi
|
||||||
"disable")
|
fi
|
||||||
#echo "in disable"
|
done
|
||||||
#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")
|
|
||||||
# #echo "in delete"
|
|
||||||
# SQLSTATE="current_sign_in_at < now()-'${UPPERLIMIT}'::interval"
|
|
||||||
# ;;
|
|
||||||
esac
|
|
||||||
#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 disabled is false and username not in ($SQLPROTECTEDUSERS) order by current_sign_in_at" | tr -d " " ); do
|
|
||||||
#echo ${u}
|
|
||||||
username=$(echo "${u}" | awk -F ";" '{print $1}')
|
|
||||||
dispname=$(echo "${u}" | awk -F ";" '{print $2}')
|
|
||||||
usermail=$(echo "${u}" | awk -F ";" '{print $3}')
|
|
||||||
registered=$(echo "${u}" | awk -F ";" '{print $4}')
|
|
||||||
lastlogin=$(echo "${u}" | awk -F ";" '{print $5}')
|
|
||||||
case ${mode2} in
|
|
||||||
"warn")
|
|
||||||
if [ "${mode}" = "hotrun" ]; then
|
|
||||||
#echo -n "hotrun "
|
|
||||||
notifyUser
|
|
||||||
elif [ "${mode}" = "dryrun" ]; then
|
|
||||||
echo "Check ${username}: notify skipped because of dryrun."
|
|
||||||
fi
|
|
||||||
num_notified=$((num_notified+1))
|
|
||||||
;;
|
|
||||||
"disable")
|
|
||||||
# delete account when last login is older than 7 months and send mail about deletion
|
|
||||||
# you should copy & paste the text from 6 months for the first runs of this script
|
|
||||||
# and later change the text to a notification that the account has been deleted.
|
|
||||||
# if username is a PROTECTED user do nothing, else delete user
|
|
||||||
echo -n "${username} : "
|
|
||||||
if [ "${mode}" = "hotrun" ]; then
|
|
||||||
#echo -n "hotrun "
|
|
||||||
#run_tootctl accounts delete "${username}"
|
|
||||||
run_tootctl accounts modify "${username}" --disable
|
|
||||||
#notifyUserDeletion
|
|
||||||
notifyUserDisable
|
|
||||||
#echo "deleted."
|
|
||||||
elif [ "${mode}" = "dryrun" ]; then
|
|
||||||
echo "will be deactivated, but is skipped because of dryrun."
|
|
||||||
fi
|
|
||||||
num_disabled=$((num_disabled+1))
|
|
||||||
exit
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
RND=$(date +%s)
|
|
||||||
sec=$(( $RND %5 ))
|
|
||||||
ms=$(( $RND %23 ))
|
|
||||||
sleep ${sec}.${ms}
|
|
||||||
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
|
|
||||||
|
|
||||||
ENDDATE=$(date +"%d.%m.%Y %H:%M:%S")
|
# find & notify users that didn't logged in >6 months and send mail to log in again
|
||||||
|
for mode2 in $(echo "warn delete"); do
|
||||||
|
#echo "mode2: $mode2"
|
||||||
|
case ${mode2} in
|
||||||
|
"warn")
|
||||||
|
#echo "in warn"
|
||||||
|
SQLSTATE="last_sign_in_at between now()-'${UPPERLIMIT}'::interval and now()-'${LOWERLIMIT}'::interval"
|
||||||
|
;;
|
||||||
|
"delete")
|
||||||
|
#echo "in delete"
|
||||||
|
SQLSTATE="last_sign_in_at < now()-'${UPPERLIMIT}'::interval"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
#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(last_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 last_sign_in_at" | tr -d " " ); do
|
||||||
|
#echo ${u}
|
||||||
|
username=$(echo "${u}" | awk -F ";" '{print $1}')
|
||||||
|
dispname=$(echo "${u}" | awk -F ";" '{print $2}')
|
||||||
|
profileurl="https://nerdculture.de/@${username}"
|
||||||
|
usermail=$(echo "${u}" | awk -F ";" '{print $3}')
|
||||||
|
registered=$(echo "${u}" | awk -F ";" '{print $4}')
|
||||||
|
lastlogin=$(echo "${u}" | awk -F ";" '{print $5}')
|
||||||
|
case ${mode2} in
|
||||||
|
"warn")
|
||||||
|
if [ "${mode}" = "hotrun" ]; then
|
||||||
|
#echo -n "hotrun "
|
||||||
|
notifyUser
|
||||||
|
elif [ "${mode}" = "dryrun" ]; then
|
||||||
|
echo "Check ${username}: notify skipped because of dryrun."
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
"delete")
|
||||||
|
# delete account when last login is older than 7 months and send mail about deletion
|
||||||
|
# you should copy & paste the text from 6 months for the first runs of this script
|
||||||
|
# and later change the text to a notification that the account has been deleted.
|
||||||
|
# if username is a protected user do nothing, else delete user
|
||||||
|
echo -n "${username} : "
|
||||||
|
if [ "${mode}" = "hotrun" ]; then
|
||||||
|
#echo -n "hotrun "
|
||||||
|
${mastodonpath}/bin/tootctl accounts delete "${username}"
|
||||||
|
notifyUserDeletion
|
||||||
|
#echo "deleted."
|
||||||
|
elif [ "${mode}" = "dryrun" ]; then
|
||||||
|
echo "skipped because of dryrun."
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
done
|
||||||
|
|
||||||
echo "==================================="
|
|
||||||
echo "Starting time : ${STARTDATE}"
|
|
||||||
echo "Ending time : ${ENDDATE}"
|
|
||||||
echo "Notified Users: $num_notified"
|
|
||||||
echo "Deactivated : $num_disabled"
|
|
||||||
echo "To be deleted : $num_deactivated_total"
|
|
||||||
echo "==================================="
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user