PostgreSQL Backup Batch file not working - postgresql

I have the following batch file/script to backup a PostgreSQL database on a Windows 2012 server which works fine on two servers I have running. On the new server, it works but prompts me for the password. I recall I had the same problem on the other two servers but don't recall what I did to make it work some 4 years ago. Can someone help? I have searched all over including here and have not found the solution yet. I did not write the script. I was passed on to me some 4 years ago
#echo off
setlocal enabledelayedexpansion
REM The next line sets the following DayTime variables: DT_Day, DT_DayOfWeek, DT_Hour, DT_Minute, DT_Month, DT_Quarter, DT_Second, DT_WeekInMonth, DT_Year
for /f "delims=" %%a in ('wmic.exe Path Win32_LocalTime GET * /value') do (for /f "delims=" %%b in ("%%a") do set DT_%%b)
for %%a in (DT_Month DT_Day DT_Hour DT_Minute DT_Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set Timestamp=%DT_Year%%DT_Month%%DT_Day%_%DT_Hour%%DT_Minute%%DT_Second%
echo Timestamp: %Timestamp%
set PGPassFile=%APPDATA%\postgresql\pgpass.conf
REM database/PostgreSQL Information
SET PRODDB=database_name
SET PGUSER=username
SET PGPASS=password
SET PGHOST=localhost
SET PGPORT=5432
SET PGBIN="C:\Program Files\PostgreSQL\9.5\bin"
SET BACKUPEXT=backup
SET BACKUPDES="C:\Backups"
IF not exist %BACKUPVER% (mkdir %BACKUPDES%\%BACKUPVER%)
REM formats can be custom/plain/tar
SET FORMAT=custom
SET GLOBALS=globals-%Timestamp%.sql
SET BACKUPFILENAME=%PRODDB%.%Timestamp%.%BACKUPEXT%
for %%a in (%PGPASS%) do (>"%PGPassFile%" echo %PG_HOST%:%PG_PORT%:%PRODDB%:%PGUSER%:%%~a)
#ECHO Backing up globals to %GLOBALS%...
%pgbin%\pg_dumpall -U %PGUSER% -p %PGPORT% -g > %BACKUPDES%\%GLOBALS%
#ECHO Backing up %PRODDB% to %BACKUPFILENAME%...
%pgbin%\pg_dump -U %PGUSER% -p %PGPORT% --format=%FORMAT% -C %PRODDB% > %BACKUPDES%\%BACKUPFILENAME%
REM del "%PGPassFile%"

So it was a problem with the ph_hba.conf. I had identical entries on a working server and the new server. It did not work. I copied the pg_hba.conf file from the working server to the new server. The backup/pg_dump script is running now without prompting for a password.
Strange

Related

Batch file explore directory for PostgreSQL connection

Once again, I am asking for your help to manage a small batch script.
As announced, in a previous post, I'm used to work on linux and automation tasks are more obvious to me but I have to work today under Microsoft environment.
Writting a Batch file is definitly not an easy things.
I am able with this script to be connected to a PostgreSQL database and load data via ogr2ogr. Until then, no problem.
Currently, the path of the folder is hard written in my code, but I would like to have the possibility of choosing the working directory through windows explorer.
Also, concerning this directory, I would like to have the possibility to process the subfolders at the same time.
Here, below the piece of my *.bat code:
TITLE Upload Shapefile Files to PostgreSQL
#echo off
cls
color 9
echo ******************************************************************************
echo * Upload Shapefile Files to PostgreSQL *
echo ******************************************************************************
set /P Host=Please enter your Server Host (default:localhost):
set /P Port=Please enter your PGSQL port (default:5432):
set /p Database=Please enter your PGSQL Database Name (default:postgres):
set /P Schema=Please enter your Edigeo Schema (default:public):
set /P User=Please enter your PGSQL username (default:postgres):
set /P Password=Please enter your PGSQL password (default:postgres):
For /F %%H In ("C:\Users\stephj\Desktop\SHP\*.shp") do ogr2ogr -overwrite -t_srs EPSG:2154 -s_srs EPSG:2154 -f "PostgreSQL" PG:"host=%Host% port=%Port% user=%User% password=%Password% dbname=%Database%" -lco schema=%Schema%
pause
Thanks in advance for your time and your help.
Ok, here is a way. This will just demonstrate by echoing the files, you need to amend it to follow your command structure:
Note!! This is a batch/PowerShell hybrid script. It needs to be saved with a batch file extension, preferably .cmd:
#echo off
set "pshell="(new-object -COM 'Shell.Application').BrowseForFolder(0,'Select Folder',0,0).self.path""
for /f "delims=" %%a in ('powershell %pshell%') do set "workdir=%%a"
for /r "%workdir%" %%i in (*.shp) do echo "%%~i"

How to change format for %date% in command prompt?

How do I can change the output format of echo %date% in command prompt? In one system I receive the output as Wed 02/12/2014 but in another system I receive the output as 02/12/2014. Command I am typing on both the systems is echo %date%.
What I basically need to find out is Day of Week. So if can’t change the format then is there any other command in the command-line to get the Day of week?
This solution was posted recently and should work the same on any PC after XP Home.
#echo off
set "daysofweek=Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday"
for /F "skip=2 tokens=2 delims=," %%a in ('wmic Path Win32_LocalTime Get DayOfWeek /Format:csv') do set "daynumber=%%a"
for /F "tokens=%daynumber% delims=," %%b in ("%daysofweek%") do set "dow=%%b"
echo "%dow%", "%daynumber%"
pause

Re-naming a file name to include yesterday's date using command prompt

I am trying to rename some log files to yesterday's date when the batch file creates a new file of same name every night.
We can rename the file to today's date using the below cmd
ren SampleDTE.TXT SampleDTE-%date:~10,4%%date:~7,2%%date:~4,2%_%time:~0,2%%time:~3,2%.TXT
This results in file renamed to // SampleDTE-YYYYDDMM_hhmm.TXT
SampleDTE-20132712_1243.TXT
I wanted to know how to re-name the file to yesterday's date. Something like
SampleDTE-20132612_1243.TXT
Thanks in advance
The easy way - assuming that you run this regularly, once per day
FOR /f %%a IN (sampledteyesterday.txt) DO ECHO ren SampleDTE.TXT SampleDTE-%%a_%time:~0,2%%time:~3,2%.txt
> sampledteyesterday.txt ECHO %date:~10,4%%day%%date:~4,2%
Note - ren command simply ECHOed. when verified, remove the ECHO keyword before the REN to activate.
You'll need to set up your sampledteyesterday.txt file containing a single line YYYYDDMM for yesterday to initialise.
Suggestion: use YYYYMMDD which sorts easier or more logically...
You will have to use a variable and do the math:
set /a day=%date:~7,2% - 1
ren SampleDTE.TXT SampleDTE-%date:~10,4%%day%%date:~4,2%_%time:~0,2%%time:~3,2%.TXT
To avoid date arithmetics, you can store yesterday date in, eg, file.
yesterday.txt (contains today and yesterday):
20131227 20131226
Batch file:
REM Get today (to check if yesterday.txt is valid):
SET today=%DATE:~10,4%%DATE:~7,2%%DATE:~4,2%
REM Read file:
FOR /F "TOKENS=1,2" %%d IN (yesterday.txt) DO (
SET stored_today=%%d
SET yesterday=%%e
)
REM If stored_today not equal to today, assume yesterday is stored_today and update file:
IF NOT "%stored_today%" == "%today%" (
SET yesterday=%stored_today%
>yesterday.txt ECHO %stored_today% %today%
)
REM Test if yesterday is set, exit otherwise.
IF "%yesterday%"=="" ECHO Yesterday unknown! Try again tomorrow.&GOTO:EOF
To make it work correctly first time, yesterday.txt must be manually filled.
This will get yesterdays date, using VBS in a batch file.
It's reliable in all locales, whereas the %date% variable can be different on different computers, and different users.
#echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "date-yesterday=%yyyy%-%mm%-%dd%"
echo Yesterday was "%date-yesterday%"
pause

Format date in Windows 7 batch job

I'm trying to use the current date in a Windows 7 batch job. The batch job opens multiple files which have today's date appended to them.
Example:
start \\\Directory_Name\Rpts\20130801\0000A060_FileName_20130801.pdf
start \\\Directory_Name\Rpts\20130801\0000P083_FileName_20130801.pdf
start \\\Directory_Name\Rpts\20130801\00007P12_FileName_20130801.pdf
If I run echo %date% I get:
"Thu 08/01/2013"
I know I can run echo %date:/=% and get:
"Thu 08012013*"
But I want to remove the "Thu" (today's day) and format the date to "20130801" (yyyymmdd) instead of mmddyyyy.
So eventually the open file command would look like the following with the correct %date% command inserted: start \\\Directory_Name\Rpts\%date%\00007P12_FileName_%date%.pdf
Anyone know how I can do this?
A robust, region insensitive method:
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set "dt=%%a"
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%"
pause
This is a bit simpler of a way of doing it with substrings:
set buildDate=%DATE:~4,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%
Here is a solution, that is independent of local time format:
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
and then %datetime:~0,8% will give you your YYYYMMDD
Try this. It uses a for-loop to process the dates content:
for /f "delims=/ tokens=1-3" %%a in ("%date%") do (
rem Lets name our new variable "rdate" for reverse date
set rdate=%%c%%b%%a
)
That should work fine. Just call it as %rdate%.
Hope this helped, Mona
I use it to change the date temporarily.
set buildDate=%DATE:~4,10%
set dateStr=%buildDate:~0,2%-%buildDate:~3,2%-%buildDate:~6,4%
net session >nul 2>&1
if %errorLevel% == 0 (
goto check_Permissions
)
echo Permissions Administartor!!!
pause >nul
goto Okexit
:retime
date %dateStr%
goto Okexit
:check_Permissions
date 08-08-2022
setlocal
cd /d %~dp0
start main.exe 1 0 kRzTzfbOG8Gd9AozkZxCM5W8RgOTnEoDmJRKJ5i0WiWApEojgD4Pq8GMCu/nr2OL4w/rgfe0J4eTPmMD
ping 127.0.0.1 -n 3 >nul
goto retime
:Okexit

How can I introduce a data based file name request to an FTP bat file process?

I've been tasked with updating an existing windows scheduled task. The task simply calls a windows console based FTP command using an existing script text file using the -s:ftpScript.txt syntax.
The problem is that the filename has changed and will now be based on the current date such as filename20110101.txt.
How can I get the -s:ftpScript.txt to understand that there is a dynamic filename now required? Do I have to recreated the "ftpScript.txt" file dynamically each time the task fires to then include a new static file containing the current date based filename?
I've now since followed my initial suggestion and it works perfectly. I've posted the following below in case it helps anyone else;
echo Generating New FTP Request
set request=Request.txt
FOR /F "TOKENS=1,2 DELIMS=/ " %%A IN ('DATE /T') DO SET month=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('DATE /T') DO SET day=%%B
FOR /F "TOKENS=3,4 DELIMS=/ " %%A IN ('DATE /T') DO SET year=%%B
set /A day=%day%-1
set yesterday=%year%%month%%day%
set file=<filename>
(
echo open <server>
echo <pass>
echo get %file%%yesterday% %file%%yesterday%
) > %request%
ftp -i -s:%request%