Add db:backup and db:restore npm scripts
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -27,3 +27,6 @@ next-env.d.ts
|
|||||||
|
|
||||||
# uploads
|
# uploads
|
||||||
/public/uploads/
|
/public/uploads/
|
||||||
|
|
||||||
|
# db backups
|
||||||
|
/backups/
|
||||||
|
|||||||
@@ -15,7 +15,9 @@
|
|||||||
"db:migrate:deploy": "prisma migrate deploy",
|
"db:migrate:deploy": "prisma migrate deploy",
|
||||||
"db:seed": "tsx prisma/seed.ts",
|
"db:seed": "tsx prisma/seed.ts",
|
||||||
"db:studio": "prisma studio",
|
"db:studio": "prisma studio",
|
||||||
"db:reset": "prisma migrate reset --force"
|
"db:reset": "prisma migrate reset --force",
|
||||||
|
"db:backup": "sh scripts/db-backup.sh",
|
||||||
|
"db:restore": "sh scripts/db-restore.sh"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@hookform/resolvers": "^3.9.1",
|
"@hookform/resolvers": "^3.9.1",
|
||||||
|
|||||||
24
scripts/db-backup.sh
Executable file
24
scripts/db-backup.sh
Executable file
@@ -0,0 +1,24 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Usage: ./scripts/db-backup.sh [output-file]
|
||||||
|
# Uses DATABASE_URL from environment (or .env if present).
|
||||||
|
# Example — local: DATABASE_URL="postgresql://bonnamoon@localhost:5432/moonbase_dev" ./scripts/db-backup.sh
|
||||||
|
# Example — remote: ssh into NAS and run via docker exec (see README)
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# Load .env if present and DATABASE_URL not already set
|
||||||
|
if [ -z "$DATABASE_URL" ] && [ -f ".env" ]; then
|
||||||
|
export $(grep -v '^#' .env | grep DATABASE_URL | xargs)
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "$DATABASE_URL" ]; then
|
||||||
|
echo "Error: DATABASE_URL is not set."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
OUTFILE="${1:-backups/moonbase-$(date +%Y%m%d-%H%M%S).sql}"
|
||||||
|
mkdir -p "$(dirname "$OUTFILE")"
|
||||||
|
|
||||||
|
echo "Backing up to $OUTFILE ..."
|
||||||
|
pg_dump "$DATABASE_URL" --no-owner --no-privileges -f "$OUTFILE"
|
||||||
|
echo "Done. $(du -sh "$OUTFILE" | cut -f1) written to $OUTFILE"
|
||||||
35
scripts/db-restore.sh
Executable file
35
scripts/db-restore.sh
Executable file
@@ -0,0 +1,35 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Usage: ./scripts/db-restore.sh <backup-file>
|
||||||
|
# Uses DATABASE_URL from environment (or .env if present).
|
||||||
|
# WARNING: drops and recreates the public schema — all existing data is replaced.
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
if [ -z "$1" ]; then
|
||||||
|
echo "Usage: $0 <backup-file.sql>"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
INFILE="$1"
|
||||||
|
|
||||||
|
if [ ! -f "$INFILE" ]; then
|
||||||
|
echo "Error: file not found: $INFILE"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Load .env if present and DATABASE_URL not already set
|
||||||
|
if [ -z "$DATABASE_URL" ] && [ -f ".env" ]; then
|
||||||
|
export $(grep -v '^#' .env | grep DATABASE_URL | xargs)
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "$DATABASE_URL" ]; then
|
||||||
|
echo "Error: DATABASE_URL is not set."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Restoring from $INFILE ..."
|
||||||
|
echo " -> dropping and recreating public schema..."
|
||||||
|
psql "$DATABASE_URL" -c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;"
|
||||||
|
echo " -> loading backup..."
|
||||||
|
psql "$DATABASE_URL" -f "$INFILE" -q
|
||||||
|
echo "Done."
|
||||||
Reference in New Issue
Block a user