我们可以为 PostgreSQL 数据库备份制定脚本或作业计划吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20007316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:11:24  来源:igfitidea点击:

Can we make a Script or Job Schedule for PostgreSQL database Back-UP?

postgresqljob-schedulingpgadminpgagent

提问by 09Q71AO534

I am using PostgreSQL Database, I am running a Database Server .

我正在使用 PostgreSQL 数据库,我正在运行一个数据库服务器。

My PostgreSQL Versionis :

我的PostgreSQL 版本是:

postgres=# select version();
                           version
-------------------------------------------------------------
 PostgreSQL 9.2.4, compiled by Visual C++ build 1600, 32-bit
(1 row)

My Requirement:

我的要求:

Can we make a Script or a Job-Scheduleto Run for Keeping the Back-Upof PostgreSQLDatabase for every 2 Days ?

我们可以做出一个Script or a Job-Schedule要运行保持Back-UpPostgreSQL的数据库,每2天?

Is there a way for doing that ?

有没有办法做到这一点?

Can we Run any Scripts or Job-Scheduling Jobs for making this happen?

我们可以运行任何脚本或作业调度作业来实现这一点吗?

Is there a way of doing that using PgAgent! I am using PgAdminIII.

有没有办法使用PgAgent做到这一点!我正在使用PgAdminIII

回答by foibs

Taken from here

取自这里

To achieve an automated backup in a windows environment:

要在 Windows 环境中实现自动备份:

1. Create a .pgpass file

1. 创建一个 .pgpass 文件

(I called mine pgpass.conf) and put it somewhere secure. I have it in a subdirectory under the script that runs the backup.

(我打电话给我的 pgpass.conf)并将其放在安全的地方。我将它放在运行备份的脚本下的子目录中。

pgpass contains host access information in this format

pgpass 包含这种格式的主机访问信息

hostname:port:database:username:password

hostname:port:database:username:password

Lock down the .pgpass file Using NTFS permissions, disable access to this file for everybody except the user pg is running as (If you're running pg under the system account, then you should set it to use it's own user credentials)

锁定 .pgpass 文件 使用 NTFS 权限,禁止除 pg 用户以外的所有人访问此文件(如果您在系统帐户下运行 pg,则应将其设置为使用自己的用户凭据)

2. Create a script to call pg_dumpall

2.创建脚本调用pg_dumpall

For example:

例如:

SET PGPASSFILE=C:\foo\bar\PG_BACKUP\PGPASSFILE\pgpass.conf
"C:\Program Files\PostgreSQL.2\bin\pg_dumpall.exe" -U postgres_username  > C:\foo\bar\PG_BACKUP\db.out current

The firstline is the location of your pgpass file. The second line is the pg_dumpall command which outputs your whole database to the file C:\foo\bar\PG_BACKUP\db.out

第一行是 pgpass 文件的位置。第二行是 pg_dumpall 命令,它将整个数据库输出到文件C:\foo\bar\PG_BACKUP\db.out

3. Create a scheduled task

3.创建定时任务

The command is

命令是

C:\Windows\System32\cmd.exe /c "C:\foo\bar\PG_BACKUP\pg_backup.bat"

C:\Windows\System32\cmd.exe /c "C:\foo\bar\PG_BACKUP\pg_backup.bat"

and it starts in the directory

它从目录中开始

C:\foo\bar\PG_BACKUP

C:\foo\bar\PG_BACKUP

Here's a good tutorial on how to create scheduled tasks on windows:

这是一个关于如何在 Windows 上创建计划任务的好教程:

http://www.makeuseof.com/tag/how-to-automate-windows-programs-on-a-schedule/.

http://www.makeuseof.com/tag/how-to-automate-windows-programs-on-a-schedule/

You can find plenty such tutorials and info with a simple search.

您可以通过简单的搜索找到大量此类教程和信息。