Solved

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

Posted on 2014-03-04
3
1,449 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 51

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 51

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
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 …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now