21 lines
779 B
TypeScript
21 lines
779 B
TypeScript
import { spawn } from 'node:child_process'
|
|
const source = process.env.DATABASE_URL,
|
|
destination = process.argv[2]
|
|
if (!source || !destination) throw new Error('DATABASE_URL and backup output path required')
|
|
const url = new URL(source),
|
|
child = spawn('pg_dump', ['--format=custom', '--file', destination], {
|
|
stdio: ['ignore', 'inherit', 'inherit'],
|
|
env: {
|
|
...process.env,
|
|
PGHOST: url.hostname,
|
|
PGPORT: url.port || '5432',
|
|
PGUSER: decodeURIComponent(url.username),
|
|
PGPASSWORD: decodeURIComponent(url.password),
|
|
PGDATABASE: url.pathname.slice(1),
|
|
},
|
|
})
|
|
await new Promise<void>((resolve, reject) => {
|
|
child.once('error', reject)
|
|
child.once('exit', (code) => (code === 0 ? resolve() : reject(new Error('pg_dump failed'))))
|
|
})
|