Windows下自动备份PostgreSQL数据库的脚本

PostgresSQL不像SQLServer一样提供了作业管理器来定时自动备份数据库,需要用户自己使用pgdump来手动备份。

@echo off

for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
 set year=%%i
 set month=%%j
 set day=%%k
 set dow=%%l
)
set datestr=%year%_%month%_%day%
echo datestr is %datestr%

SET SAVEPATH=D:\
SET PGPATH=C:\"Program Files"\PostgreSQL\10\bin\
SET PGUSER=postgres
SET PGPASSWORD=postgres
SET PGDB=report
set BACKUP_FILE=%SAVEPATH%%PGDB%_%datestr%.backup
echo backup file is %BACKUP_FILE%

echo on
%PGPATH%pg_dump -h localhost -p 5432 -U %PGUSER% -F c -b -v -f %BACKUP_FILE% %PGDB%

@echo Backup Completed.
@echo Backup File: %BACKUP_FILE%

 

参考:https://wiki.postgresql.org/wiki/Automated_Backup_on_Windows

2 thoughts on “Windows下自动备份PostgreSQL数据库的脚本”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top