Solved

Backup of postgresql database with batch

Posted on 2012-03-28
2
1,001 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
  • 2
2 Comments
 
LVL 82

Accepted Solution

by:
oBdA earned 300 total points
Comment Utility
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 82

Expert Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
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…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

12 Experts available now in Live!

Get 1:1 Help Now