Restore dump with nodejs

Asked

Viewed 107 times

0

Precise restore a postgres database nodding...

My routine downloads the latest backup (custom dump) by ftp, and creates the database (without the structure).

Now I need to run this file I downloaded to restore the bank. But I don’t know how.

I thought it was just read the contents of the file and run as a query, but no. I also couldn’t find any lib or example for this.

I need examples or some idea of how to do this...

  • Windows or Linux?

  • For the 2 environments.

  • Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!

1 answer

0

In the Node.js you can run an external application. In this case we will use the module child_process to execute the psql and dump the contents into the new bank:

const { spawn } = require('child_process');
const { createReadStream } = require('fs');

const restaurar = (destino, origem) => {
  console.time('Despejando conteúdo.');
  // Cria o banco de dados
  const child = spawn('createdb', [destino]);

  child.on('exit', () => {
    const conteudo = createReadStream(origem);
    const psql = spawn('psql', [destino]);
    conteudo.pipe(psql.stdin);
    console.timeEnd('Despejando conteúdo.');
  });
};

child_process

The child_process module provides the Ability to Spawn Child processes in a Manner that is similar, but not identical, to popen(3).

In free translation:

The module child_process provides the ability to perform child processes in a similar but not identical way to popen(3).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.