Compare commits

..

5 Commits

Author SHA1 Message Date
skylar
fd9ca3ee17 add database prune sql script 2025-08-03 04:03:22 -05:00
Skylar Kaede
e8639069fb Add backup & restore commands 2025-01-12 14:02:10 -06:00
skylar
8e52f971c9 fix formatting 2024-06-20 20:39:16 -05:00
skylar
32e614865c update repack 2024-06-20 20:38:22 -05:00
skylar
eb0f634040 add delete info 2024-06-20 20:37:56 -05:00
4 changed files with 132 additions and 7 deletions

30
backup.md Normal file
View File

@@ -0,0 +1,30 @@
This is the script I use for routine backups of a pleroma server where I don't have access to the hypervisor. We need a few things:
- The pgsql database dumped to a file
- The contents of /opt/pleroma, mostly the uploads directory
- And it doesn't hurt to grab all of /etc to make sure we've got the latest nginx config and anything else that could make restoring to a new server easier.
```
#!/bin/bash
cd /opt/pleroma
echo Backup started at $(date)
sudo -Hu postgres pg_dump -d pleroma --format=custom -f /var/tmp/pleroma.pgsql
rsync -e "ssh -i $HOME/.ssh/backup_rsa -p 22" -avzh /opt/pleroma /etc /var/tmp/pleroma.pgsql pleroma-backup@server.example.com:/data
rm -f /var/tmp/pleroma.pgsql
```
For restoring the database and files, the easiest way I've found to do so is installing a fresh copy of pleroma of the same or later version and then restoring the backed up copy over top of it.
```
sudo -Hu pleroma git clone -b develop https://git.pleroma.social/pleroma/pleroma /opt/pleroma
sudo -Hu pleroma mix deps.get
sudo -Hu pleroma MIX_ENV=prod mix pleroma.instance gen
sudo -Hu postgres psql -c 'DROP DATABASE pleroma;';
sudo -Hu postgres psql -c 'DROP USER pleroma;'
sudo -Hu postgres psql -f config/setup_db.psql
sudo -Hu postgres pg_restore -d pleroma -v /opt/pleroma/backup.pgsql -j 5
sudo -Hu pleroma MIX_ENV=prod mix ecto.migrate
sudo -Hu postgres vacuumdb --all --analyze-in-stages
sudo -Hu pleroma MIX_ENV=prod mix pleroma.frontend install pleroma-fe
systemctl start pleroma
```
After that, the uploads directory, custom emoji, and theme will need to go back in place, but it should be 99% working.

39
database_prune.sql Normal file
View File

@@ -0,0 +1,39 @@
\c pleroma
do $$
declare
maxA integer := (SELECT count(*) from activities WHERE inserted_at < now() - interval '4 months');
maxO integer := (SELECT count(*) from objects WHERE inserted_at < now() - interval '4 months');
maxN integer := (SELECT count(*) from notifications WHERE inserted_at < now() - interval '4 months');
counter integer := 0;
begin
RAISE NOTICE 'Marked for deletion: % activities', maxA;
while counter < maxA loop
DELETE FROM activities WHERE id = any (array(SELECT id FROM activities WHERE inserted_at < now() - interval '4 months' LIMIT 1000));
COMMIT;
PERFORM pg_sleep(3);
counter := counter + 1000;
end loop;
RAISE NOTICE 'Marked for deletion: % objects', maxO;
counter := 0;
while counter < maxO loop
DELETE FROM deliveries WHERE object_id = any (array(SELECT id FROM objects WHERE inserted_at < now() - interval '4 months' LIMIT 10000));
COMMIT;
PERFORM pg_sleep(3);
counter := counter + 1000;
end loop;
counter := 0;
while counter < maxO loop
DELETE FROM objects WHERE id = any (array(SELECT id FROM objects WHERE inserted_at < now() - interval '4 months' LIMIT 1000));
COMMIT;
PERFORM pg_sleep(3);
counter := counter + 1000;
end loop;
RAISE NOTICE 'Marked for deletion: % notifications', maxN;
counter := 0;
while counter < maxO loop
DELETE FROM notifications WHERE id = any (array(SELECT id FROM notifications WHERE inserted_at < now() - interval '4 months' LIMIT 1000));
COMMIT;
PERFORM pg_sleep(3);
counter := counter + 1000;
end loop;
end$$;

