千家信息网

postgreSQL11备份与恢复方法是什么

发表于:2024-09-30 作者:千家信息网编辑
千家信息网最后更新 2024年09月30日,本篇内容介绍了"postgreSQL11备份与恢复方法是什么"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有
千家信息网最后更新 2024年09月30日postgreSQL11备份与恢复方法是什么

本篇内容介绍了"postgreSQL11备份与恢复方法是什么"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1、归档目录:

[postgres@centos1 arch]$ pwd/home/postgres/arch

2、设置归档命令:

 archive_command                                             -------------------------------------------------------------------------------------------------------- DATE=`date +%Y%m%d`; DIR="/home/postgres/arch/$DATE"; (test -d $DIR || mkdir -p $DIR) && cp %p $DIR/%f

修改wal_level和archive_mode参数都需要重新启动数据库才可以生效,修改archive_command不需要重启,只需要reload即可:

postgres=# SELECT pg_reload_conf();

3、验证归档:

postgres=# checkpointpostgres-# ;CHECKPOINTpostgres=# select pg_switch_wal(); pg_switch_wal --------------- 0/11029F08(1 row)[postgres@centos1 20200103]$ lltotal 16M-rw------- 1 postgres postgres 16M Jan  3 10:45 000000010000000000000011

4、配置备份用户访问:

[postgres@centos1 pg_root]$ vi pg_hba.confhost replication rep 0.0.0.0/0 md5

5、创建基础备份:

[postgres@centos1 pgbak]$ pg_basebackup -Ft  -D /home/postgres/pgbak`date +%F` -h 192.168.1.212 -p 1921 -U repPassword:[postgres@centos1 pgbak2020-01-03]$ lltotal 96M-rw------- 1 postgres postgres 1.5K Jan  3 11:34 26097.tar-rw------- 1 postgres postgres  80M Jan  3 11:34 base.tar-rw------- 1 postgres postgres  17M Jan  3 11:34 pg_wal.tar

查看备份内容:

[postgres@centos1 pgbak2020-01-03]$ tar -tvf base.tar |less-rw------- postgres/postgres 226 2020-01-03 11:34 backup_label-rw------- postgres/postgres  28 2020-01-03 11:34 tablespace_mapdrwx------ postgres/postgres   0 2020-01-03 11:34 pg_wal/drwx------ postgres/postgres   0 2020-01-03 11:34 ./pg_wal/archive_status/drwx------ postgres/postgres   0 2019-12-19 17:24 global/-rw------- postgres/postgres 16384 2019-12-17 16:42 global/1262-rw------- postgres/postgres 49152 2019-06-17 23:47 global/1262_fsm-rw------- postgres/postgres     0 2019-06-17 23:47 global/2964-rw------- postgres/postgres 16384 2020-01-03 10:45 global/1213-rw------- postgres/postgres 49152 2019-06-17 23:47 global/1213_fsm-rw------- postgres/postgres 16384 2019-06-17 23:47 global/1136-rw------- postgres/postgres 49152 2019-06-17 23:47 global/1136_fsm-rw------- postgres/postgres 16384 2019-12-17 11:49 global/1260

6、生成测试恢复数据:

postgres=# create table test_bk (id int) tablespace tbs_pg01;CREATE TABLEpostgres=# insert into test_bk values(1),(2);INSERT 0 2

由于WAL文件是写满16MB才会进行归档,测试阶段可能写入会非常少,可以在执行完 基础备份之后,手动进行一次WAL切换。如:

postgres=# checkpoint;CHECKPOINTpostgres=# select pg_switch_wal(); pg_switch_wal --------------- 0/14027F78(1 row)

7、还原部分

关闭数据库:

[postgres@centos1 ~]$ pg_ctl stopwaiting for server to shut down.... doneserver stopped[postgres@centos1 ~]$ ipcs

移除数据库 及表空间

[postgres@centos1 ~]$ mv pgdata pgdatatbbk[postgres@centos1 ~]$ mv pg_root pg_rootbk

将备份文件拷贝到原目录

[postgres@centos1 ~]$ echo $PGDATA/home/postgres/pg_root[postgres@centos1 ~]$ mkdir pg_root[postgres@centos1 ~]$ mkdir pgdata[postgres@centos1 ~]$ cd pgbak2020-01-03[postgres@centos1 pgbak2020-01-03]$ lltotal 96M-rw------- 1 postgres postgres 1.5K Jan  3 11:34 26097.tar-rw------- 1 postgres postgres  80M Jan  3 11:34 base.tar-rw------- 1 postgres postgres  17M Jan  3 11:34 pg_wal.tar[postgres@centos1 pgbak2020-01-03]$ cp 26097.tar /home/postgres/pgdata[postgres@centos1 pgbak2020-01-03]$ cp base.tar $PGDATA[postgres@centos1 pgbak2020-01-03]$ cp pg_wal.tar  $PGDATA[postgres@centos1 pgbak2020-01-03]$ cd $PGDATA[postgres@centos1 pg_root]$ lltotal 96M-rw------- 1 postgres postgres 80M Jan  3 12:06 base.tar-rw------- 1 postgres postgres 17M Jan  3 12:07 pg_wal.tar

