Skip to content

Instantly share code, notes, and snippets.

@mmgroner
Last active April 22, 2025 19:46
Show Gist options
  • Save mmgroner/699f798f88c6231d85c52e5f1731f74d to your computer and use it in GitHub Desktop.
Save mmgroner/699f798f88c6231d85c52e5f1731f74d to your computer and use it in GitHub Desktop.
const { exec } = require("child_process");
const path = require("path");
const dbUrl = process.argv[2];
const certPath = process.argv[3];
if (!dbUrl) {
console.error(`No DB URL given`);
help();
}
if (!certPath) {
console.error(`No cert path given`);
help();
}
const url = new URL(dbUrl);
const today = new Date();
const outputFile = `backup-${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()}.dump`;
const command = `pg_dump "port=${url.port || 5432} dbname=${url.pathname.slice(1)} host=${url.hostname} user=${url.username} password=${url.password} sslrootcert=${certPath}" --format=custom --schema=public --exclude-table=lpr_capture_record -f ${outputFile}`;
console.log(`Executing: ${command}`);
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing pg_dump: ${error.message}`);
process.exit(2);
}
if (stderr) {
console.error(`pg_dump stderr: ${stderr}`);
}
console.log(`Database dump saved to ${outputFile}`);
console.log(`To restore, use the following command:`);
console.log(`pg_restore --no-owner --no-privileges --disable-triggers --jobs=4 -d <your_target_db> ${outputFile}`);
});
function help() {
console.log(`Usage: node staging-dump.js <postgres-db-url> <cert-path>`);
process.exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment