Odoo 10 数据库备份及恢复总结

数据库备份的时候难免会出现一些小问题,本篇文章主要讲解一下odoo的PostgreSQL数据库的备份恢复以及操作:

备份策略:阶段性备份+特定事件备份。周期性自动备份+手动检查+自动通知。
官方参考:https://www.odoo.com/forum/help-1/question/how-to-setup-a-regular-postgresql-database-backup-4728

非并行备份及恢复
使用PostgreSQL的pg_dump和pg_dumpall来备份数据,这两种备份工具都不支持并行备份。
所有备份文件都保存到Master节点,而不是segment节点。一般情况不使用pg_dump,它适用于PostgreSQL迁移到Greenplum。

1、pg_dump
可以在本地及远程进行备份,只需要表的读权限即可备份。pg_dump创建的备份是一致的,在pg_dump运行时数据库产生快照,不阻塞数据库的DML操作,但是会阻塞需要排他锁的操作,如alter table等。特别注意的是,pg_dump一次只能备份一个单独的数据库,且不能备份角色和表空间信息(因为这些信息是cluster-wide,而不是在某个数据库中(per-database))。 

命令参数:

$ pg_dump --help

pg_dump dumps a database as a text file or to other formats.

 

Usage:

  pg_dump [OPTION]... [DBNAME]

 

General options:

  -f, --file=FILENAME      output file name

  -F, --format=c|t|p       output file format (custom, tar, plain text)

  -i, --ignore-version     proceed even when server version mismatches

                           pg_dump version

  -v, --verbose            verbose mode

  -Z, --compress=0-9       compression level for compressed formats

  --help                   show this help, then exit

  --version                output version information, then exit

 

Options controlling the output content:

  -a, --data-only             dump only the data, not the schema

  -b, --blobs                 include large objects in dump

  -c, --clean                 clean (drop) schema prior to create

  -C, --create                include commands to create database in dump

  -d, --inserts            dump data as INSERT, rather than COPY, commands

  -D, --column-inserts     dump data as INSERT commands with column names

  -E, --encoding=ENCODING     dump the data in encoding ENCODING

  -n, --schema=SCHEMA         dump the named schema(s) only

  -N, --exclude-schema=SCHEMA do NOT dump the named schema(s)

  -o, --oids                  include OIDs in dump

  -O, --no-owner              skip restoration of object ownership

                              in plain text format

  -s, --schema-only           dump only the schema, no data

  -S, --superuser=NAME        specify the superuser user name to use in

                              plain text format

  -t, --table=TABLE           dump only matching table(s) (or views or sequences)

  -T, --exclude-table=TABLE   do NOT dump matching table(s) (or views or sequences)

  -x, --no-privileges         do not dump privileges (grant/revoke)

  --disable-dollar-quoting    disable dollar quoting, use SQL standard quoting

  --disable-triggers          disable triggers during data-only restore

  --use-set-session-authorization

                              use SESSION AUTHORIZATION commands instead of

                              ALTER OWNER commands to set ownership

  --gp-syntax                 dump with Greenplum Database syntax (default if gpdb)

  --no-gp-syntax              dump without Greenplum Database syntax (default if postgresql)

 

Connection options:

  -h, --host=HOSTNAME      database server host or socket directory

  -p, --port=PORT          database server port number

  -U, --username=NAME      connect as specified database user

  -W, --password           force password prompt (should happen automatically)

 

If no database name is supplied, then the PGDATABASE environment

variable value is used.

 

Report bugs to .

创建备份目录 :

[root@gp-master ~]# mkdir /gpbackup

[root@gp-master ~]# chown  gpadmin.gpadmin /gpbackup/

<1>表级别备份恢复
#  备份szlsd_db数据库中的member表。 -t表名,-U用户,-W密码 -f输出的备份文件名字 

$ pg_dump -t member -Uszlsd -W szlsd_db -f /gpbackup/member.dmp 

# 恢复数据

$ psql szlsd_db -Uszlsd -W 

<2>database级别备份恢复
#  备份database,szlsd_db是库名 

$ pg_dump szlsd_db -Ugpadmin -W  -f /gpbackup/szlsd_db.dmp   

 # 删除szlsd_db数据库
若用户连接数据库,那么无法删除该数据库,否则会报错 

szlsd_db=> drop database szlsd_db;

ERROR:  cannot drop the currently open database

这里使用gpadmin用户删除szlsd_db数据库 

testDB=# drop database szlsd_db;

DROP DATABASE

testDB=# \c

You are now connected to database "testDB" as user "gpadmin".

需创建database 

testDB=# \c

You are now connected to database "testDB" as user "gpadmin".

testDB=# create database szlsd_db with owner=szlsd  template=template0 ;

CREATE DATABASE

# 数据恢复,--single-transaction表示整个恢复过程是一个事务,要么成功要么回滚。
# 另外,导入的用户必须有superuser权限。恢复成功后,别忘了运行ANALYZE收集统计信息。 

$ psql --single-transaction szlsd_db 

<3>schema级别备份恢复
# 备份database中的schema。-n 表示schema。public是所有database中默认的schema。这里要备份szlsd_db中的temp schema。 

$ pg_dump szlsd_db -n temp -Uszlsd -W -f /gpbackup/temp.dmp  

# 删除schema 

szlsd_db=> drop schema temp cascade;

NOTICE:  drop cascades to table temp.a

DROP SCHEMA

# 恢复schema(无需手动创建schema) 

$ psql --single-transaction szlsd_db -Uszlsd -W 

确认temp schema已恢复 

szlsd_db=> \dn

       List of schemas

        Name        |  Owner  

--------------------+---------

 gp_toolkit         | gpadmin

 information_schema | gpadmin

 pg_aoseg           | gpadmin

 pg_bitmapindex     | gpadmin

 pg_catalog         | gpadmin

 pg_toast           | gpadmin

 public             | gpadmin

 temp               | szlsd

(8 rows)

<4>pg_dump其他常用参数 

-T, --exclude-table=TABLE   do NOT dump matching table(s) (or views or sequences)

-N, --exclude-schema=SCHEMA do NOT dump the named schema(s)

2、pg_dumpall
上面说了,gp_dump同时只能备份一个数据库。为了解决这个问题,就要使用pg_dumpall工具,它备份每个数据库和角色、表空间定义。
执行pg_dumpall需要超级用户权限。
 

 $ pg_dumpall --help

pg_dumpall extracts a PostgreSQL database cluster into an SQL script file.

 

Usage:

  pg_dumpall [OPTION]...

 

General options:

  -f, --file=FILENAME      output file name

  -i, --ignore-version     proceed even when server version mismatches

                           pg_dumpall version

  --help                   show this help, then exit

  --version                output version information, then exit

 

Options controlling the output content:

  -a, --data-only          dump only the data, not the schema

  -c, --clean              clean (drop) databases before recreating

  -d, --inserts            dump data as INSERT, rather than COPY, commands

  -D, --column-inserts     dump data as INSERT commands with column names

  -f, --filespaces         dump filespace data

  -g, --globals-only       dump only global objects, no databases

  -o, --oids               include OIDs in dump

  -O, --no-owner           skip restoration of object ownership

  -r, --resource-queues    dump resource queue data

  -s, --schema-only        dump only the schema, no data

  -S, --superuser=NAME     specify the superuser user name to use in the dump

  -x, --no-privileges      do not dump privileges (grant/revoke)

  --disable-dollar-quoting

                           disable dollar quoting, use SQL standard quoting

  --disable-triggers       disable triggers during data-only restore

  --use-set-session-authorization

                           use SESSION AUTHORIZATION commands instead of

                           OWNER TO commands

  --gp-syntax              dump with Greenplum Database syntax (default if gpdb)

  --no-gp-syntax           dump without Greenplum Database syntax (default if postgresql)

 

Connection options:

  -h, --host=HOSTNAME      database server host or socket directory

  -l, --database=DBNAME    alternative default database

  -p, --port=PORT          database server port number

  -U, --username=NAME      connect as specified database user

  -w, --no-password        never prompt for password

  -W, --password           force password prompt (should happen automatically)

 

If -f/--file is not used, then the SQL script will be written to the standard

output.

 

Report bugs to .

<1>导出所有role和tablespace 

$ pg_dumpall  -g >/gpbackup/role_tbs.sql

<2>导出所有database 

$ pg_dumpall  >/gpbackup/all.dmp

最后特别说一下,无论是pg_dump还是pg_dumpall,默认备份出的数据都不是SQL格式,而是COPY格式。-d, --inserts参数控制到底是SQL是COPY格式。
下面是我从备份文件中摘出来的一段: 

COPY member (id, name, gender, a, b) FROM stdin;

1       tom     m       \N      \N

3       tom3    m       \N      \N

8       lily8   f       \N      \N

2       tom2    m       \N      \N

4       lily    f       \N      \N

 

数据库保存在本地的时候有时会报错

odoo备份数据库
http://ip/web/data/manager
选择backup,输入密码admin
提示:Database backup error: Access denied(这个错误很常见)

1、处理:登陆odoo服务器,修改/etc/openerp_server.conf更改数据库维护的Master password; 管理员主控密码(用于创建、还原和备份数据库等操作)
admin_passwd = admin
保存文件后运行:service odoo restart 重启动odoo配置生效
重试备份数据库 http://ip/web/data/manager
backup数据库,输入Mastrer password:admin
提示: Database backup error: Command `pg_dump` not found.
尝试:在odoo服务器centos上安装postgresql/pg_dump,在终端中:yum -install postgresql

安装了pg_dump执行备份结果提示:Database backup error: Postgres subprocess ('/usr/bin/pg_dump', '--no-owner', '--file=/tmp/tmpGs8RYD/dump.sql', u'gsola') error 1 

2、odoo数据库duplicate操作:

odoo数据库管理的duplicate数据库,是将所选数据库在同一个服务器上覆制一份不同名称的克隆,用于测试或者联系等用途。

http://ip/web/data/manager
选择duplicate,输入密码admin,新数据库名称例如:lianxi,完成后会多出一个数据库“lianxi”共登陆或者操作室选择,表示duplicate成功。

 

3、odoo数据库的restore操作
http://ip/web/data/manager
选择restore

 

4、登陆freebsd+postgresql服务器
用pg_dump 备份,用pg_restore 恢复,这两个命令是postgresql系统自带的。

备份前停止odoo服务器:

# Stop OpenERP Server
/etc/init.d/openerp-server stop
# start OpenERP Server
/etc/init.d/openerp-server start

 

使用tar格式备份和恢复:

pg_dump -U username -Ft TestDb1>TestDb1.tar

没有testDb2则需要先创建:createdb testDb2,或者使用-C --create选项

pg_restore -U username -c -d TestDb2 TestDb1.tar 

对odoo来说需要先记住原来的数据库名和拥有者的用户名,然后删除之,再建同名同拥有者的数据库,然后恢复。或者-c --clean使用此选项,恢复对象前先删除。(此选项会出现错误提示,但是经过先备份后删除几个项目然后恢复,证明删除项正确恢复了)

 

5、在客户端pgAdminIII可以备份数据库

 

6、客户端pgAdmin带的pg_dump 备份,psql恢复,程序在pgadmin的安装目录。
备份:

pg_dump -h 192.168.12.40 -U uhml gsola > e:\gsola.bak

指令解释: 
pg_dump 是备份数据库指令,
192.168.12.40是数据库的ip地址(必须保证数据库允许外部访问的权限,如果是本地可以用localhost)
uhml是数据库的用户名
gsola是数据库名。
> 意思是导出到e:\gsola.bak文件里,如果没有写路径,单单写gsola.bak文件名,那么备份文件会保存在
当前目录
恢复:

psql -h 192.168.12.40 -c -U uhml -d gsola < e:\gsola.bak 

指令解释: 
psql是恢复数据库命令
192.168.12.40 是数据库的ip地址(必须保证数据库允许外部访问的权限,如果是本地可以用localhost)
uhml是数据库的用户名
gsola是要恢复到哪个数据库
< 的意思是把e:\gsola.bak文件导入到指定的数据库里
-c --clean 选项恢复对象前先删除。
以上所有的是针对windows,在linux里依然有效。

 

7、备份策略:多级异地异种备份
保证系统的数据的备份存在3个以上不同位置不同介质的备份
选择一个异地一个本地备份点,自动备份
移动备份/磁带备份:定期备份

 

8、pg_rman备份

 

9、barman备份
异常处理:
删除有活动链接的数据库:
如果数据库尚有活动连接,则drop数据库时会失败并有错误提示。

postgres=# DROP DATABASE testdb;

ERROR: database "testdb" is being accessed by other users
DETAIL: There are 3 other sessions using the database.
可以先用下面的语句把testdb的活动连接中止,然后再DROP数据库就可以了。
postgres=# SELECT pg_terminate_backend(pid)
postgres-# FROM pg_stat_activity
postgres-# WHERE datname='testdb' AND pid<>pg_backend_pid();

pg_terminate_backend
----------------------
t
t
t
(3 rows)

pg_stat_activity是一个系统视图,表中的每一行代表一个服务进程的属性和状态。
boolean pg_terminate_backend(pid int)是一个系统函数,用于终止一个后端服务进程。
int pg_backend_pid()系统函数用于获取附加到当前会话的服务器进程的ID
使用的数据库版本PostgreSQL 9.3
 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章