File . bat to generate backup and Restore in Postgresql

Asked

Viewed 6,281 times

1

I am creating . bat files to backup and restore a database in Postgresql using windows 10 and pg 9.4

I perform the following command to perform the backup

set PGUSER=postgres
set PGPASSWORD=postgres123

"C:/Program Files/PostgreSQL/9.4/bin\"pg_dump.exe --host localhost --port 5432 --format custom --blobs --verbose --file "D:\bkp.sql" "dbsibcom"

it works perfectly.

And to perform the Store I have the following command

set PGUSER=postgres

set PGPASSWORD=postgres123
"C:/Program Files/PostgreSQL/9.4/bin\pg_restore.exe -i -h localhost -p 5432 -c -d "testrestore" -v " D:\bkp.sql"

I create the bat file and try to restore, it just opens and closes the dos screen quickly and doesn’t work.

There is something wrong or another method of doing Restore ?

I have to give some permission for pg to have access to my backup file or something like that?

Editing


I also tried these commands to try to perform Restore and it doesn’t work

set PGUSER=postgres
set PGPASSWORD=postgres123 

C:/Program Files/PostgreSQL/9.4/bin\pg_restore.exe --host localhost --port 5432 --username "postgres" --dbname "testrestore"   --verbose "D:\bkp.sql"
  • is printing some error ? because it has a backslash ai /, try all in the same direction, the way is right ?

  • try this way with the quotes "C:/Program Files/Postgresql/9.4/bin pg_restore.exe"

  • Hello, do not print any error, just blink dos screen and finish without performing anything

3 answers

6


Considering:

  • The name of the database as: minha_database
  • The backup file as: C:\bkp\backup_database.dump
  • Installation of Postgresql 9.1 in C:\Program Files (x86)\PostgreSQL

To back up:

C:\Progra~2\PostgreSQL\9.1\bin\pg_dump -h servidor -p 5432 -U postgres --inserts -c -f C:\bkp\backup_database.dump minha_database

To do the Store:

C:\Progra~2\PostgreSQL\9.1\bin\psql -U postgres -d minha_database -f C:\bkp\backup_database.dump
  • You can keep command set PGPASSWORD=postgres123 not to ask for the password and automate the execution.

  • You can put a commando pause at the end of .bat to see which error occurs.

  • In your environment, the bin folder path will be: C:\Progra~1\PostgreSQL\9.4\bin\. Use this path to place commands.

  • The dump file will be generated in sql format, and in plain text, without any protection / encryption.

  • why is psql used and not pg_restore? pg_restore is not used for these cases. I just tried to implement the backup, and it didn’t work, the same error occurs just blinks the screen and doesn’t work. Look how I am creating the file . bar set PGPASSWORD=postgres123


C:\Program Files\PostgreSQL\9.4\bin\pg_dump -h localhost -p 5432 -U postgres --inserts -c -f D:\bkp2.dump basedados

  • And with Restore the same error occurred. If I put this path in the address of explorer C: Program Files Postgresql 9.4 bin psql it opens the . pg exe normally but when I create the file with the following information set PGPASSWORD=postgres123


C:\Program Files\PostgreSQL\9.4\bin\psql -U postgres -d testrestore -f D:\bkp.dump The same error occurs

  • Could you tell me which command to pause?

  • 1

    kkkkk, That’s right, there was an error on account of C: Program Files, he said that "C: Program" was not recognized. thank you so much for your help !

  • When backing up a bank, does your . dump file come with special characters? when I run Store appears several special characters and invalid commands. thus invalidating my Store

  • But my question is, if I backup a database in . sql and open it in a text editor I can see all the data? or does the data appear with special characters as a form of protection? why here in my case, my database is in ENCODING = 'UTF8', but if I open my backup file generated in Notepad++ for example it comes with some characters in this format ``Zë¿ne=1~:Ò Éeýo ³a25ºPL... &` e varios nulls

  • Right Thanks for the help, there must be something wrong with my Encoding when I take the backup then

  • blz, I changed the reply by adding the comments information. and I will delete the unnecessary comments. vlw

Show 3 more comments

-1

Backup script

Windows 

@Echo Off
 @For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
 Set DayW=%%A
 Set Day=%%B
 Set Month=%%C
 Set Year=%%D
 Set All=%%A_%%B_%%C_%%D
 )
 @For /F "tokens=1,2,3 delims=:,. " %%A in ('echo %time%') do @(
 Set Hour=%%A
 Set Min=%%B
 Set Sec=%%C
 Set Allm=%%A.%%B.%%C
 )
 @For /F "tokens=3 delims=: " %%A in ('time /t ') do @(
 Set AMPM=%%A
 )
set datestr=%All%%Allm%%AMPM%
echo datestr is %datestr%

set BACKUP_FILE=E:\BACKUP\WMSBACKUP_%datestr%.backup
echo backup file name is %BACKUP_FILE%
SET PGPASSWORD=suasenha
echo on
cd "\Program Files\PostgreSQL\10\bin\"
pg_dump.exe -h localhost -U postgres -p 5432 wms > %BACKUP_FILE% 

pause

-2

@echo off

rem Coloca o nome do banco aqui

set banco=

REM Senha da base de dados

SET PGPASSWORD=

rem Caminho da pasta do backup

set pastabk=D:\backup_sistema

rem Caminho do pg_dump

set pg_dump=C:\Program Files\PostgreSQL\9.3\bin

rem Unidade do HD EXTERNO

set pendrive=G:\

rem Nome do Servidor

set servidor=

if not exist %pastabk% (mkdir %pastabk%)

color 1f

Title BACKUP SISTEMA

echo ----------------------------------------------------------------------------

echo -----------------------Backup de banco de dados-----------------------


echo ----------------------------------------------------------------------------

echo.

set dia=%date:~0,2%%date:~3,2%%date:~6,4%_%time:~0,2%%time:~3,2%

set dia=%dia: =%

echo Banco de Dados.: %banco%

echo.

cd\

CD %pg_dump%

pg_dump.exe -h %servidor% -p 5432 -U postgres -F c -b -v -f 
"%pastabk%\%banco%%dia%.backup" %banco%

echo.

echo ----------------------------------------------------------------------------

echo ----------------------------------------------------------------------------

echo.


if exist %pastabk%\%banco%%dia%.backup (

    echo Backup Concluido... 

    echo Criado no arquivo: "%pastabk%\%banco%%dia%.backup".

    echo.

    if exist %pendrive% (

        echo Copiando arquivo para dispositivo removivel.

        if not exist %pendrive%\BancosBk\ (md %pendrive%BancosBk\)
 
        copy "%pastabk%\%banco%%dia%.backup" 
"%pendrive%\BancosBk\%banco%%dia%.backup"

        echo.

        echo Backup copiado para dispositivo removivel.

        echo Copiando em %pendrive%\BancosBk\%banco%%dia%.backup

    ) else (echo Dispositivo removivel nao encontrado.)

) else (echo Não foi possivel concluir o backup.)


echo.

pause
  • NOTE: COPY FILES FROM BIN FOLDER TO SYSTEM32

  • You can edit the reply to include the text in the comment.

Browser other questions tagged

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