62
deletes.md Normal file
View File

@@ -0,0 +1,62 @@
Deletes are the danger zone, deleting activities from the table can and will introduce weird problems. For example, when a new activity federates in referencing a deleted activity, it'll cause notifications to temporarily break for the affected user(s). Pleroma will look in the table for the activity it's referencing, that'll fail, and users will see one or more 500 errors on screen.
But if it's a choice between killing the instance entirely due to storage constraints or deleting activities and accepting the risk of errors, we can delete them. Before you delete anything, take a look at how to clean up pg_repack remnants [here](https://git.yandere.love/skylar/pleromer-stuff/src/branch/main/repack.md).
You can check the size of each table and get an idea of your worst offenders with this pgsql command:
```
SELECT
relname as table_name,
pg_size_pretty(pg_total_relation_size(relid)) As "Total Size",
pg_size_pretty(pg_indexes_size(relid)) as "Index Size",
pg_size_pretty(pg_relation_size(relid)) as "Actual Size"
FROM pg_catalog.pg_statio_user_tables
ORDER BY pg_total_relation_size(relid) DESC;
```
Since there's a LOT of stuff to delete, we don't want to do it all at once and piss off pgsql. Here's an example pgsql script to delete all activities, both local and remote, older than 2024-01-01:
```
do $$
declare
counter integer := 0;
begin
while counter < 1000000 loop
DELETE FROM activities WHERE id = any (array(SELECT id FROM activities WHERE inserted_at < '2024-01-01' LIMIT 100));
COMMIT;
PERFORM pg_sleep(2);
counter := counter + 1;
end loop;
end$$;
```
It will occasionally fail when something weird happens, but you can just run it again until you get no results from:
```
SELECT count(*) FROM activities WHERE inserted_at < '2024-01-01';
```
The objects table is a little different and requires coordination with the deliveries table.
```
do $$
declare
counter integer := 0;
begin
while counter < 1000000 loop
DELETE FROM deliveries WHERE object_id = any (array(SELECT id FROM objects WHERE inserted_at < '2024-01-01' LIMIT 1));
DELETE FROM objects WHERE id = any (array(SELECT id FROM objects WHERE inserted_at < '2024-01-01' LIMIT 1));
COMMIT;
PERFORM pg_sleep(2);
counter := counter + 1;
end loop;
end$$;
```
To prune the database on a recurring basis, adjust this [SQL file](https://git.yandere.love/skylar/pleromer-stuff/src/branch/main/database_prune.sql) to your desired retention period for activities, objects, and notifications, then execute it with:
```
sudo -Hu postgres psql -f /path/to/database_prune.sql
```
If you've named your database something other than "pleroma", that will need to be changed as well.

View File

@@ -9,15 +9,9 @@ SELECT
pg_size_pretty(pg_relation_size(relid)) as "Actual Size" pg_size_pretty(pg_relation_size(relid)) as "Actual Size"
FROM pg_catalog.pg_statio_user_tables FROM pg_catalog.pg_statio_user_tables
ORDER BY pg_total_relation_size(relid) DESC; ORDER BY pg_total_relation_size(relid) DESC;
if you see shit like log_490586 and table_345345345345 in there, that's repack remnants.
DROP EXTENSION pg_repack CASCADE;
CREATE EXTENSION pg_repack;
``` ```
To clean this up, simply drop the repack extension with the CASCADE option and re-add it to the database: If you see shit like log_490586 and table_345345345345 in there, that's repack remnants. To clean this up, simply drop the repack extension with the CASCADE option and re-add it to the database:
``` ```
DROP EXTENSION pg_repack CASCADE; DROP EXTENSION pg_repack CASCADE;
CREATE EXTENSION pg_repack; CREATE EXTENSION pg_repack;