Skip to content

Commit 21324dd

Browse files
committed
chore(scripts): add pg backup & restore scripts for dev
1 parent 85bb318 commit 21324dd

File tree

6 files changed

+244
-4
lines changed

6 files changed

+244
-4
lines changed

biome.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
"!engine/artifacts",
99
"!engine/sdks",
1010
"!engine/sdks/typescript/api-full",
11-
"!engine/sdks/typescript/runner-protocol"
12-
"!examples/snippets",
11+
"!engine/sdks/typescript/runner-protocol",
1312
"!frontend",
1413
"!rivetkit-openapi/openapi.json",
1514
"!scripts",

pnpm-lock.yaml

Lines changed: 98 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/run/backup-postgres.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
CONTAINER_NAME="rivet-engine-postgres"
5+
6+
if [ $# -ne 1 ]; then
7+
echo "Usage: $0 <backup-path>"
8+
echo "Example: $0 /path/to/backup/postgres-backup.tar.gz"
9+
exit 1
10+
fi
11+
12+
BACKUP_PATH="$1"
13+
14+
# Check if container exists
15+
if ! docker ps --all --format '{{.Names}}' | grep -qw "${CONTAINER_NAME}"; then
16+
echo "error: container '${CONTAINER_NAME}' not found"
17+
exit 1
18+
fi
19+
20+
# Get the volume name
21+
VOLUME_NAME=$(docker inspect "${CONTAINER_NAME}" --format '{{range .Mounts}}{{if eq .Destination "/var/lib/postgresql/data"}}{{.Name}}{{end}}{{end}}')
22+
23+
if [ -z "${VOLUME_NAME}" ]; then
24+
echo "error: could not find postgres data volume for container '${CONTAINER_NAME}'"
25+
exit 1
26+
fi
27+
28+
echo "Backing up postgres data from volume '${VOLUME_NAME}'..."
29+
30+
# Create backup directory if it doesn't exist
31+
BACKUP_DIR="$(dirname "${BACKUP_PATH}")"
32+
mkdir -p "${BACKUP_DIR}"
33+
34+
# Backup the volume data
35+
docker run --rm \
36+
-v "${VOLUME_NAME}":/data \
37+
-v "${BACKUP_DIR}":/backup \
38+
alpine \
39+
tar czf "/backup/$(basename "${BACKUP_PATH}")" -C /data .
40+
41+
echo "Backup completed: ${BACKUP_PATH}"

scripts/run/engine-postgres.sh

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,21 @@ fi
1111

1212
if ! nc -z localhost 5432 >/dev/null 2>&1; then
1313
echo "Postgres is not reachable at localhost:5432."
14-
echo "Hint: run scripts/dev/run-postgres.sh to start the local Postgres container."
15-
exit 1
14+
echo "Starting postgres container..."
15+
"${SCRIPT_DIR}/postgres.sh"
16+
17+
echo "Waiting for postgres to be ready..."
18+
for i in {1..30}; do
19+
if nc -z localhost 5432 >/dev/null 2>&1; then
20+
echo "Postgres is ready!"
21+
break
22+
fi
23+
if [ $i -eq 30 ]; then
24+
echo "error: postgres did not become ready in time"
25+
exit 1
26+
fi
27+
sleep 1
28+
done
1629
fi
1730

1831
cd "${REPO_ROOT}"

scripts/run/nuke-postgres.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
CONTAINER_NAME="rivet-engine-postgres"
5+
6+
echo "Nuking postgres container and data..."
7+
8+
# Get the volume name before removing the container
9+
VOLUME_NAME=""
10+
if docker ps --all --format '{{.Names}}' | grep -qw "${CONTAINER_NAME}"; then
11+
VOLUME_NAME=$(docker inspect "${CONTAINER_NAME}" --format '{{range .Mounts}}{{if eq .Destination "/var/lib/postgresql/data"}}{{.Name}}{{end}}{{end}}' 2>/dev/null || true)
12+
fi
13+
14+
# Stop and remove container
15+
if docker ps --all --format '{{.Names}}' | grep -qw "${CONTAINER_NAME}"; then
16+
echo "Stopping and removing container '${CONTAINER_NAME}'..."
17+
if docker ps --format '{{.Names}}' | grep -qw "${CONTAINER_NAME}"; then
18+
docker stop "${CONTAINER_NAME}" >/dev/null 2>&1 || true
19+
fi
20+
docker rm "${CONTAINER_NAME}" >/dev/null 2>&1 || true
21+
echo "Container removed."
22+
else
23+
echo "Container '${CONTAINER_NAME}' not found."
24+
fi
25+
26+
# Remove volume if it exists
27+
if [ -n "${VOLUME_NAME}" ]; then
28+
echo "Removing volume '${VOLUME_NAME}'..."
29+
docker volume rm "${VOLUME_NAME}" >/dev/null 2>&1 || true
30+
echo "Volume removed."
31+
fi
32+
33+
echo "Postgres nuked successfully!"

scripts/run/restore-postgres.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
CONTAINER_NAME="rivet-engine-postgres"
5+
POSTGRES_IMAGE="postgres:17"
6+
7+
if [ $# -ne 1 ]; then
8+
echo "Usage: $0 <backup-path>"
9+
echo "Example: $0 /path/to/backup/postgres-backup.tar.gz"
10+
exit 1
11+
fi
12+
13+
BACKUP_PATH="$1"
14+
15+
if [ ! -f "${BACKUP_PATH}" ]; then
16+
echo "error: backup file '${BACKUP_PATH}' not found"
17+
exit 1
18+
fi
19+
20+
# Stop and remove existing container if it exists
21+
if docker ps --all --format '{{.Names}}' | grep -qw "${CONTAINER_NAME}"; then
22+
echo "Stopping and removing existing container '${CONTAINER_NAME}'..."
23+
if docker ps --format '{{.Names}}' | grep -qw "${CONTAINER_NAME}"; then
24+
docker stop "${CONTAINER_NAME}" >/dev/null 2>&1 || true
25+
fi
26+
docker rm "${CONTAINER_NAME}" >/dev/null 2>&1 || true
27+
fi
28+
29+
# Create a new volume
30+
echo "Creating new volume..."
31+
VOLUME_NAME=$(docker volume create)
32+
echo "Created volume: ${VOLUME_NAME}"
33+
34+
# Restore the data to the new volume
35+
echo "Restoring data from '${BACKUP_PATH}'..."
36+
BACKUP_DIR="$(dirname "${BACKUP_PATH}")"
37+
docker run --rm \
38+
-v "${VOLUME_NAME}":/data \
39+
-v "${BACKUP_DIR}":/backup \
40+
alpine \
41+
tar xzf "/backup/$(basename "${BACKUP_PATH}")" -C /data
42+
43+
# Create and start the container
44+
echo "Starting container '${CONTAINER_NAME}'..."
45+
docker run \
46+
--detach \
47+
--name "${CONTAINER_NAME}" \
48+
--publish 5432:5432 \
49+
--env POSTGRES_PASSWORD=postgres \
50+
--env POSTGRES_USER=postgres \
51+
--env POSTGRES_DB=postgres \
52+
-v "${VOLUME_NAME}":/var/lib/postgresql/data \
53+
"${POSTGRES_IMAGE}"
54+
55+
echo "Restore completed successfully!"
56+
echo "Container ID: $(docker ps -q -f name=${CONTAINER_NAME})"

0 commit comments

Comments
 (0)