數據庫備份的時候難免會出現一些小問題,本篇文章主要講解一下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