解压base:

[postgres@centos1 pg_root]$ tar -xvf base.tar

解压表空间:

[postgres@centos1 pgdata]$ tar -xvf 26097.tar PG_11_201809051/[postgres@centos1 pgdata]$ lltotal 4.0K-rw------- 1 postgres postgres 1.5K Jan  3 12:06 26097.tardrwx------ 2 postgres postgres    6 Jan  2 20:07 PG_11_201809051

解压归档文件:

[postgres@centos1 pg_root]$ tar -xvf pg_wal.tar 000000010000000000000013archive_status/000000010000000000000013.done

拷贝恢复文件

[postgres@centos1 pg_root]$ cp /opt/postgresql/share/recovery.conf.sample recovery.conf配置恢复文件命令:vi recovery.confrestore_command = 'cp /home/postgres/arch/20200103/%f %p'

启动数据库:

[postgres@centos1 pg_root]$ pg_ctl start

waiting for server to start....2020-01-03 13:05:16.488 CST [21872] FATAL: data directory "/home/postgres/pg_root" has invalid permissions
2020-01-03 13:05:16.488 CST [21872] DETAIL: Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).
stopped waiting
pg_ctl: could not start server
Examine the log output.

报错,修改权限:

[postgres@centos1 ~]$ chmod -R 750 ./pg_root

启动数据库:

[postgres@centos1 ~]$ pg_ctl start waiting for server to start....2020-01-03 13:09:16.927 CST [22152] LOG:  listening on IPv4 address "0.0.0.0", port 19212020-01-03 13:09:16.972 CST [22152] LOG:  listening on Unix socket "/tmp/.s.PGSQL.1921"2020-01-03 13:09:17.035 CST [22153] LOG:  database system was interrupted; last known up at 2020-01-03 11:34:44 CST2020-01-03 13:09:17.035 CST [22153] LOG:  creating missing WAL directory "pg_wal/archive_status"2020-01-03 13:09:17.446 CST [22153] LOG:  starting archive recovery2020-01-03 13:09:17.457 CST [22153] LOG:  restored log file "000000010000000000000013" from archive2020-01-03 13:09:17.700 CST [22153] LOG:  redo starts at 0/130000282020-01-03 13:09:17.726 CST [22153] LOG:  consistent recovery state reached at 0/130001302020-01-03 13:09:17.727 CST [22152] LOG:  database system is ready to accept read only connections2020-01-03 13:09:17.743 CST [22153] LOG:  restored log file "000000010000000000000014" from archive doneserver started[postgres@centos1 ~]$ 2020-01-03 13:09:17.920 CST [22153] LOG:  restored log file "000000010000000000000015" from archivecp: cannot stat '/home/postgres/arch/20200103/000000010000000000000016': No such file or directory2020-01-03 13:09:18.084 CST [22153] LOG:  redo done at 0/150001402020-01-03 13:09:18.085 CST [22153] LOG:  last completed transaction was at log time 2020-01-03 11:40:52.26971+082020-01-03 13:09:18.125 CST [22153] LOG:  restored log file "000000010000000000000015" from archivecp: cannot stat '/home/postgres/arch/20200103/00000002.history': No such file or directory2020-01-03 13:09:18.310 CST [22153] LOG:  selected new timeline ID: 22020-01-03 13:09:18.477 CST [22153] LOG:  archive recovery completecp: cannot stat '/home/postgres/arch/20200103/00000001.history': No such file or directory2020-01-03 13:09:18.840 CST [22152] LOG:  database system is ready to accept connections

启动完成,查看表是否存在:

[postgres@centos1 ~]$ psqlpsql (11.3)Type "help" for help.pgdb=# \c postgresYou are now connected to database "postgres" as user "postgres".postgres=# select * from test_bk; id ----  1  2

恢复完成,恢复文件会变成.done

-rwxr-x--- 1 postgres postgres 5.7K Jan  3 13:00 recovery.done

顺便记一下逻辑备份的部分嘿嘿

逻辑备份

[postgres@centos1 dump]$ pg_dump -F c -f ./pgdb.dmp -C -E UTF8 -h 192.168.1.212 -p 1921 -U postgres -d pgdb

查看备份文件

[postgres@centos1 dump]$ pg_restore -l ./pgdb.dmp

"postgreSQL11备份与恢复方法是什么"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0