標籤:
一、準備
串連上master節點建立replication使用者
# CREATE ROLE replicator with login replication password 123456;
修改白名單pg_hba.conf
# host replication all xxxxx/32 md5
二、製作基礎備份
# pg_basebackup --format=tar \ #使用打包方式備份--xlog-method=fetch \ #擷取wal檔案方式,等備份結束後把備份期間所有的wal備份--compress=1 \ #壓縮等級--checkpoint=fast \ #執行檢查點的方式--label=backup \ #標籤--pgdata=/data/backup/ #備份目錄--progress \ #列印過程--verbose \ #列印詳細資料--host=‘172.17.5.45‘ \ #master節點ip--port=5432 \ #master節點連接埠--user=postgres #串連master節點的使用者Password: transaction log start point: 2/34000028 on timeline 1414272/414272 kB (100%), 3/3 tablespaces transaction log end point: 2/34000128pg_basebackup: base backup completed
備份預設會佔用一個max_wal_senders串連,需要設定大一點,還可以使用
-r, --max-rate=RATE maximum transfer rate to transfer data directory (in kB/s, or use suffix "k" or "M")
限制傳輸速度
# ll backup/total 123196-rw-r--r--. 1 root root 129 Sep 8 01:21 25375.tar.gz-rw-r--r--. 1 root root 128 Sep 8 01:21 25376.tar.gz-rw-r--r--. 1 root root 126141406 Sep 8 01:22 base.tar.gz
base.tar.gz是資料目錄的備份,其他兩個是資料表空間的備份
三、恢複1. 解壓縮
# cd /data/backup/# mkdir pgdata 25375 25376# tar zxf base.tar.gz -C pgdata/# ls pgdata/backup_label global pg_dynshmem pg_ident.conf pg_logical pg_notify pg_serial pg_stat pg_subtrans pg_twophase pg_xlog postgresql.confbase pg_clog pg_hba.conf pg_log pg_multixact pg_replslot pg_snapshots pg_stat_tmp pg_tblspc PG_VERSION postgresql.auto.conf recovery.conf# tar zxf 25375.tar.gz -C 25375# tar zxf 25376.tar.gz -C 25376 # ls25375 25375.tar.gz 25376 25376.tar.gz base.tar.gz pgdata
2. 修改資料表空間軟串連
# ln -sf /data/backup/25375 pgdata/pg_tblspc/25375 [[email protected] backup]# ln -sf /data/backup/25376 pgdata/pg_tblspc/25376 [[email protected] backup]# ll pgdata/pg_tblspc/total 0lrwxrwxrwx. 1 root root 18 Sep 8 01:35 25375 -> /data/backup/25375lrwxrwxrwx. 1 root root 18 Sep 8 01:35 25376 -> /data/backup/25376
3. 修改資料目錄許可權
# chown -R postgres:postgres pgdata/ 25375 25376 # chmod 0700 pgdata/# lltotal 123208drwxr-xr-x. 3 postgres postgres 4096 Sep 8 01:32 25375-rw-r--r--. 1 root root 129 Sep 8 01:21 25375.tar.gzdrwxr-xr-x. 3 postgres postgres 4096 Sep 8 01:33 25376-rw-r--r--. 1 root root 128 Sep 8 01:21 25376.tar.gz-rw-r--r--. 1 root root 126141406 Sep 8 01:22 base.tar.gzdrwx------. 19 postgres postgres 4096 Sep 8 01:31 pgdata
4. 啟動
# su postgres$ pg_ctl -D pgdata/ startserver starting [ 2015-09-08 05:40:04.510 UTC 11107 55ee74b4.2b63 1 0]LOG: redirecting log output to logging collector process[ 2015-09-08 05:40:04.511 UTC 11107 55ee74b4.2b63 2 0]HINT: Future log output will appear in directory "pg_log".$ psql psql (9.4.4)Type "help" for help.postgres=# \db+ List of tablespaces Name | Owner | Location | Access privileges | Options | Description ------------+----------+--------------------+-------------------+---------+------------- pg_default | postgres | | | | pg_global | postgres | | | | tblspc1 | postgres | /data/backup/25375 | | | tblspc2 | postgres | /data/backup/25376 | | | (4 rows)
四、 製作從庫
1. 擷取備份檔案
# pg_basebackup --format=plain \ #使用目錄方式--write-recovery-conf \ #產生簡單recovery.conf--xlog-method=stream \ #使用stream的方式備份wal檔案,會再佔用一個max_wal_sender,wal檔案一產生就會備份--checkpoint=fast --label=backup --pgdata=/data/test_pgdata_94 --progress --verbose --host=‘172.17.5.45‘ --port=5432 --user=postgresPassword: transaction log start point: 2/38000028 on timeline 1pg_basebackup: starting background WAL receiver397887/397887 kB (100%), 3/3 tablespaces transaction log end point: 2/380000F0pg_basebackup: waiting for background process to finish streaming ...pg_basebackup: base backup completed
2. 修改目錄許可權
# chown -R postgres:postgres test_pgdata_94 tblspc1/ tblspc2/# chmod 0700 test_pgdata_94# ll test_pgdata_94total 104-rw-------. 1 postgres postgres 189 Sep 8 01:43 backup_labeldrwx------. 6 postgres postgres 4096 Sep 8 01:43 basedrwx------. 2 postgres postgres 4096 Sep 8 01:43 globaldrwx------. 2 postgres postgres 4096 Sep 8 01:43 pg_clogdrwx------. 2 postgres postgres 4096 Sep 8 01:43 pg_dynshmem-rw-------. 1 postgres postgres 4581 Sep 8 01:43 pg_hba.conf-rw-------. 1 postgres postgres 1636 Sep 8 01:43 pg_ident.confdrwxr-xr-x. 2 postgres postgres 4096 Sep 8 01:43 pg_logdrwx------. 4 postgres postgres 4096 Sep 8 01:43 pg_logicaldrwx------. 4 postgres postgres 4096 Sep 8 01:43 pg_multixactdrwx------. 2 postgres postgres 4096 Sep 8 01:43 pg_notifydrwx------. 2 postgres postgres 4096 Sep 8 01:43 pg_replslotdrwx------. 2 postgres postgres 4096 Sep 8 01:43 pg_serialdrwx------. 2 postgres postgres 4096 Sep 8 01:43 pg_snapshotsdrwx------. 2 postgres postgres 4096 Sep 8 01:43 pg_statdrwx------. 2 postgres postgres 4096 Sep 8 01:43 pg_stat_tmpdrwx------. 2 postgres postgres 4096 Sep 8 01:43 pg_subtransdrwx------. 2 postgres postgres 4096 Sep 8 01:43 pg_tblspcdrwx------. 2 postgres postgres 4096 Sep 8 01:43 pg_twophase-rw-------. 1 postgres postgres 4 Sep 8 01:43 PG_VERSIONdrwx------. 3 postgres postgres 4096 Sep 8 01:43 pg_xlog-rw-------. 1 postgres postgres 88 Sep 8 01:43 postgresql.auto.conf-rw-------. 1 postgres postgres 4676 Sep 8 01:43 postgresql.conf-rw-r--r--. 1 postgres postgres 131 Sep 8 01:43 recovery.conf# ll tblspc1/total 4drwx------. 2 postgres postgres 4096 Sep 8 01:43 PG_9.4_201409291# ll tblspc2total 4drwx------. 2 postgres postgres 4096 Sep 8 01:43 PG_9.4_201409291
3. 啟動
# su postgres$ pg_ctl -D test_pgdata_94 startserver starting$ [ 2015-09-08 05:49:55.383 UTC 12100 55ee7703.2f44 1 0]LOG: redirecting log output to logging collector process[ 2015-09-08 05:49:55.383 UTC 12100 55ee7703.2f44 2 0]HINT: Future log output will appear in directory "pg_log".$ psql psql (9.4.4)Type "help" for help.postgres=# \db+ List of tablespaces Name | Owner | Location | Access privileges | Options | Description ------------+----------+---------------+-------------------+---------+------------- pg_default | postgres | | | | pg_global | postgres | | | | tblspc1 | postgres | /data/tblspc1 | | | tblspc2 | postgres | /data/tblspc2 | | | (4 rows)
參考:http://www.postgresql.org/docs/9.4/static/app-pgbasebackup.html
PostgreSQL備份之pg_basebackup