@echo off
set ldt=
set datetime=

set PG_DUMP=C:\Software\PostgreSQL\9.6\bin\pg_dump.exe
set GZIP=C:\Software\7-Zip\7z.exe

set TMP_DIR=C:\temp\backups
set APP_NAME=suite
set DB_NAME=suite
set DATA_SRC_DIR=C:\data\%APP_NAME%

for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set DATETIME=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%%ldt:~8,2%%ldt:~10,2%
set ARCHIVE=%APP_NAME%_%DATETIME%
echo %DATETIME%

mkdir %TMP_DIR%
mkdir %TMP_DIR%\%ARCHIVE%
mkdir %TMP_DIR%\%ARCHIVE%\db
mkdir %TMP_DIR%\%ARCHIVE%\data

REM create DB dump
%PG_DUMP% --host 127.0.0.1 --port 5432 --username "postgres" --no-password  --format plain --no-owner --create --clean --inserts --no-privileges --no-tablespaces --no-unlogged-table-data --file "%TMP_DIR%\%ARCHIVE%\db\db.sql" "%DB_NAME%"

REM copy stored data
xcopy %DATA_SRC_DIR% %TMP_DIR%\%ARCHIVE%\data /s /e /y

REM zip it all
%GZIP% -ttar a dummy %TMP_DIR%\%ARCHIVE%\* -so | %GZIP% -si -tgzip a %TMP_DIR%\%ARCHIVE%.tgz
rd /s/q "%TMP_DIR%\%ARCHIVE%"

echo Backup created - file %TMP_DIR%\%ARCHIVE%.tgz