Solved

vbscript, scripts, postgresql,batch file, automate, windows 2008

Posted on 2014-03-04
3
1,527 Views
Last Modified: 2014-03-04
Hi experts..

I running the below script everyday using task scheduler to backup the database locally , and save along with the current date and time.
I need your help how to enhance little bit to rotate the backup copy, keep the latest copy and delete the previous copies.

set PGPASSWORD=password123
@echo off
set datetime=%date:~-4%_%date:~-7,2%_%date:~-10,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %datetime%
"D:\Program Files\VASCO\PostgreSQL\bin\pg_dump" -f "D:\Program Files\VASCO\IAS-Backup\DB_BK\PRI_AUTH_DB_BAK_%datetime%.bak" -Fc -Z9 -U username -v databaseinstance


please help me.
0
Comment
Question by:Skumar_CCSA
  • 2
3 Comments
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
ID: 39905393
Here's a basic addition to your script (I'm assuming it works correctly as you showed it) to add a step to only keep a certain number of backups.  Typically you want more than 1, I chose to keep 10 for the example.

@echo off

REM Set password
set PGPASSWORD=password123

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 now
"D:\Program Files\VASCO\PostgreSQL\bin\pg_dump" -f "D:\Program Files\VASCO\IAS-Backup\DB_BK\PRI_AUTH_DB_BAK_%datetime%.bak" -Fc -Z9 -U username -v databaseinstance

REM Only keep 10 newest files in backup folder
for /F "tokens=* skip=10" %%A in ('dir /b /a-d /tc /o-d "D:\Program Files\VASCO\IAS-Backup\DB_BK\PRI_AUTH_DB_BAK_*.bak"') do del "D:\Program Files\VASCO\IAS-Backup\DB_BK\%%~A"

Open in new window

~bp
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39905401
Also, consider using variables to reduce typing, make maintenance and changes easier, and improve readability.  Here's how I might approach it:

@echo off

REM Set paths and database info
set PGPASSWORD=password123
set PGuser=username
set PGinstance=databaseinstance
set PGdump=D:\Program Files\VASCO\PostgreSQL\bin\pg_dump
set BackupDir=D:\Program Files\VASCO\IAS-Backup\DB_BK
set BackupName=DB_BK\PRI_AUTH_DB_BAK_

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 now
"%PGdump%" -f "%BackupDir%\%BackupName%%DateTime%.bak" -Fc -Z9 -U %PGuser% -v %PGinstance%

REM Only keep 10 newest files in backup folder
for /F "tokens=* skip=10" %%A in ('dir /b /a-d /tc /o-d "%BackupDir%\%BackupName%*.bak"') do del "%BackupDir%\%%~A"

Open in new window

~bp
0
 

Author Closing Comment

by:Skumar_CCSA
ID: 39905549
your both scripts worked excellent...
appropriated your kind and help...

small correction in the second script..

set BackupName=DB_BK\PRI_AUTH_DB_BAK_ (Not work)

set BackupName=PRI_AUTH_DB_BAK_  (Worked)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question