forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb-backup.sh
More file actions
executable file
·62 lines (51 loc) · 1.88 KB
/
Copy pathdb-backup.sh
File metadata and controls
executable file
·62 lines (51 loc) · 1.88 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env bash
# db-backup.sh — Daily PostgreSQL backup with AES-256 encryption and S3 upload
#
# Required env vars:
# DATABASE_URL — postgres connection string
# BACKUP_S3_BUCKET — S3 bucket name
# BACKUP_PASSPHRASE — encryption passphrase
#
# Optional env vars:
# AWS_REGION — AWS region (default: us-east-1)
# BACKUP_DIR — local staging dir (default: /tmp/pg-backups)
# BACKUP_RETAIN_DAYS — S3 lifecycle managed; local cleanup after upload (default: 1)
set -euo pipefail
: "${DATABASE_URL:?DATABASE_URL is required}"
: "${BACKUP_S3_BUCKET:?BACKUP_S3_BUCKET is required}"
: "${BACKUP_PASSPHRASE:?BACKUP_PASSPHRASE is required}"
BACKUP_DIR="${BACKUP_DIR:-/tmp/pg-backups}"
AWS_REGION="${AWS_REGION:-us-east-1}"
TIMESTAMP=$(date -u +"%Y%m%dT%H%M%SZ")
DUMP_FILE="${BACKUP_DIR}/nova_rewards_${TIMESTAMP}.dump"
ENC_FILE="${DUMP_FILE}.enc"
mkdir -p "$BACKUP_DIR"
echo "[backup] Dumping database → ${DUMP_FILE}"
pg_dump "$DATABASE_URL" \
--format=custom \
--compress=9 \
--no-password \
--file="$DUMP_FILE"
echo "[backup] Encrypting with AES-256-CBC"
openssl enc -aes-256-cbc -pbkdf2 -salt \
-pass "pass:${BACKUP_PASSPHRASE}" \
-in "$DUMP_FILE" \
-out "$ENC_FILE"
# Write metadata sidecar
cat > "${ENC_FILE%.enc}.json" <<EOF
{
"fileName": "$(basename "$ENC_FILE")",
"timestamp": "${TIMESTAMP}",
"encryption": "aes-256-cbc-pbkdf2"
}
EOF
echo "[backup] Uploading to s3://${BACKUP_S3_BUCKET}/postgres/"
aws s3 cp "$ENC_FILE" "s3://${BACKUP_S3_BUCKET}/postgres/$(basename "$ENC_FILE")" \
--region "$AWS_REGION" \
--storage-class STANDARD_IA
aws s3 cp "${ENC_FILE%.enc}.json" \
"s3://${BACKUP_S3_BUCKET}/postgres/$(basename "${ENC_FILE%.enc}.json")" \
--region "$AWS_REGION"
echo "[backup] Upload complete — cleaning up local files"
rm -f "$DUMP_FILE" "$ENC_FILE" "${ENC_FILE%.enc}.json"
echo "[backup] Done — $(date -u)"