01, pg_basebackup 介紹
pg_basebackup是postgresql提供的一個方便基礎備份的工具(9.1開始提供),這個工具會把整個數據庫實例的數據都拷貝出來,而不只是把實例中的部分(如某個數據庫或表)單獨備份出來,
該工具使用replication協議連接到數據庫實例上,所以主數據庫中的pg_hba.conf必須允許replication連接,類似如下:
host replication replica trust
在9.2之后支持級連復制,所以在之后的版本中,pg_basebackup也可以從另外一個standby庫上做基礎備份,都需注意如下幾方面:
1、備份中沒有備份歷史文件;
2、不確保所有需要的WAL文件都備份了,如果想確保,需要加命令行參數 ”-x";
3、如果在備份過程中standby被提升為主庫,則備份會失敗;
4、要求主庫中打開了“full_page_writes"參數,WAL文件不能被類似pg_compresslog的工具去掉full_page_writes信息。
[postgre@pg-1 backup]$ pg_basebackup --help
pg_basebackup takes a base backup of a running PostgreSQL server.
Usage:
pg_basebackup [OPTION]...
Options controlling the output:
-D, --pgdata=DIRECTORY receive base backup into directory
指定把備份寫到那個目錄,如果這個目錄或這個目錄路徑中的各級父目錄不存在,則pg_basebackup就會自動創建這個目錄,如果目錄存在,但目錄不為空,則會導致pg_basebackup執行
失敗。
-F, --format=p|t output format (plain (default), tar)
指定輸出格式:p原樣輸出,即把主數據庫中的各個數據文件,配置文件、目錄結構都完全一樣的寫到備份目錄;
t 把輸出的備份文件打包到一個tar文件中。
-r, --max-rate=RATE maximum transfer rate to transfer data directory
(in kB/s, or use suffix "k" or "M")
-R, --write-recovery-conf
write recovery.conf after backup
生成recovery.conf文件
-T, --tablespace-mapping=OLDDIR=NEWDIR
relocate tablespace in OLDDIR to NEWDIR
-x, --xlog include required WAL files in backup (fetch mode)
備份時會把備份中產生的xlog文件也自動備份出來,這樣才能在恢復數據庫時,應用這些xlog文件把數據庫推到一個一致點,然后真正打開這個備份的數據庫,這個選項與
-X fetch是完全一樣的。使用這個選項,需要設置“wal_keep_segments"參數,以保證在備份過程中,需要的WAL日志文件不會被覆蓋。
-X, --xlog-method=fetch|stream
include required WAL files with specified method
--xlogdir=XLOGDIR location for the transaction log directory
-z, --gzip compress tar output
使用gzip壓縮,僅能能與tar輸出模式配合使用。
-Z, --compress=0-9 compress tar output with given compression level
指定壓縮級別
General options:
-c, --checkpoint=fast|spread
set fast or spread checkpointing
設置checkpoint的模式。
-l, --label=LABEL set backup label
設置備份標識,
-P, --progress show progress information
在備份過程中實時打印備份進度
-v, --verbose output verbose messages
詳細模式,使用了-P后,還會打印出正在備份的具體文件的信息。
-V, --version output version information, then exit
-?, --help show this help, then exit
Connection options:
-d, --dbname=CONNSTR connection string
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port number
-s, --status-interval=INTERVAL
time between status packets sent to server (in seconds)
-U, --username=NAME connect as specified database user
-w, --no-password never prompt for password
-W, --password force password prompt (should happen automatically)
Report bugs to <pgsql-bugs@postgresql.org>.
02,備份數據
[postgres@kafka01 psql]$ pg_basebackup -Ft -Pv -Xf -z -Z5 -p 5432 -D /data/backup/
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 1/F6000028 on timeline 1
4581723/4581723 kB (100%), 2/2 tablespaces
pg_basebackup: write-ahead log end point: 1/F6000130
pg_basebackup: base backup completed
完成備份