File size: 1,732 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #!/bin/bash
set -e
PGDATA="/data/postgres"
BACKUP_DIR="/data/backups"
DUMP_FILE="$BACKUP_DIR/n8n_backup.sql"
PG_BIN="/usr/lib/postgresql/15/bin"
restore_from_hf() {
if [ -z "$HF_TOKEN" ] || [ -z "$HF_BACKUP_REPO" ]; then
return
fi
python3 -c "
from huggingface_hub import hf_hub_download
try:
hf_hub_download(repo_id='$HF_BACKUP_REPO', filename='n8n_backup.sql', repo_type='dataset', token='$HF_TOKEN', local_dir='$BACKUP_DIR')
print('Backup downloaded from HF')
except Exception as e:
print(f'No backup found on HF: {e}')
"
}
mkdir -p /data/postgres /data/backups /data/n8n /run/postgresql
chown -R postgres:postgres /data/postgres /run/postgresql
chmod 700 /data/postgres
if [ ! -f "$PGDATA/PG_VERSION" ]; then
su - postgres -c "$PG_BIN/initdb -D $PGDATA --encoding=UTF8 --locale=C"
cat >> "$PGDATA/postgresql.conf" <<EOF
shared_buffers = 32MB
work_mem = 4MB
dynamic_shared_memory_type = posix
shared_memory_type = mmap
listen_addresses = 'localhost'
port = 5432
unix_socket_directories = '/run/postgresql'
EOF
su - postgres -c "$PG_BIN/pg_ctl -D $PGDATA -l /tmp/pg_init.log start -w -t 60" || {
echo "=== PostgreSQL failed to start. Log: ==="
cat /tmp/pg_init.log
exit 1
}
su - postgres -c "psql -c \"CREATE USER n8n WITH PASSWORD 'n8n_secret';\""
su - postgres -c "psql -c \"CREATE DATABASE n8n OWNER n8n;\""
su - postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE n8n TO n8n;\""
restore_from_hf
if [ -f "$DUMP_FILE" ]; then
su - postgres -c "psql -d n8n < $DUMP_FILE" || true
fi
su - postgres -c "$PG_BIN/pg_ctl -D $PGDATA stop -w"
fi
exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
|