Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-03-04
3
Medium Priority
?
1,656 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 59

Assisted Solution

by:Bill Prew
Bill Prew earned 2000 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 59

Accepted Solution

by:
Bill Prew earned 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

876 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