Solved

Backup of postgresql database with batch

Posted on 2012-03-28
2
1,007 Views
Last Modified: 2012-03-28
Hi,

I want to create a batch file that backup our postgresql database schema. But I also want it to look in the backup folder how many files there are. If there are already 4 backup files, I want the batch file to delete 1. So the maximum of backup files has to be 4.

Below you find the code that I already have. I works, but I want to add that there can only be a maximum of 4 backupfiles.

 @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=%month%_%day%_%year%
   echo datestr is %datestr%
    
   set BACKUP_FILE=C:\postgresql_backup\backup_archx_%datestr%.backup
   echo backup file name is %BACKUP_FILE%
   SET PGPASSWORD=P0stgr3s
   echo on
   postgres\bin\pg_dump -i -h localhost -p 5432 -U postgres -F c -v -f %BACKUP_FILE% -n archx archx

Open in new window

0
Comment
Question by:Silencer001
[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
2 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 300 total points
ID: 37775454
Try the version below; it will look for .backup files in the backup folder, and delete all but the latest 3 (lines 12 to 14).
The script is currently in test mode and will only display the "del" command(s) it would normally run, so that you can test it. Remove the uppercase "ECHO" to run it for real.
@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=%month%_%day%_%year%
echo datestr is %datestr%
set BACKUP_FILE=C:\postgresql_backup\backup_archx_%datestr%.backup
echo backup file name is %BACKUP_FILE%
for /f "skip=3 delims=" %%a in ('dir /b /o:-d "C:\postgresql_backup\*.backup"') do (
  ECHO del "C:\postgresql_backup\%%a"
)
SET PGPASSWORD=P0stgr3s
echo on
postgres\bin\pg_dump -i -h localhost -p 5432 -U postgres -F c -v -f %BACKUP_FILE% -n archx archx

Open in new window

0
 
LVL 84

Expert Comment

by:oBdA
ID: 37775474
Oh, and you might want to change the postgre password now ...
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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