2015-07-20 55 views
0

我嘗試從postgres備份所有數據庫,但我不知道如何在循環中執行此操作。 pg_dumpall對我來說不是個好主意,因爲我得到了超過1k的數據庫,並且它會增長。如何在批處理文件中通過pg_dump備份所有數據庫 - postgres

@echo off 

set BASELOG=LOG 
set BACKUPDIR=C:/postgres/backups/ 
set BACKUPDIR2=C:\postgres\backups 
set PGHOST=localhost 
set PGUSER=postgres 
set PGBIN="C:/Program Files/PostgreSQL/9.3/bin/" 
set FILELOG=log.txt 

set BACKUPDIRDATE="%BACKUPDIR%%date%/" 
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i 
set month=%%j 
set day=%%k 
set year=%%l 
) 
if not exist %BACKUPDIRDATE% mkdir %BACKUPDIRDATE% 

(
echo Backup start %date% %time% 
%PGBIN%pg_dump -w -i -h %PGHOST% -U %PGUSER% -F c -b -v -f  "%BACKUPDIRDATE%%LOG%.compressed" %BASELOG% 
echo End of backup %BASELOG% 

FORFILES /p %BACKUPDIR2% /s /D -7 /C "cmd /c rd /S /Q @path" 
echo Files are deleted 
)>> %BACKUPDIRDATE%%FILELOG% 2>&1 

有人能解釋我如何得到所有數據庫的列表,然後循環它們?

我試過%PGBIN%的psql /列表但這並不工作

+0

它不是確切的答案,但它可能是你http://serverfault.com/a/59847 –

+0

#得到系統的數據庫列表非常有用,排除tempate DBS DBS = $($ PSQL - l -t | egrep -v'template [01]'| awk'{print $ 1}') 不適用於windows批處理 –

+0

使用數據庫列表創建一個txt並使用-d添加一個for(dblist.txt)參數 –

回答

1

請重新檢查的紙條,我做它的飛行。我不記得pg_dump上的-w是什麼。 通過這種方式,您將獲得每個數據庫的文件。 作爲一個建議,使用\改爲/當談論Windows上的文件夾路徑 請原諒我的英語。

@echo off 

set BASELOG=LOG 
set BACKUPDIR=C:/postgres/backups/ 
set BACKUPDIR2=C:\postgres\backups 
set PGHOST=localhost 
set PGUSER=postgres 
set PGBIN="C:/Program Files/PostgreSQL/9.3/bin/" 
set FILELOG=log.txt 

set BACKUPDIRDATE="%BACKUPDIR%%date%/" 
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i 
set month=%%j 
set day=%%k 
set year=%%l 
) 
if not exist %BACKUPDIRDATE% mkdir %BACKUPDIRDATE% 

(

if exist dbs.lst del dbs.lst 
psql -h %PGHOST% -p 5432 -U %PGUSER% -c "SELECT datname FROM pg_database WHERE datistemplate = false;" -o "dbs.lst" 

for /f %%a IN (dbs.lst) DO (

    echo Backup start %date% %time% 
    %PGBIN%pg_dump -w -i -h %PGHOST% -U %PGUSER% -F c -b -v -f "%BACKUPDIRDATE%-%%a.compressed" %%a 
    echo End of backup %BASELOG% 

) 

FORFILES /p %BACKUPDIR2% /s /D -7 /C "cmd /c rd /S /Q @path" 
echo Files are deleted 
)>> %BACKUPDIRDATE%%FILELOG% 2>&1 
相關問題