docker搭建postgresql 14主从库 及使用shell脚本备份数据库
 
1、地址规划
 
| IP地址 | 名称 | 备注 | 
|---|
| 192.168.1.11 | pg1 | 主库 | 
| 192.168.1.21 | pg2 | 从库 | 
2、安装部署主库
 
[root@cn01 ~]
[root@cn01 ~]
97fb21c9e700842e131269907f88e17ca274794e9410d702ef9da9fbf89c140e
[root@cn01 data]
root@97fb21c9e700:/
postgres@97fb21c9e700:/$ psql
psql (14.2 (Debian 14.2-1.pgdg110+1))
Type "help" for help.postgres=
CREATE ROLE
postgres=List of rolesRole name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}replica   | Replication                                                | {}
postgres=
postgres@97fb21c9e700:/$ exit
exit
[root@cn01 data]
listen_addresses = '*'
archive_mode = on
archive_command = '/bin/date'
max_connections = 100 
max_wal_senders = 32
wal_sender_timeout = 60s
wal_keep_size = 16
wal_level = replica
 
3、安装部署从库
 
[root@cn21 ~]
6cd25ae30181b20bf70d7a2b2a813b724dc972a6bef09c2d7ec5186a6ebf3363
[root@cn21 ~]
root@6cd25ae30181:/
[root@cn21 ~]
root@6cd25ae30181:/
26288/26288 kB (100%), 1/1 tablespace
[root@cn21 ~]
pgtest
[root@cn21 ~]
primary_conninfo = 'user=replica passfile=''/root/.pgpass'' channel_binding=prefer host=192.168.10.11 port=5432 sslmode=prefer sslcompression=0 sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=prefer krbsrvname=postgres target_session_attrs=any'
 
4、查看进程
 
[root@cn01 ~]
polkitd   13707  13678  0 15:11 ?        00:00:00 postgres
polkitd   13767  13707  0 15:11 ?        00:00:00 postgres: checkpointer
polkitd   13768  13707  0 15:11 ?        00:00:00 postgres: background writer
polkitd   13769  13707  0 15:11 ?        00:00:00 postgres: walwriter
polkitd   13770  13707  0 15:11 ?        00:00:00 postgres: autovacuum launcher
polkitd   13772  13707  0 15:11 ?        00:00:00 postgres: stats collector
polkitd   13773  13707  0 15:11 ?        00:00:00 postgres: logical replication launcher
polkitd   18562  13707  0 15:13 ?        00:00:00 postgres: walsender replica 192.168.10.21(57954) streaming 0/4000060                                
root      22923  13304  0 15:15 pts/0    00:00:00 grep --color=auto postgres[root@cn21 ~]
6cd25ae30181        postgres:latest     "docker-entrypoint..."   11 minutes ago      Up About a minute   0.0.0.0:5432->5432/tcp   pgtest
 
5、测试同步
 
postgres=
CREATE DATABASE
postgres=List of databasesName    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +|          |          |            |            | postgres=CTc/postgrestemplate1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +|          |          |            |            | postgres=CTc/postgrestest      | postgres | UTF8     | en_US.utf8 | en_US.utf8 | test12    | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
(5 rows)postgres=List of databasesName    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +|          |          |            |            | postgres=CTc/postgrestemplate1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +|          |          |            |            | postgres=CTc/postgrestest      | postgres | UTF8     | en_US.utf8 | en_US.utf8 | test12    | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
(5 rows)
 
6、shell脚本定期备份postgresql数据库
 
folder=/backup/pgdatabackup
if [ ! -d folder ];then
mkdir -p "$folder"
fi
cd $folder
day=`date +%Y%m%d%M`
FILENAME=$day.backup
mkdir -p "$day"
host=192.168.10.21
user=postgres
password=123456
db=test
docker exec -i pgtest /bin/bash -c 'PASSWORD='$password' /usr/bin/pg_dump -Fc -p 5432 -U '$user' -d '$db'>/home/'$FILENAME''
docker cp 'pgtest:/home/'$FILENAME'' ''$folder'/'$FILENAME''
docker exec -i pgtest /bin/bash -c 'rm -rf /home/'$FILENAME''
 
7、shell脚本定时任务
 
crontab -e
*/1 * * * * /test/pgbackup.sh       
*/10 * * * * /test/pgbackup.sh      
0 */1 * * * /test/pgbackup.sh       
30 1 * * * /test/pgbackup.sh        
0 23-7/2,8 * * * /test/pgbackup.sh 
0 11 4 * 1-3 /test/pgbackup.sh      
22 4 * * 0 /test/pgbackup.sh        
42 4 1 * * /test/pgbackup.sh