Solved

Backup of postgresql database with batch

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

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 83

Expert Comment

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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

777 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