Last active
November 10, 2024 11:40
-
-
Save SanMurakami/27eab4fc8f2e91eaab4079aa1497ac03 to your computer and use it in GitHub Desktop.
指定したサーバーデータをMisskeyから削除するシェルスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PostgreSQLへの接続はDB名「misskey」で決め打ちしてるのでよしなに変更して使ってください