@echo off
@rem *******************************************************************************************************************
@rem ** Author: Gustavo Moreira
@rem ** 
@rem ** This version is a modification of Robert Lie (Mobilefish.com) version.
@rem **
@rem ** Makes a backup of the semaspv database.
@rem ** One backup file is created: nome_creas_semaspv_<yyyymmdd>_<hhmm>.sql
@rem ** For example: miguel_couto_semaspv_20090520_1252.zip
@rem **
@rem ** Software requirement:
@rem ** - Windos XP
@rem ** - PostgreSQL Client
@rem **
@rem ** This script only works on Windows 2000 and XP because 
@rem ** option "usebackq" is only valid for these operating systems.
@rem **
@rem *******************************************************************************************************************

@rem ALTERE AS SEGUINTES CONFIGURACOES DE ACORDO COM A INSTALACAO NO DETERMINADO CREAS
@rem *** set BACKUP_FILE=xxxxx
set POSTGRE_DIR=C:\PostgreSQL\8.4\bin
set POSTGRE_USER_ACCOUNT=postgres
set POSTGRE_USER_PASSWORD=postgres
set POSTGRE_DATABASE=semaspv

@rem *******************************************************************************************************************
@rem NAO MEXA NOS CODIGOS ABAIXO!!!
@rem *******************************************************************************************************************

set SCRIPT_DIR=C:\semaspv_backup
set BACKUP_DIR=%SCRIPT_DIR%
set SCRIPT_VERSION=1.0

echo ***************************************************************"<br>"
echo Script para gerar arquivos backups de um determinado creas"<br>"
echo ***************************************************************"<br>"
echo Versao: %SCRIPT_VERSION%"<br>"
echo Configuracoes:"<br>"
echo Diretorio bin do PostgreSQL: %POSTGRE_DIR% "<br>"
echo Banco: %POSTGRE_DATABASE% "<br>"
echo Usuario: %POSTGRE_USER_ACCOUNT% "<br>"
echo ***************************************************************"<br>"

@rem Fill the day, month en year variables
for /F "usebackq tokens=3 delims=/ " %%i in ('%DATE%') do set d=%%i
for /F "usebackq tokens=2 delims=/ " %%i in ('%DATE%') do set m=%%i
for /F "usebackq tokens=4 delims=/ " %%i in ('%DATE%') do set y=%%i


@rem Fill the hour and minute variables
for /F "usebackq tokens=1 delims=:" %%i in ('%TIME%') do set hour=%%i
for /F "usebackq tokens=2 delims=:" %%i in ('%TIME%') do set minute=%%i
goto create


@rem *** Step 1: Create date dependent directory ***********************************************************************
:create
set datetime=%y%%m%%d%_%hour%%minute%
set dir=%BACKUP_DIR%
if not exist %dir% mkdir %dir%

@rem *** Step 2: Create backup database ********************************************************************************
@rem Do not change anything below unless you know what you are doing
cd %POSTGRE_DIR%
SET PGPASSWORD=postgres
pg_dump -a -f "%dir%\%1_%POSTGRE_DATABASE%_%datetime%.sql" -Fp -O -t pcni_assistenciacreas.usuario* -t pcni_assistenciacreas.logon -t pcni_assistenciacreas.visita* -t pcni_assistenciacreas.familia -U %POSTGRE_USER_ACCOUNT% %POSTGRE_DATABASE%

@rem *** Step 3: Delete the date dependent directory *******************************************************************
@rem ******************************************************
@rem * VERIFY IF %dir% IS SET CORRECTLY                   *
@rem * ONLY THEN ENABLE THE STATEMENT BELOW               *
@rem *                                                    *
@rem ******************************************************
@rem rmdir /S /Q %dir%

