01. 概述
這裏簡略地介紹一下postgres單機是如何熱備份的,演示整個過程,以及簡要說明一下配置策略。 參照: https://www.postgresql.org/docs/9.4/static/continuous-archiving.html http://www.postgres.cn/docs/9.4/continuous-archiving.html
02. 部署postgres
這裏使用postgres9.4版本,使用apt-get一鍵到位安裝 $ sudo add-apt-repository "deb https://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main" $ wget --quiet -O - https://postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - $ sudo apt-get update;sudo apt-get install postgresql-9.4 -y $ sudo /etc/init.d/postgresql start; sudo netstat -tnlp |grep 5432
03. 修改配置
$ sudo vim /etc/postgresql/9.4/main/postgresql.conf --修改下面的參數 wal_level = archive # 歸檔級別爲archive archive_mode = on # 開啓歸檔模式 archive_command = 'test ! -f /data/back/pg_xlog2/%f && cp %p /data/back/pg_xlog2/%f' # 將歸檔日誌拷貝到自定義的備份目錄下,這裏是同一目錄 archive_timeout = 60 # 每60秒歸檔切換一次,即是生成000000000*樣子的文件 $ sudo mkdir -p /data/back/pg_xlog2; sudo chown postgres:postgres /data/back/pg_xlog2 --創建歸檔目錄及授權 $ sudo /etc/init.d/postgresql restart; sudo netstat -tnlp|grep 5432 --可啓動一下服務,並查看端口是否正常
04. 模擬生成數據過程
4.1 模擬創建數據 $ su - postgres --切換postgres管理用戶 $ createdb db1 --創建數據庫,或者psql登錄用語句創建庫[# create database db2 encoding='UTF-8';] $ psql db1 --登錄數據庫 db1=# create table t1(id int,primary key (id)); --創建一張測試表 db1=# create table t2(id int,primary key (id)); --再創建一張測試表 db1=# insert into t1(id) values(1); --t1插入N1條數據 db1=# insert into t1(id) values(2); --t1插入N2條數據 db1=# insert into t2(id) values(1); --t2插入N1條數據 db1=# insert into t2(id) values(2); --t2插入N2條數據 db1=# select * from t1; --兩條數據 id ---- 1 2 (2 rows) 4.2 開啓備份 db1=# select pg_start_backup('basedata'); --強制發生一次checkpoint點 pg_start_backup ----------------- 0/3000028 (1 row) $ tar zcvf /data/back/pg_xlog2/basedata.20190927.tar.gz /var/lib/postgresql/9.4/main/ --基礎數據備份 db1=# select pg_stop_backup(); --備份完了,需要停止,不然恢復的時候會提示錯誤 NOTICE: pg_stop_backup complete, all required WAL segments have been archived pg_stop_backup ---------------- 0/9000050 (1 row) $ watch -n 1 'ls -lh /var/lib/postgresql/9.4/main/pg_xlog/' --實時生成日誌文件(最新部分),監控日誌文件生成頻率爲60s,就是剛剛設置的archive_timeout Every 1.0s: ls -lh /var/lib/postgresql/9.4/main/pg_xlog/ Thu Sep 27 23:18:27 2018 total 65M -rw------- 1 postgres postgres 292 Sep 27 23:11 000000010000000000000003.00000028.backup -rw------- 1 postgres postgres 16M Sep 27 23:10 000000010000000000000008 -rw------- 1 postgres postgres 16M Sep 27 23:11 000000010000000000000009 -rw------- 1 postgres postgres 16M Sep 27 23:16 00000001000000000000000A -rw------- 1 postgres postgres 16M Sep 27 23:05 00000001000000000000000B drwx------ 2 postgres postgres 4.0K Sep 27 23:16 archive_status $ watch -n 1 'ls -lh /data/back/pg_xlog2/' --備份日誌文件(全部文件) Every 1.0s: ls -lh /data/back/pg_xlog2/ Thu Sep 27 23:17:49 2018 total 165M -rw------- 1 postgres postgres 16M Sep 27 22:47 000000010000000000000001 -rw------- 1 postgres postgres 16M Sep 27 22:47 000000010000000000000002 -rw------- 1 postgres postgres 16M Sep 27 22:47 000000010000000000000003 -rw------- 1 postgres postgres 292 Sep 27 23:11 000000010000000000000003.00000028.backup -rw------- 1 postgres postgres 16M Sep 27 22:50 000000010000000000000004 -rw------- 1 postgres postgres 16M Sep 27 22:55 000000010000000000000005 -rw------- 1 postgres postgres 16M Sep 27 23:00 000000010000000000000006 -rw------- 1 postgres postgres 16M Sep 27 23:05 000000010000000000000007 -rw------- 1 postgres postgres 16M Sep 27 23:10 000000010000000000000008 -rw------- 1 postgres postgres 16M Sep 27 23:11 000000010000000000000009 -rw------- 1 postgres postgres 16M Sep 27 23:16 00000001000000000000000A -rw-rw-r-- 1 postgres postgres 4.1M Sep 27 23:10 basedata.20190927.tar.gz 4.3 增量數據(基礎備份後的數據) db1=# insert into t1(id) values(3); --插入數據,插多幾次,數據也多條 INSERT 0 1 db1=# select pg_switch_xlog(); --手動切換日誌點,即是每執行一次每次會生成00000000*的日誌文件 pg_switch_xlog ---------------- 0/C000000 (1 row)
05. 模擬恢復過程
5.1 恢復數據 $ sudo /etc/init.d/postgresql stop --恢復需要停止數據庫,不能再寫入了,如果再寫數據,則新寫入的數據會丟失 $ mv /var/lib/postgresql/9.4/main/ /tmp/main --直接將數據移走,或者直接rm -rf刪除 $ tar zxvf /data/back/pg_xlog2/basedata.20190927.tar.gz --將基本數據解壓到本地 $ cd /var/lib/postgresql/9.4/main --進入數據目錄 $ rm -f pg_xlog/00000001000000000000000* --將原來歸檔的都刪除,以防影響數據質量 $ rm -f pg_xlog/archive_status/00000001000000000000000* --將原來歸檔的都刪除,以防影響數據質量 $ vim recovery.conf --創建恢復文件,從哪裏去,到哪裏來 restore_command='cp /data/back/pg_xlog2/%f "%p"' $ sudo /etc/init.d/postgresql restart --重新啓動 $ tailf /var/log/postgresql/postgresql-9.4-main.log --查看日誌是否有錯誤 # select * from t1; --表數據恢復 id ---- 1 2 3 4 5 (5 rows) 至此,增量熱備份完成。