postgresql
  • batch-file
  • 2014-01-29 48 views 0 likes 
    0

    我想在批处理文件中调用pg_dump命令。首先我得到所有表名,然后循环每个表并执行pg_dump命令。它必须很可能类似的东西,但我得到一个错误“语法错误”:在批处理脚本中调用psql pg_dump命令

    for %%T in (psql -U postgres -w -d test_db -t -c "SELECT table_name FROM 
    information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'") 
    do pg_dump -t %%T -U postgres test_db -w -f "C:\Users\mtuna\Documents\dumpfiles\%%T.sql" 
    done; 
    

    任何帮助,将不胜感激。

    +0

    你是如何存储的密码? – Houari

    回答

    2

    这里是一个解决方案:

    @echo off 
    SET TableListeFile=C:\Users\mtuna\Documents\dumpfiles\database_list.txt 
    
    REM Saveing all tables name of database test_db on a temp file: database_list.txt 
    psql -U postgres -d test_db -t -c "SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'" -o "%TableListeFile%" 
    
    REM Loop on liste tables name: 
    FOR /F "tokens=*" %%I IN (%TableListeFile%) DO (
    REM Dump each table on file 
    pg_dump -U postgres -h localhost -t %%I test_db > "C:\Users\mtuna\Documents\dumpfiles\%%I.sql" 
    ) 
    REM Delete temp file 
    del /Q %TableListeFile% 
    

    它会提示您为每个转储密码输入。如果你不想被承认,你可以使用Pgpass File

    希望有所帮助。

    胡阿里。

    +0

    谁发明了cmd.exe脚本中的'for/F'语法...哦,我不喜欢它们。 –

    +0

    太好了,那是我一直在寻找的,但问题是表名有空格,如“ID:00070246 CH:0001 ExportEinstellungen”。所以通常“公共。”ID:00070246 CH:0001 ExportEinstellungen \“”必须工作,但“公共。\”%%我\“”不起作用。你有什么主意吗? – mctuna

    +0

    我在德语中遇到了错误,可能会被翻译为,按文件名,目录名或驱动器号出现语法错误 – mctuna

    1

    备份:batch_file_backup.bat

    @echo off 
    SET PGPATH="E:\PostgreSQL\9.5\bin\pg_dump.exe" 
    SET PGPASSWORD=admin 
    %PGPATH% -h 127.0.0.1 -p 5432 -U postgres -F c -b -v -f C:\Users\Pukar\Downloads\backupfile\2017-04-04.backup database_name 
    

    bat文件备份运行PHP代码:

    $batchfile_path = "E:/xampp/htdocs/yig2016/ybase/main_app/bizlayer/protected/batch_file_backup.bat"; 
    $WshShell  = new COM("WScript.Shell"); 
    $exec   = $WshShell->Run($batchfile_path, 0, false); 
    

    还原:batch_file_restore.bat

    @echo off 
    SET PGPATH="E:\PostgreSQL\9.5\bin\pg_restore.exe" 
    SET PGPASSWORD=admin 
    %PGPATH% -h 127.0.0.1 -p 5432 -U postgres -d database_name -v C:\Users\Pukar\Downloads\backupfile\2017-04-04.backup 
    

    BAT文件恢复运行PHP代码:

    $batchfile_path = 
    E:/xampp/htdocs/yig2016/ybase/main_app/bizlayer/protected/batch_file_restore.bat"; 
    $WshShell  = new COM("WScript.Shell"); 
    $exec   = $WshShell->Run($batchfile_path, 0, false); 
    

    参考文献:http://www.somelesson.blogspot.com/2017/04/postgresql-backup-and-restore.html

    相关问题