Skip to content

Instantly share code, notes, and snippets.

@SanMurakami
Last active November 10, 2024 11:40
Show Gist options
  • Save SanMurakami/27eab4fc8f2e91eaab4079aa1497ac03 to your computer and use it in GitHub Desktop.
Save SanMurakami/27eab4fc8f2e91eaab4079aa1497ac03 to your computer and use it in GitHub Desktop.
指定したサーバーデータをMisskeyから削除するシェルスクリプト
#!/bin/bash
if [ -z "$1" ]; then
echo "Error: No instance specified."
echo "Usage: ./delete-all.sh example.com"
exit 1
fi
read -p $'\e[31mDelete all data for \e[1;31m'"$1"$'\e[31m. Are you sure? (y/n): \e[0m' response
delete_rows() {
local table="$1"
local condition_column="$2"
local condition_value="$3"
while :
do
local query="SELECT \"id\" FROM \"public\".\"${table}\" WHERE (\"${condition_column}\" = '${condition_value}') ORDER BY \"id\" ASC LIMIT 100"
local ids=$(psql -d misskey -c "$query" -t -A -F',')
if [ -z "$ids" ]; then
echo "No more records to delete from ${table}."
break
fi
local ids_string=$(echo $ids | tr ' ' ',' | sed "s/\(\w\+\)/'\1'/g" | tr '\n' ',' | sed 's/,$//')
local delete_query="DELETE FROM \"public\".\"${table}\" WHERE \"id\" IN ($ids_string)"
echo $(date)
psql -d misskey -c "$delete_query"
done
}
case $response in
[yY][eE][sS]|[yY])
echo "Execution will start in 10 seconds. Press Ctrl+C to cancel."
for i in {10..1}; do
printf "\rStarting in %2d seconds..." "$i"
sleep 1
done
printf "\rExecuting now... \n"
sleep 1
echo "Notes deletion is starting..."
delete_rows "note" "userHost" "$1"
sleep 1
echo "Files deletion is starting..."
delete_rows "drive_file" "userHost" "$1"
sleep 1
echo "Users deletion is starting..."
delete_rows "user" "host" "$1"
sleep 1
echo "Instance deletion is starting..."
psql -d misskey -c "DELETE FROM \"public\".\"instance\" WHERE (\"host\" = '$1')"
echo -e "\e[32mAll done!\e[0m"
;;
*)
exit 1
;;
esac
@SanMurakami
Copy link
Author

PostgreSQLへの接続はDB名「misskey」で決め打ちしてるのでよしなに変更して使ってください

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment