免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 16277 | 回复: 7
打印 上一主题 下一主题

一种简单的PostgreSQL备份脚本(无须手动输入密码) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-08-04 11:15 |只看该作者 |倒序浏览
参考了网上的文档,再结合自己的实际操作。给有需要的人,与之共勉。如有不当之处,还请指正。谢谢!!

因为PostgreSQL里没有加入密码选项,一般备份命令需要手动输入密码,所以会给自动备份带来一定的不便。这里我们使用 pg_dump 来备份,该命令一般存放在程序的安装位置,比如: /opt/PostgreSQL/8.4/bin/  下。

数据库帐号:test     数据库名:testdb    密码:123456  数据库地址:localhost   默认端口:5432

第一种方法:通过PostgreSQL的环境变量参数来实现保存密码。
在备份脚本执行前 设置一下 PGPASSWORD 参数,如:
cat pgsql_backup.sh
  1. #!/bin/bash
  2. export PGPASSWORD="123456"
  3. /opt/PostgreSQL/8.4/bin/pg_dump  -U test testdb > /backup/pgsql.backup.`date +%F`.sql
复制代码


可以通过一些其它方式实现脚本自动输入密码。

第二种方法:通过Expect 来实现自动交互,帮助输入密码。
建立 pgsql_backup.sh 脚本。实现按日期备份
cat pgsql_backup.sh
  1. #!/bin/bash
  2. /opt/PostgreSQL/8.4/bin/pg_dump  -U test testdb > /backup/pgsql.backup.`date +%F`.sql
复制代码


建立 pgsql_expect.sh 脚本,通过执行pgsql_expect.sh脚本来调用pgsql_backup.sh实现后者自动输入密码。
cat pgsql_expect.sh
  1. #!/usr/bin/expect
  2. set passwd "123456"
  3. spawn /root/pgsql_backup.sh;
  4. expect "*Password:";
  5. sleep 0.1;
  6. send "$passwd\r";
  7. sleep 2
复制代码


第三种方法:网上说是什么 ~/.pgpass 来保存密码。可试了很多次都不成功。郁闷。

附上官方 postgresql环境变量说明。
http://www.postgresql.org/docs/8.4/interactive/libpq-envars.html

The following environment variables can be used to select default connection parameter values, which will be used by PQconnectdb, PQsetdbLogin and PQsetdb if no value is directly specified by the calling code. These are useful to avoid hard-coding database connection information into simple client applications, for example.

    *

      PGHOST behaves the same as host connection parameter.
    *

      PGHOSTADDR behaves the same as hostaddr connection parameter. This can be set instead of or in addition to PGHOST to avoid DNS lookup overhead.
    *

      PGPORT behaves the same as port connection parameter.
    *

      PGDATABASE behaves the same as dbname connection parameter.
    *

      PGUSER behaves the same as user connection parameter. database.
    *

      PGPASSWORD behaves the same as password connection parameter. Use of this environment variable is not recommended for security reasons (some operating systems allow non-root users to see process environment variables via ps); instead consider using the ~/.pgpass file (see Section 30.14).
    *

      PGPASSFILE specifies the name of the password file to use for lookups. If not set, it defaults to ~/.pgpass (see Section 30.14).
    *

      PGSERVICE behaves the same as service connection parameter.
    *

      PGREALM sets the Kerberos realm to use with PostgreSQL, if it is different from the local realm. If PGREALM is set, libpq applications will attempt authentication with servers for this realm and use separate ticket files to avoid conflicts with local ticket files. This environment variable is only used if Kerberos authentication is selected by the server.
    *

      PGOPTIONS behaves the same as options connection parameter.
    *

      PGSSLMODE behaves the same as sslmode connection parameter.
    *

      PGREQUIRESSL behaves the same as requiressl connection parameter.
    *

      PGSSLCERT behaves the same as sslcert connection parameter.
    *

      PGSSLKEY behaves the same as sslkey connection parameter.
    *

      PGSSLROOTCERT behaves the same as sslrootcert connection parameter.
    *

      PGSSLCRL behaves the same as sslcrl connection parameter.
    *

      PGKRBSRVNAME behaves the same as krbsrvname connection parameter.
    *

      PGGSSLIB behaves the same as gsslib connection parameter.
    *

      PGCONNECT_TIMEOUT behaves the same as connect_timeout connection parameter.

The following environment variables can be used to specify default behavior for each PostgreSQL session. (See also the ALTER USER and ALTER DATABASE commands for ways to set default behavior on a per-user or per-database basis.)

    *

      PGDATESTYLE sets the default style of date/time representation. (Equivalent to SET datestyle TO ....)
    *

      PGTZ sets the default time zone. (Equivalent to SET timezone TO ....)
    *

      PGCLIENTENCODING sets the default client character set encoding. (Equivalent to SET client_encoding TO ....)
    *

      PGGEQO sets the default mode for the genetic query optimizer. (Equivalent to SET geqo TO ....)

Refer to the SQL command SET for information on correct values for these environment variables.

The following environment variables determine internal behavior of libpq; they override compiled-in defaults.

    *

      PGSYSCONFDIR sets the directory containing the pg_service.conf file.
    *

      PGLOCALEDIR sets the directory containing the locale files for message internationalization.

评分

参与人数 1可用积分 +2 收起 理由
renxiao2003 + 2 总结得很好。值得大家学习。

查看全部评分

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
2 [报告]
发表于 2010-08-04 11:57 |只看该作者
不错。支持一下。我备份的时候因为密码找了半天最后使用的是EXPECT。

论坛徽章:
0
3 [报告]
发表于 2010-08-04 13:49 |只看该作者
我备份的都没用密码.......

论坛徽章:
0
4 [报告]
发表于 2010-08-04 16:20 |只看该作者
root用户下是不需要密码的:
[root@XXXX bin]# su postgres -c "/usr/local//pgsql/bin/pg_dump -Fc testdb" > /tmp/testdb.pgsql
[root@XXXX bin]# ls /tmp/
mapping-root  testdb.pgsql

论坛徽章:
0
5 [报告]
发表于 2010-08-04 17:20 |只看该作者
root用户下是不需要密码的:
[root@XXXX bin]# su postgres -c "/usr/local//pgsql/bin/pg_dump -Fc testdb ...
zhujinzz 发表于 2010-08-04 16:20


这个应该是设置了数据库允许 postgres帐号无密码的情况下才行吧。
或者是数据库认证方式不同。情况当然就不一样了。

论坛徽章:
0
6 [报告]
发表于 2010-08-09 17:12 |只看该作者
第三种方法: 比如root 运行的脚本 一定呀 在root home目录中 建 .pgpass file 权限如下
-rw-------  1 root root     38 Nov 24  2009 .pgpass
内容如下:
localhost:5432:*:postgres:passwod
我验证可以这样用

论坛徽章:
1
寅虎
日期:2013-09-29 23:15:15
7 [报告]
发表于 2010-08-25 14:03 |只看该作者
ls的可以,谢谢!!

论坛徽章:
0
8 [报告]
发表于 2010-09-23 10:28 |只看该作者
学习
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP