I want to create a postgresql database backup with a .bat file on Windows
I tried with this code:
#echo off
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
set datestr=%month%_%day%_%year%
echo datestr is %datestr%
set BACKUP_FILE=odoo_%datestr%.backup
echo backup file name is %BACKUP_FILE%
SET PGPASSWORD=openpgpwd
echo on
bin\pg_dump -i -h localhost -p 5432 -U openpg -F c -b -v -f %BACKUP_FILE% formation
But I received nothing.
And with this code:
#echo off
SET PG_BIN="C:\Program Files\OpenERP 7.0-20150818\PostgreSQL\bin\pg_dump.exe"
SET PG_HOST=localhost
SET PG_PORT=5432
SET PG_DATABASE=formation
SET PG_USER=openpg
SET PG_PASSWORD=openpgpwd
SET PG_PATCH=D:\odoo
SET FECHAYHORA=%date:/=%-%time:-0,8%
SET FECHAYHORA=%FECHAYHORA::=-%
SET FECHAYHORA=%FECHAYHORA: =0%
SET PG_FILENAME=%PG_PATH%\%PG_DATABASE%-%FECHAYHORA%.sql
%PG_BIN% - i -h %PG_HOST% -p %PG_PORT% -U %PG_USER% %PG_DATABASE% > %PG_FILENAME%
I have the same problem.
I want to use it later in the Windows Task Scheduler.
This answer was provided by the author:
I think this is the solution for all:
#echo Backup database %PG_PATH%%PG_FILENAME%
#echo off
SET PG_BIN="C:\Program Files\PostgreSQL\bin\pg_dump.exe"
SET PG_HOST=localhost
SET PG_PORT=5432
SET PG_DATABASE=motor_8
SET PG_USER=openpg
SET PG_PASSWORD=openpgpwd
SET PG_PATH=C:\OEM
SET FECHAYHORA=%date:/=%-%time:-0,8%
SET FECHAYHORA=%FECHAYHORA::=-%
SET FECHAYHORA=%FECHAYHORA: =0%
SET PG_FILENAME=%PG_PATH%\%PG_DATABASE%_%d%_%t%.sql
%PG_BIN% -h %PG_HOST% -p %PG_PORT% -U %PG_USER% %PG_DATABASE% > %PG_FILENAME%
#echo Backup Taken Complete %PG_PATH%%PG_FILENAME%
It is hardly surprising that your scripts don't work, because they contain typos all over. I don't claim that I found them all, but here is a list of those that I did spot:
First script:
There is no option -i for pg_dump.
Second script:
There is a bogus space in -i, but since that option doesn't exist and will cause an error, it should be removed.
You set PG_PATCH and reference PG_PATH.
The environment variable PG_PASSWORD is not recognized by libpq. Remove the underscore.
You can figure out all this if you read the error messages you undoubtedly get. You should also always include such error messages in questions like this.
Related
I have a batch file that I'm attempting to use to do database backups. In order to try and make this reusable I have parameterised most of the inputs such as:
Database
User
Password
Schema
and so on.
This is what it looks like
#echo on
SET dbPath=%~1
SET dbHost=%~2
SET dbName=%~3
SET dbUser=%~4
SET dbPassword=%~5
SET backupFile=%~6
SET schema=%~7
SET databaseSQL=%~8
SET today=%DATE:~-4%%DATE:~4,2%%DATE:~7,2%
#REM -------------------------------------------------------------------------------------------
:Setup Logging
#REM -------------------------------------------------------------------------------------------
IF NOT EXIST "%dbPath%\logs\" mkdir "%dbPath%\logs"
CD %dbPath%\logs\
IF NOT EXIST restore_log_%schema%_%today%.txt (
type NUL > restore_log_%schema%_%today%.txt
)
SET logFile="%dbPath%\logs\restore_log_%schema%_%today%.txt"
#REM -------------------------------------------------------------------------------------------
:Restore schema
#REM -------------------------------------------------------------------------------------------
echo Started: %date% %time% >> %logFile%
cd ..
SET dbPassword=%dbPassword%
psql -h %dbHost% -p "PASSWORD" -U %dbUser% -d %dbName% -c "DROP SCHEMA IF EXISTS %schema%" >> %logFile%
psql -h %dbHost% -p "PASSWORD" -U %dbUser% -d "dbSUB2" < %backupFile% 2>&1|more >> %logFile%
echo Finished: %date% %time% >> %logFile%
However, when passing in %schema% within double quotes the value of the parameter is converted to lowercase, which is a problem as my tables follow the naming convention of tblTableName. This means the statement says correctly that it cannot find the table as it legitimately doesn't exist.
I have tried using single quotes but that turned out strangely. I also tried the :f flag to just run a separate file but then it did not recognise the variable, the only time it worked was when the file only contained SELECT :variable.
Ideally I'd like to use the first method but I'm unsure how to tell the psql command to not convert my database schema to lowercase.
I'm trying to create an automated backup in Postgresql using below link, but I don't know where to find the needed dll, I'm stuck here. Can't proceed to next instruction because of this. Can anyone knows how to do it? Need help please.
https://wiki.postgresql.org/wiki/Automated_Backup_on_Windows
comerr32.dll
gssapi32.dll
k5sprt32.dll
krb_32.dll
libeay32.dll
libiconv2.dll
libpq.dll
Microsoft.VC80.CRT.manifest
msvcm80.dll
msvcp80.dll
msvcr80.dll
pg_dump.dll
ssleay32.dll
zlib1.dll
Here's batch file script:
#echo off
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
set datestr=%month%_%day%_%year%
echo datestr is %datestr%
set BACKUP_FILE=<backup_name_>_%datestr%.backup
echo backup file name is %BACKUP_FILE%
SET PGPASSWORD=<password>
echo on
bin\pg_dump -i -h <localhost> -p 5432 -U <postgres> -F c -b -v -f %BACKUP_FILE% <db_name>
Is there missing syntax?
When manually executed error shows:
bin\pg_dump: illegal option -- i
I execute something like this
pg_dump.exe -h %SERVER% -p 5432 -U postgres -Fc -d %basedatos% -v -f %filebackup%
This variables replace value respective.
I've been studying how to use pg_dump and creating a batch file in order to backup the database of my PostgreSQL since the pgAgent is not appearing on the pgAdmin 3 that Odoo installed on my machine. We all know that there's a method in backing up the database in it's UI at 'http://localhost:8069/web/database/manager'. I was able to backup using pg_dump but it's giving me a file which is 300% the size of backing up the normal way. Is there anyone who knows the command (pg_dump) where i can output the same size and file just as the Odoo's UI does?
Here is my batch file:
cd\
cd "Program Files\Odoo 9.0-20161004\PostgreSQL\bin"
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
set dbname=WHpingcon_%fullstamp%
pg_dump -h localhost -p 5432 --format c --file "C:/%dbname%.sql" -U openpg --inserts sample
I used '--inserts' because i also need the whole data of database and restore it the normal way (UI). Everytime i remove '--inserts' it's giving me an error when i'm restoring it. i checked the file and it does not escape special characters. Thanks all!
I'm running this script to backup a database. It works but I would like to add a secondary backup directory to get the output file in two different directories. Any help is appreciated..)
REM Set paths and database info
set PGPASSWORD='postgre'
set PGuser='postgres'
set PGinstance='e3238s'
set PGdump='F:/Program Files/PostgreSQL/9.5/bin\pg_dump'
set BackupDir='F:/backup'
set BackupName='DB_BK'
REM Build a datetime stamp
set DateTime='%DATE:~-4%_%DATE:~-7,2%_%DATE:~-10,2%_%TIME:~0,2%_%TIME:~3,2% _%TIME:~6,2%'
echo %DateTime%
REM Backup
"%PGdump%" -f "%BackupDir%\%BackupName%%DateTime%.sql" --format plain -U %PGuser% -v %PGinstance%
This is quite unrelated to Postgres, and more related to how you'd output to two files in the same run.
You could use something like this, which in your case, becomes:
REM Backup
"%PGdump%" --format plain -U %PGuser% -v %PGinstance% > "%BackupDir%\%BackupName%%DateTime%.sql" & type "%BackupDir%\%BackupName%%DateTime%.sql" >> "%SecondaryBackupDir%\%BackupName%%DateTime%.sql"
This question already has answers here:
How do I specify a password to 'psql' non-interactively?
(11 answers)
Closed 8 years ago.
Hello I write simple batch script to backup postgeSQL databases, but I find one strange problem whether the pg_dump command can specify a password?
There is batch script:
REM script to backup PostgresSQL databases
#ECHO off
FOR /f "tokens=1-4 delims=/ " %%i IN ("%date%") DO (
SET dow=%%i
SET month=%%j
SET day=%%k
SET year=%%l
)
SET datestr=%month%_%day%_%year%
SET db1=opennms
SET db2=postgres
SET db3=sr_preproduction
REM SET db4=sr_production
ECHO datestr is %datestr%
SET BACKUP_FILE1=D:\%db1%_%datestr%.sql
SET FIlLENAME1=%db1%_%datestr%.sql
SET BACKUP_FILE2=D:\%db2%_%datestr%.sql
SET FIlLENAME2=%db2%_%datestr%.sql
SET BACKUP_FILE3=D:\%db3%_%datestr%.sql
SET FIlLENAME3=%db3%_%datestr%.sql
SET BACKUP_FILE4=D:\%db14%_%datestr%.sql
SET FIlLENAME4=%db4%_%datestr%.sql
ECHO Backup file name is %FIlLENAME1% , %FIlLENAME2% , %FIlLENAME3% , %FIlLENAME4%
ECHO off
pg_dump -U postgres -h localhost -p 5432 %db1% > %BACKUP_FILE1%
pg_dump -U postgres -h localhost -p 5432 %db2% > %BACKUP_FILE2%
pg_dump -U postgres -h localhost -p 5432 %db3% > %BACKUP_FILE3%
REM pg_dump -U postgres -h localhost -p 5432 %db4% > %BACKUP_FILE4%
ECHO DONE !
Please give me advice
Regards Mick
edited to reflect information in comments
There is no option to feed the password form command line. You need to include the -w or --no-password switch and create a file that will contain the password for the connection. It is a text file in the form
hostname:port:database:username:password
By default this file will be readed from %APPDATA%\postgresql\pgpass.conf, but its location can be changed setting the environment variable PGPASSFILE to the path to the file to use.
You can find more information in the product documentation