pgsql备份还原测试
2022/6/19 2:20:17
本文主要是介绍pgsql备份还原测试,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
前提:已配置好docker-compose
说明: pgsql服务已通过容器方式部署,在centos服务器上安装pgsql主要是使用pg_dump命令
安装pgsql测试环境一(模拟生产环境已经安装了pgsql)
创建目录
mkdir -p /data/docker mkdir -p /data/pgsql/{data,bak,script}
容器docker-compose方式安装:
~]# cat /data/docker/docker-compose.yaml version: "3.7" services: pgsql: container_name: pgsql image: postgres:latest privileged: true environment: POSTGRES_PASSWORD: password POSTGRES_USER: postgres POSTGRES_DB: postgres TZ: Asia/Shanghai ports: - 5432:5432 volumes: - /data/pgsql/data:/var/lib/postgresql/data restart: always
启动
docker]# docker-compose up -d
容器内操作
docker]# docker exec -it pgsql /bin/bash root@ae06c41c4223:/# su -l postgres postgres@ae06c41c4223:~$ psql psql (14.1 (Debian 14.1-1.pgdg110+1)) Type "help" for help. postgres=#
修改密码
配置密码 postgres=# alter user postgres password '123456'; ALTER ROLE
创建表,导入一些测试数据
postgres=# CREATE DATABASE runoobdb;
postgres=# \c runoobdb
runoobdb=# CREATE TABLE COMPANY( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL, JOIN_DATE DATE );
runoobdb=# INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00, '2007-12-13' ),
(5, 'David', 27, 'Texas', 85000.00, '2007-12-13');
安装pgsql测试环境二(使用pg_dump命令)
下载安装
~]# wget https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm ~]# rpm -ivh pgdg-redhat-repo-latest.noarch.rpm ~]# yum install -y postgresql14
备份脚本
script]# cat /data/pgsql/script/bak_full_db.sh #!/bin/bash date_now=`date +%Y%m%d-%H%M%S` dbname=runoobdb back_dir=/data/pgsql/bak back_log_dir="./backup.log" rmback_log_dir="./rmback.log" export PGPASSWORD="123456" #postgresql bakup /usr/bin/pg_dump -h 172.20.0.2 -p 5432 -U postgres $dbname >$back_dir/$dbname-$date_now.out if [ $? = 0 ];then echo "$date_now postgresql backup success" >>$back_log_dir echo "" >> $back_log_dir else echo "$date_now postgresql backup failed" >> $back_log_dir echo "" >>$back_log_dir fi #rm back files find $back_dir -mtime +8 -exec rm -fr {} \; if [ $? = 0 ];then echo "$date_now rm backup files successful">>$rmback_log_dir echo "" >>$rmback_log_dir else echo "$date_now rm backup files failed">>$rmback_log_dir echo "" >>$rmback_log_dir fi
备份任务计划
* * * * * /data/pgsql/script/bak_full_db.sh
查看备份情况
还原测试
删除表
runoobdb=# \d List of relations Schema | Name | Type | Owner --------+---------+-------+---------- public | company | table | postgres (1 row) runoobdb=# drop table company; DROP TABLE runoobdb=# \d Did not find any relations. runoobdb=#
删除库
runoobdb=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- dbname | postgres | UTF8 | en_US.utf8 | en_US.utf8 | postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | runoobdb | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres (5 rows) runoobdb=# \c postgres You are now connected to database "postgres" as user "postgres". postgres=# drop database runoobdb ; DROP DATABASE postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- dbname | postgres | UTF8 | en_US.utf8 | en_US.utf8 | postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres (4 rows) postgres=#
创建库
postgres=# create database runoobdb; CREATE DATABASE
还原
[root@localhost bak]# psql -h 172.20.0.2 -p 5432 -U postgres -W -d runoobdb < runoobdb-20220618-105902.out 口令: SET SET SET SET SET set_config ------------ (1 行记录) SET SET SET SET SET SET CREATE TABLE ALTER TABLE COPY 3 ALTER TABLE
查看数据
postgres=# \c runoobdb; You are now connected to database "runoobdb" as user "postgres". runoobdb=# \d List of relations Schema | Name | Type | Owner --------+---------+-------+---------- public | company | table | postgres (1 row) runoobdb=# select * from company; id | name | age | address | salary | join_date ----+-------+-----+----------------------------------------------------+--------+------------ 1 | Paul | 32 | California | 20000 | 2001-07-13 4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13 5 | David | 27 | Texas | 85000 | 2007-12-13 (3 rows)
参考文档
备份还原参考
http://t.zoukankan.com/shuaixf-p-2695245.html
安装参考
https://blog.csdn.net/qq_39626154/article/details/121029620
https://www.runoob.com/postgresql/postgresql-tutorial.html
yum仓库包配置查找
https://yum.postgresql.org/repopackages/
这篇关于pgsql备份还原测试的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南