Solved

Backup of postgresql database with batch

Posted on 2012-03-28
2
1,010 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 85

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 85

Expert Comment

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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

691 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