pg_rman 一个第三方的备份工具。
1. 安装
1.1 rpm 安装
打开 https://github.com/ossc-db/pg_rman/releases
下载对应版本的rpm包。
1.2 source code安装
下载源码
git clone https://github.com/ossc-db/pg_rman.git
make
make installcheck
make install
2. How to use
# pg_rman --help
pg_rman manage backup/recovery of PostgreSQL database.
Usage:
pg_rman OPTION init
pg_rman OPTION backup
pg_rman OPTION restore
pg_rman OPTION show [DATE]
pg_rman OPTION show detail [DATE]
pg_rman OPTION validate [DATE]
pg_rman OPTION delete DATE
pg_rman OPTION purge
Common Options:
-D, --pgdata=PATH location of the database storage area
-A, --arclog-path=PATH location of archive WAL storage area
-S, --srvlog-path=PATH location of server log storage area
-B, --backup-path=PATH location of the backup storage area
-c, --check show what would have been done
-v, --verbose show what detail messages
-P, --progress show progress of processed files
Backup options:
-b, --backup-mode=MODE full, incremental, or archive
-s, --with-serverlog also backup server log files
-Z, --compress-data compress data backup with zlib
-C, --smooth-checkpoint do smooth checkpoint before backup
-F, --full-backup-on-error switch to full backup mode
if pg_rman cannot find validate full backup
on current timeline
NOTE: this option is only used in --backup-mode=incremental or archive.
--keep-data-generations=NUM keep NUM generations of full data backup
--keep-data-days=NUM keep enough data backup to recover to N days ago
--keep-arclog-files=NUM keep NUM of archived WAL
--keep-arclog-days=DAY keep archived WAL modified in DAY days
--keep-srvlog-files=NUM keep NUM of serverlogs
--keep-srvlog-days=DAY keep serverlog modified in DAY days
--standby-host=HOSTNAME standby host when taking backup from standby
--standby-port=PORT standby port when taking backup from standby
Restore options:
--recovery-target-time time stamp up to which recovery will proceed
--recovery-target-xid transaction ID up to which recovery will proceed
--recovery-target-inclusive whether we stop just after the recovery target
--recovery-target-timeline recovering into a particular timeline
--hard-copy copying archivelog not symbolic link
Catalog options:
-a, --show-all show deleted backup too
Delete options:
-f, --force forcibly delete backup older than given DATE
Connection options:
-d, --dbname=DBNAME database to connect
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port
-U, --username=USERNAME user name to connect as
-w, --no-password never prompt for password
-W, --password force password prompt
Generic options:
-q, --quiet don't show any INFO or DEBUG messages
--debug show DEBUG messages
--help show this help, then exit
--version output version information, then exit
Read the website for details. <http://github.com/ossc-db/pg_rman>
Report bugs to <http://github.com/ossc-db/pg_rman/issues>.
3. 使用前的必备
开启archive
archive_mode = on
archive_command = 'cp %p /data/archive/%f'
4. 操作
备份 -- 全备
# pg_rman --backup-path /data/backup_rman --pgdata /usr/local/pgsql/data/ --arclog-path /data/archive --backup-mode full -d postgres -U postgres backup
INFO: copying database files
INFO: copying archived WAL files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
备份 -- 增量
# pg_rman --backup-path /data/backup_rman --pgdata /usr/local/pgsql/data/ --arclog-path /data/archive --backup-mode incr -d postgres -U postgres backup
INFO: copying database files
INFO: copying archived WAL files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
查看
# pg_rman --backup-path /data/backup_rman --pgdata /usr/local/pgsql/data/ --arclog-path /data/archive --backup-mode full -d postgres -U postgres show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2019-07-09 07:13:38 2019-07-09 07:13:45 INCR 45MB 1 OK
2019-07-09 07:11:24 2019-07-09 07:11:37 FULL 1043MB 1 OK
2019-07-09 07:11:09 2019-07-09 07:11:09 FULL 0B 0 ERROR
删除错误备份
# pg_rman --backup-path /data/backup_rman --pgdata /usr/local/pgsql/data/ --arclog-path /data/archive --backup-mode full -d postgres -U postgres delete 2019-07-09 07:11:09
INFO: delete the backup with start time: "2019-07-09 07:11:09"
清除catalog 中已经删除的备份
# pg_rman --backup-path /data/backup_rman --pgdata /usr/local/pgsql/data/ --arclog-path /data/archive --backup-mode full -d postgres -U postgres purge
INFO: DELETED backup "2019-07-09 07:11:09" is purged
查看
# pg_rman --backup-path /data/backup_rman --pgdata /usr/local/pgsql/data/ --arclog-path /data/archive --backup-mode full -d postgres -U postgres show
=====================================================================
StartTime EndTime Mode Size TLI Status
=====================================================================
2019-07-09 07:13:38 2019-07-09 07:13:45 INCR 45MB 1 OK
2019-07-09 07:11:24 2019-07-09 07:11:37 FULL 1043MB 1 OK
查看 -- 备份的detail 信息
# pg_rman --backup-path /data/backup_rman --pgdata /usr/local/pgsql/data/ --arclog-path /data/archive --backup-mode full -d postgres -U postgres show detail
======================================================================================================================
StartTime EndTime Mode Data ArcLog SrvLog Total Compressed CurTLI ParentTLI Status
======================================================================================================================
2019-07-09 07:13:38 2019-07-09 07:13:45 INCR 12MB 33MB ---- 45MB false 1 0 OK
2019-07-09 07:11:24 2019-07-09 07:11:37 FULL 1144MB 167MB ---- 1043MB false 1 0 OK
查看单个备份
# pg_rman --backup-path /data/backup_rman --pgdata /usr/local/pgsql/data/ --arclog-path /data/archive --backup-mode full -d postgres -U postgres show '2019-07-09 07:11:24'
# configuration
BACKUP_MODE=FULL
FULL_BACKUP_ON_ERROR=false
WITH_SERVERLOG=false
COMPRESS_DATA=false
# result
TIMELINEID=1
START_LSN=b/c8000028
STOP_LSN=b/c8000130
START_TIME='2019-07-09 07:11:24'
END_TIME='2019-07-09 07:11:37'
RECOVERY_XID=5076779
RECOVERY_TIME='2019-07-09 07:11:36'
TOTAL_DATA_BYTES=1144619246
READ_DATA_BYTES=1144619013
READ_ARCLOG_BYTES=167773548
WRITE_BYTES=1043583686
BLOCK_SIZE=8192
XLOG_BLOCK_SIZE=8192
STATUS=OK
恢复:
1. 建立文件夹
2. 初始化数据库
./initdb -D /data/pgsql-data-test
3. 写恢复文件
vim recovery.conf
restore_command = 'cp /data/archive/%f %p'
recovery_target_time = '2019-07-09 23:49:51'
recovery_target_timeline = '1'
4. 各个文件夹权限必须为 postgres:postgres
chown -R postgres:postgres /data/backup_rman
chown -R postgres:postgres /data/pgsql-data-test
chown -R postgres:postgres /data/archive restore
5. 执行恢复
# pg_rman --backup-path /data/backup_rman --pgdata /data/pgsql-data-test --arclog-path /data/archive restore --recovery-target-time "2019-07-09 23:49:51"
INFO: the recovery target timeline ID is not given
INFO: use timeline ID of current database cluster as recovery target: 1
INFO: calculating timeline branches to be used to recovery target point
INFO: searching latest full backup which can be used as restore start point
INFO: found the full backup can be used as base in recovery: "2019-07-09 07:11:24"
INFO: copying online WAL files and server log files
INFO: clearing restore destination
INFO: validate: "2019-07-09 07:11:24" backup and archive log files by SIZE
INFO: backup "2019-07-09 07:11:24" is valid
INFO: restoring database files from the full mode backup "2019-07-09 07:11:24"
INFO: searching incremental backup to be restored
INFO: validate: "2019-07-09 07:13:38" backup and archive log files by SIZE
INFO: backup "2019-07-09 07:13:38" is valid
INFO: restoring database files from the incremental mode backup "2019-07-09 07:13:38"
INFO: searching backup which contained archived WAL files to be restored
INFO: backup "2019-07-09 07:13:38" is valid
INFO: restoring WAL files from backup "2019-07-09 07:13:38"
INFO: restoring online WAL files and server log files
INFO: generating recovery.conf
INFO: restore complete
HINT: Recovery will start automatically when the PostgreSQL server is started.
6. 启动数据库
# service postgresql-11 start
Redirecting to /bin/systemctl start postgresql-11.service
7. 登录数据库验证
mytest=# select pg_wal_replay_resume();
pg_wal_replay_resume
----------------------
(1 row)
mytest=# create table test0710 (id int);
CREATE TABLE