Solved

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

Posted on 2014-03-04
3
1,583 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 56

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 56

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

632 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