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"
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 want to automate backup of PostgreSQL database using crontab in UNIX. I have tried but it will create 0 bytes backup.
My crontab entry is:
24 * * * * /home/desktop/myscript.sh
and my sh file contains the following code:
pg_dump -U PostgreSQL -d test > b.backup
It will create the file but the file is empty. Is there any solution? Is there any way to solve this question?
Don't assume that any environment variables are set in a cron job; be explicit:
/full/path/to/pg_dump -U postgres -d test > /full/path/to/b.backup
Look for mail in your inbox for failure reports from cron.
You must specify full path to pg_dump
#!/bin/bash
BKPDATE=$(date +%d.%m.%Y-%H:%M:%S)
cd /var/lib/pgsql/12/backups
/usr/pgsql-12/bin/pg_dump -Fc dl_db > DBNAME_$BKPDATE.dmp --verbose 2> LOG_$BKPDATE.log
or you must add PostgreSQL's bin directory to the path like below:
vi /var/lib/pgsql/.pgsql_profile
export PATH=$PATH:/usr/pgsql-12/bin
When I using this command to backup postgres database,the backup data echo to screen:
screen /usr/pgsql-9.6/bin/pg_dump -v -h prod-book-db -U postgres dolphin > ./dolphin-fulldb-backup-201904130913.bak
How to avoid it?When using this command :
/usr/pgsql-9.6/bin/pg_dump -v -h prod-book-db -U postgres dolphin > ./dolphin-fulldb-backup-201904130913.bak
This only echo backup log,do not contains content.
this is probably a bit late but there's an 'f' flag for this:
-f file
--file=file
Send output to the specified file. This parameter can be omitted for file based output formats, in which case the standard output is used. It must be given for the directory output format however, where it specifies the target directory instead of a file. In this case the directory is created by pg_dump and must not exist before.
So you can use it like this:
screen /usr/pgsql-9.6/bin/pg_dump -v -f dolphin-fulldb-backup-201904130913.bak -h prod-book-db -U postgres dolphin
I want to download dumped PostgreSQL databases from an Ubuntu 16.04 server.
sudo su - postgres
pg_dump my_db > backup_db
Search for the path yields the following:
ps auxw | grep postgres | grep -- -D
postgres 7311 0.0 0.0 293332 3384 ? S Mai04 0:39 /usr/lib/postgresql/9.5/bin/postgres -D /var/lib/postgresql/9.5/main -c config_file=/etc/postgresql/9.5/main/postgresql.conf
Yet I cannot find the dumped files there. What is the location of the dumped files?
$HOME of user postgres
pg_dump just echoes to stdout, unless you specify -f
-f file --file=file
Send output to the specified file. This parameter can be omitted for
file based output formats, in which case the standard output is used.
It must be given for the directory output format however, where it
specifies the target directory instead of a file. In this case the
directory is created by pg_dump and must not exist before.
(formatting mine)
so in your case file backup_db will be in same directory where you were running pg_dump my_db > backup_db
next time try specifying full path to know exact location
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.