I am looking for a solution - a script to automate the process of taking backup of a database in PostgreSQL. As of now I do it manually, that is by right clicking on the db and clicking the backup option.
I did some research and ended up with a script which solves the issue pretty much, ie:
#ECHO OFF
#setlocal enableextensions
#cd /d "%~dp0"
SET PGPATH=C:\PostgreSQL\9.4\bin\
SET SVPATH=d:\
SET PRJDB=Test
SET DBUSR=postgres
FOR /F "TOKENS=1,2,3 DELIMS=/ " %%i IN ('DATE /T') DO SET d=%%i-%%j-%%k
FOR /F "TOKENS=1,2,3 DELIMS=: " %%i IN ('TIME /T') DO SET t=%%i%%j%%k
SET pg_dump=%PRJDB%_%d%_%t%.backup
#ECHO OFF
%PGPATH%pg_dump -h localhost -p 5432 -U postgres %PRJDB% > %SVPATH%%pg_dump%
echo Backup Taken Complete %SVPATH%%pg_dump%
pause
It did take the backup, but the file generated was a sql file, though I did change the extension to .backup. As a result, if I need to restore the DB, and try to restore from the file generated it is not possible. Can someone please provide me with a solution to this problem.
Thanks in advance.
Following script can be used to get the Postgres backup with .backup extension
#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=%day%_%month%_%year%
echo datestr is %datestr%
set BACKUP_FILE=C:\Users\slan\Desktop\backup_test\DBNAME_%datestr%.backup
echo backup file name is %BACKUP_FILE%
SET PGPASSWORD=YOUR_PASSWORD
echo on
bin\pg_dump -i -h localhost -p 5432 -U postgres -F c -b -v -f %BACKUP_FILE% YOUR_DB_NAME
you must have PostgreSQL's bin folder(Ex. C:\PostgreSQL\9.4\bin) along with this script otherwise this will not work as you expect
To schedule the task you can use Windows Task scheduler, here is an example for how to ?? - How to schedule a Batch File to run automatically in Windows 10 / 8 / 7
You can use PGagent for scheduling batch it have multiple advantages over windows scheduler bat scripts
Related
I'm trying to create a batch file to execute all the scripts from a folder and print logs in a different folder and getting access id denied error. Below is my folder structure. Thanks in advance.
Scripts path - C:\project\Queries_Testing\Scripts
Logs - C:\project\Queries_Testing\logs
Batch file - C:\project\Queries_Testing\executeQueries.bat
Code from executeQueries.bat
#ECHO OFF
setlocal enabledelayedexpansion
set /p serverName=Enter DB Servername :
set /p dbName=Enter Database Name :
set /p userName=Enter Username :
set /p password=Enter password :
set /p scriptsPath=Enter Scripts Path :
set /p output=Enter path for output:
for %%G in (*.sql) do sqlcmd /S %serverName% /d %dbName% -U %userName% -P %password% -i"%%G" -o%output%\%%G.lng text**og
ECHO Finished!
pause
Here's an example script which uses the information I provide in my comments.
executeQueries.bat
#Echo Off
Set /P "serverName=Enter DB Server Name: "
Set /P "dbName=Enter Database Name: "
Set /P "usrName=Enter User Name: "
Set /P "password=Enter User Password: "
Set "scriptsPath=C:\project\Queries_Testing\Scripts"
Set "output=C:\project\Queries_Testing\logs"
For %%A In ("%scriptsPath%\*.sql"
) Do sqlcmd /S "%serverName%" /d "%dbName%" -U "%usrName%" -P "%password%" -i"%%A" -o"%output%\%%~nA.log"
Echo Finished!
Pause
The code above uses the provided locations for the scripts and logs directories with a standard Set command. Once you've verified that it works, you may revert back to the Set /P format as used in the 4 lines above them.Please note that your script does not provide any verification routines for the end users input information. If they input incorrect information, the sqlcmd will be run using it, and may be problematic or produce errors.
Please provide feedback accordingly; thank you.
I have a Batch file as below
for /f "tokens=1-3 delims=/" %%a in ("%date%") do md "%%a_%%b_%%c"
C:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump -uroot gs > c:\%date%\gs.sql
I create the Folder with current date and time as what I want to do, but how do I set the dump folder to insert it in the current date and time folder
The above file runs but stops when I try and direct the folder to the one I created
I get the following error
C:\Trevor>C:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump -uroot gs 04/14/2017\gs.sql 1>c:\Fri
Access is denied.
What I think you were meant to do was this:
FOR /F "TOKENS=1-3 DELIMS=/" %%A IN ("%DATE%") DO SET "MYDATE=%%A_%%B_%%C"
IF NOT EXIST "C:\%MYDATE%\" MD "C:\%MYDATE%"
C:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump -uroot gs > "C:\%MYDATE%\gs.sql"
Please be aware that %DATE% can change a lot from machine to machine, there is no way you can expect this to therefore work accross other machines.
Guys im using this script in a batch file to perform continuous ping with timestamp and to be recorded in a log file, it is working fine on my computer ( windows 8 64 bit ) but when i try to use it on a windows 7 machine also 64 bit, whenever i run the batch i keep getting that:
Could not find C:\user\administrator\pinglog.txt
knowing that i have created the file pinglog.txt but i really cant figure out the problem.
#echo off
del pinglog.txt
for /f "tokens=*" %%A in ('ping localhost -n 1 ') do (echo %%A>>pinglog.txt && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping localhost -n 1 ') do (echo %date% %time% %%A>>pinglog.txt && GOTO Ping)
i appreciate any help.
Thank you in advance
Over PowerShell you can use this CLI.
ping.exe -t 10.227.23.241 |Foreach{"{0} - {1}" -f (Get-Date),$_} >> Ping_IP.txt
Could not find C:\user\administrator\pinglog.txt means that the file was not found. And the reason for this is really simple. The folder is called users and not user. So the correct path is C:\users\administrator\pinglog.txt.
I got a batch file to do mysqldump. The code is like this:
#echo off
echo Starting Backup of Mysql Database on server
for /F "tokens=2,3,4 delims=/ " %i in ('date /t') do set myDate=%k%i%j
set bkupfilename=%myDate%.sql
echo Backing up to file: %bkupfilename%
C:\xampp\mysql\bin\mysqldump --routines -u <user> -p<pwd> <database> > D:\MYSQL_DAILY_BACKUPS\"<database>%bkupfilename%"
When I run it on cmd console in Win7 by typing the batch file, it won't work and complain about:
C:\xampp\mysql\bin>mysqldumpbatch
Starting Backup of Mysql Database on server
kj was unexpected at this time.
But when I run it by copy pasting the code directly to command prompt it run just fine and produce file 20152401.sql. Anyone know why?
The single % variant only works from the command line. Try replacing with %% like so:
#echo off
echo Starting Backup of Mysql Database on server
for /F "tokens=2,3,4 delims=/ " %%i in ('date /t') do set myDate=%%k%%i%%j
set bkupfilename=%myDate%.sql
echo Backing up to file: %bkupfilename%
C:\xampp\mysql\bin\mysqldump --routines -u <user> -p<pwd> <database> > D:\MYSQL_DAILY_BACKUPS\"<database>%bkupfilename%"
Let me know if that works?
How do I do this properly. I'm trying to name the sql file that is being produced by mysqldump into the current date and time. I've already some research in this site and found a code in here: How to get current datetime on Windows command line, in a suitable format for using in a filename?
Tried to mixed it up with my current code and I came up with this one. The file is named into the current date and time but its only a 1kb file and does not produce a .sql file.
It is supposed to be a 7 kb sql file.
#For /f "tokens=2-4 delims=/ " %%a in ('date /t') do #(set mydate=%%c-%%a-%%b)
#For /f "tokens=1-2 delims=/:" %%a in ('time /t') do #(set mytime=%%a%%b)
#echo mydate= %mydate%
#echo mytime= %mytime%
mysqldump -u root -p --add-drop-table --create-options --password= onstor >c:\%mydate%_%mytime%.sql
UPDATE
I don't think there's a problem with the mysqldump command since it works well when I do it this way. The code below just uses the date as its filename.
#For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do #(
Set Month=%%A
Set Day=%%B
Set Year=%%C
)
#echo DAY = %Day%
#echo Month = %Month%
#echo Year = %Year%
mysqldump -u root --add-drop-table --create-options --password= onstor >c:\%Day%-%Month%-%Year%.sql
Please help, thanks.
On Linux, simply put $(date +%Y-%m-%d-%H.%M.%S) to show date and time in the file name, so it looks like:
mysqldump -u <user> -p <database> | bzip2 -c > <backup>$(date +%Y-%m-%d-%H.%M.%S).sql.bz2
(This command also compresses the file using bzip2)
I think the syntax of your mysqldump command is wrong;
mysqldump -u root -p --add-drop-table --create-options --password= onstor
You use both -p and --pasword=, you should only use one option. And there is a space before the password.
Just try to run the mysqldump command on the commandline to see error messages. Alternatively add 2>&1 at the end of the command in the batchfile. Then you would also see error messages in the output file.
mysqldump -u root --add-drop-table --create-options --password=onstor >c:\%mydate%_%mytime%.sql 2>&1
For those who want to get this to work in crontab, in that case it's a bit different:
30 01 * * * /opt/bitnami/mysql/bin/mysqldump -u root --password=MySecretPass database_name | gzip > /path/backup/`date "+\%d-\%m-\%y_\%H:\%M"`.gz