-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdb_backup
More file actions
45 lines (43 loc) · 1.59 KB
/
db_backup
File metadata and controls
45 lines (43 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
## 2025-06-25 Fine tuning backup script to be more selective when backing up
for DB in $(su postgres -c "psql -At -c \"select datname from pg_database where datistemplate = false and datname <> 'postgres';\" postgres")
do
echo "Kontrollerar $DB..."
DO_BACKUP=$(sudo su postgres -c "psql $DB -c \"DO \\$\\$
DECLARE
tabeller text[] := ARRAY['res_users', 'res_partner', 'sale_order', 'account_move', 'mrp_production', 'project_task', 'stock_picking', 'ir_session'];
t text;
sql text := '';
first boolean := true;
active boolean;
BEGIN
FOR t IN
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = ANY(tabeller)
LOOP
IF first THEN
sql := sql || format('SELECT MAX(write_date) AS write_date FROM %I', t);
first := false;
ELSE
sql := sql || format(' UNION ALL SELECT MAX(write_date) FROM %I', t);
END IF;
END LOOP;
IF sql <> '' THEN
sql := 'SELECT (MAX(write_date) > (CURRENT_DATE - INTERVAL ''5 days'')) AS active FROM (' || sql || ') AS all_dates;';
EXECUTE sql INTO active;
RAISE NOTICE '%', active;
ELSE
RAISE NOTICE 'Inga tabeller hittades.';
-- Alternativt: RETURN FALSE;
END IF;
END \\$\\$;
\"" 2>&1|cut -c10) ;
if [ "$DO_BACKUP" == 't' ]; then
echo "Ändringar hittades ($DO_BACKUP). Tar backup..."
su postgres -c "pg_dump -Fp $DB" | gzip > /var/backups/"$DB".sql.gz
else
echo "Ingen relevant ändring hittades i $DB. Ingen backup tas."
fi
done