Automatic PostgreSQL backup tutorial in Windows and postgresql backup tutorial
Background
An automated system is required for my work on a PostgreSQL database project to perform backups every day. After some research, we decided to create a Windows batch processing file and add it to the Windows scheduled task.
The procedure is as follows:
How to configure
Step 1:
Download the batch file.
Step 2:
You can use a simple command (schtasks /? View help) or use a graphical interface (START-control panel-system and security-management tools-Task Scheduler) to run the task scheduler management tool, you can also double-click Taskschd in the % SYSTEMROOT % \ System32 directory. msc to start it.
Step 3:
You can use the wizard or XML file import information to create a basic task. For how to create a wizard, see the task scheduler help. The following describes the command line Syntax:
schtasks /Create [/S <system> [/U <username> [/P [<password>]]]] [/RU <username> [/RP <password>]] /SC <schedule> [/MO <modifier>] [/D <day>] [/M <months>] [/I <idletime>] /TN <taskname> /TR <taskrun> [/ST <starttime>] [/RI <interval>] [ {/ET <endtime> | /DU <duration>} [/K] [/XML <xmlfile>] [/V1]] [/SD <startdate>] [/ED <enddate>] [/IT] [/Z] [/F]
View the help of this command:
schtasks /Create /?
If remote task scheduler management is disabled in Windows Firewall, file and print sharing are enabled, and the remote registry service is running, a V1 task is created on the remote computer even if the parameter V1 is not used. The V1 parameter indicates that a task is visible to the lower-level system.
Use Code
Script:
<a href="http://my.oschina.net/echolee1987" target="_blank" rel="nofollow">@ECHO</a> OFF@setlocal enableextensions<a href="http://my.oschina.net/duini" target="_blank" rel="nofollow">@cd</a> /d "%~dp0" SET PGPATH=C:\"Program Files"\PostgreSQL\9.1\bin\SET SVPATH=f:\SET PRJDB=demoSET DBUSR=postgresFOR /F "TOKENS=1,2,3 DELIMS=/ " %%i IN ('DATE /T') DO SET d=%%i-%%j-%%kFOR /F "TOKENS=1,2,3 DELIMS=: " %%i IN ('TIME /T') DO SET t=%%i%%j%%k SET DBDUMP=%PRJDB%_%d%_%t%.sql<a href="http://my.oschina.net/echolee1987" target="_blank" rel="nofollow">@ECHO</a> OFF%PGPATH%pg_dump -h localhost -p 5432 -U postgres %PRJDB% > %SVPATH%%DBDUMP% echo Backup Taken Complete %SVPATH%%DBDUMP%
Initial Value
- PGPATH-PostgreSQL path
- SVPATH-backup file path
- PRJDB-name of the database to be backed up
- DBUSR-database username