pg_basebackup

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 <[email protected]>.

02,备份数据

#创建文件夹用户保存新的备份文件 并给 postgrs用户授权
[root@test-backup centos]# mkdir -p /data/backup/
[root@test-backup backup]# mkdir -p /data/11/backup/
[root@test-backup centos]# chown -R postgres /data/backup/
[root@test-backup centos]# chown -R postgres /data/
[root@test-backup centos]# chown -R postgres /data/11/backup/
[root@test-backup centos]# chmod 0700 /data/11/backup/
[root@test-backup centos]# chmod 0700 /data/backup/
[root@test-backup centos]# chmod 0600 /data/backup/
[root@test-backup centos]# chmod 0600 /data/11/backup/
[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


完成备份

03 恢复备份

 添加pg_clt的执行顺序环境变量

[root@test-backup centos]# find / -name pg_ctl
/usr/pgsql-11/bin/pg_ctl
[root@test-backup centos]# vim /etc/environment 

PATH="/usr/pgsql-11/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

[root@test-backup centos]# 
[root@test-backup centos]# 
[root@test-backup centos]# 
[root@test-backup centos]# source /etc/environment 

#解压生成的备份文件

cd /data/backup/
tar zxvf base.tar.gz  -C /data/11/backup/

#备份恢复

su postgres
#此命令会可能会出现端口已占用问题,需要将之前的postgresql进程杀死
pg_ctl start -D /data/11/backup/ 

猜你喜欢

转载自blog.csdn.net/qq_42533216/article/details/112860666