Nightly PostgreSQL backups with 14-day rotation · javascriptcm/javascript.cm@0ea53b6 · GitHub
Skip to content

Commit 0ea53b6

Browse files
committed
Nightly PostgreSQL backups with 14-day rotation
1 parent 838254c commit 0ea53b6

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions

‎deploy/backup.sh‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
# Dump the PostgreSQL database (custom format, compressed) and keep the
3+
# last BACKUP_KEEP_DAYS days. Run from the repository root's deploy/ dir
4+
# by the jscm-backup systemd timer (see deploy/README section in README.md).
5+
#
6+
# Restore:
7+
# docker compose exec -T db pg_restore -U <user> -d <db> --clean --if-exists < file.dump
8+
set -euo pipefail
9+
10+
main() {
11+
cd "$(dirname "$0")/.."
12+
13+
local dir="${BACKUP_DIR:-$HOME/backups/jscm}"
14+
local keep="${BACKUP_KEEP_DAYS:-14}"
15+
local db_user db_name
16+
db_user="$(grep -E '^DB_USER=' .env | cut -d= -f2-)"
17+
db_name="$(grep -E '^DB_DATABASE=' .env | cut -d= -f2-)"
18+
19+
install -d -m 700 "$dir"
20+
local file="$dir/jscm-$(date -u +%Y%m%dT%H%M%SZ).dump"
21+
22+
docker compose exec -T db pg_dump -U "$db_user" -d "$db_name" --format=custom --no-owner > "$file.tmp"
23+
mv "$file.tmp" "$file"
24+
chmod 600 "$file"
25+
26+
find "$dir" -name 'jscm-*.dump' -type f -mtime +"$keep" -delete
27+
echo "backup: $file ($(du -h "$file" | cut -f1))"
28+
}
29+
30+
main "$@"

‎deploy/jscm-backup.service‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[Unit]
2+
Description=javascript.cm PostgreSQL backup
3+
Wants=docker.service
4+
After=docker.service
5+
6+
[Service]
7+
Type=oneshot
8+
User=brightky
9+
ExecStart=/home/brightky/apps/javascript.cm/deploy/backup.sh

‎deploy/jscm-backup.timer‎

Lines changed: 10 additions & 0 deletions

0 commit comments

Comments
 (0)