File size: 1,017 Bytes
5064fae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

INTERVAL="${BACKUP_INTERVAL:-300}"
BACKUP_DIR="/data/backups"
DUMP_FILE="$BACKUP_DIR/n8n_backup.sql"

mkdir -p "$BACKUP_DIR"
sleep 60

while true; do
    if su - postgres -c "pg_isready -h localhost -p 5432" > /dev/null 2>&1; then
        su - postgres -c "pg_dump -d n8n" > "$DUMP_FILE.tmp" 2>/dev/null
        if [ $? -eq 0 ] && [ -s "$DUMP_FILE.tmp" ]; then
            mv "$DUMP_FILE.tmp" "$DUMP_FILE"

            if [ -n "$HF_TOKEN" ] && [ -n "$HF_BACKUP_REPO" ]; then
                python3 -c "
from huggingface_hub import HfApi
api = HfApi(token='$HF_TOKEN')
try:
    api.create_repo(repo_id='$HF_BACKUP_REPO', repo_type='dataset', private=True, exist_ok=True)
    api.upload_file(path_or_fileobj='$DUMP_FILE', path_in_repo='n8n_backup.sql', repo_id='$HF_BACKUP_REPO', repo_type='dataset', token='$HF_TOKEN')
except Exception as e:
    print(f'Backup upload failed: {e}')
" 2>/dev/null
            fi
        else
            rm -f "$DUMP_FILE.tmp"
        fi
    fi
    sleep "$INTERVAL"
done