一、增量备份
定期对数据库做基础备份,再配合WAL的归档日志,可实现再较短时间将数据库恢复
开启WAL 归档
创建归档目录
[root@s101 /var/lib/pgsql/13]#ls
archive_wals
backups
scripts
wal归档 记录 touch pg_archive_logs
[root@s101 /var/lib/pgsql/13/archive_wals]#cat pg_archive_logs
the old backup file not exists!
开启WAL 归档
方法1)按天一个文件wal归档
1.配置文件参数设置
[root@s101 /var/lib/pgsql/13/data]#nano postgresql.conf
wal_level = replica # minimal, replica, or logical archive_mode = on # enables archiving; off, on, or always archive_command = '/bin/bash /var/lib/pgsql/13/scripts/pg_archive.sh %p %f' # command to use to archive a logfile segment # placeholders: %p = path of file to archive # %f = file name only # e.g. 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f'
2.归档执行脚本
pg_archive.sh
[root@s101 /var/lib/pgsql/13/scripts]#cat pg_archive.sh
#!/bin/bash source /var/lib/pgsql/.bash_profile DATE=`date +%Y%m%d` DIR="/var/lib/pgsql/13/archive_wals/$DATE" BACK="/var/lib/pgsql/13/archive_wals/"`date -d '-20 day' +%Y%m%d` if [ -d "$BACK" ]; then rm -rf $BACK echo "success rm $BACK" > /var/lib/pgsql/13/archive_wals/pg_archive_logs else echo "the old backup file not exists!" > /var/lib/pgsql/13/archive_wals/pg_archive_logs fi (test -d $DIR || mkdir -p $DIR) && cp $1 $DIR/$2
方法二)归档文件放在一个文件中(目前使用的方法)
1.配置文件参数设置
[root@s101 /var/lib/pgsql/13/data]#nano postgresql.conf
wal_level = replica # minimal, replica, or logical
archive_mode = on # enables archiving; off, on, or always #读取命令方式 archive_command ='test ! -f /var/lib/pgsql/13/archive_wals/%f && cp %p /var/lib/pgsql/13/archive_wals/%f'
#重新启动数据库 sudo systemctl restart postgresql-13
说明:配置参数
wal_level 、 archive_mode 需要重启数据库
archive_command 不需要重启数据库,只需要再次加载就可以
reload配置文件
select pg_reload_conf();
查看 archive_command
show archive_command
开启定时清理 -pg_archivecleanup — 清理PostgreSQL WAL 归档文件
修改配置文件 postgresql.conf
archive_cleanup_command = '/bin/bash /var/lib/pgsql/13/scripts/pg_archivecleanup.sh %r 2>>/var/lib/pgsql/13/scripts/cleanup.log'
执行脚本
[root@s101 /var/lib/pgsql/13/scripts]#nano pg_archivecleanup.sh
#!/bin/bash ARCHIVEDIR='/var/lib/pgsql/13/archive_wals/' CHKPOINT=$(find $ARCHIVEDIR -type f -mtime +3 -name '*backup' -printf '%f\n' | sort -r | head -1) cd $ARCHIVEDIR /usr/pgsql-13/bin/pg_archivecleanup -x .00000028.backup $ARCHIVEDIR $CHKPOINT find $ARCHIVEDIR -type f -mtime +1 -a -name '*backup' -a ! -newer $CHKPOINT -delete
常用命令
查看数据目录 show data_directory;
是否开启归档 show archive_mode;
--查看日志目录所有文件select * from pg_ls_logdir();
--查看/data目录下的文件select pg_ls_dir('/data');
输出数据库WAL目录的所有文件
--输出文件总大小,单位是byte select sum(size) from pg_ls_waldir(); select sum(size)/1024/1024 as "文件大小(M)" from pg_ls_waldir();
--查看WAL文件数量,单个wal日志文件大小默认为16MB。 select count(*) from pg_ls_waldir(); select count(*)*16 as "文件大小(M)" from pg_ls_waldir();
查看运行日志的相关配置
show logging_collector;--启动日志收集
show log_directory;--日志输出路径
show log_filename;--日志文件名
show log_truncate_on_rotation;--当生成新的文件时如果文件名已存在,是否覆盖同名旧文件名
show log_statement;--设置日志记录内容
show log_min_duration_statement;--运行XX毫秒的语句会被记录到日志中,-1表示禁用这个功能,0表示记录所有语句,类似mysql的慢查询配置
查询当前lsn (log sequence number)
--1、用到哪些方法select proname from pg_proc where proname like 'pg_%_lsn';proname--------------------------------- pg_current_wal_flush_lsn pg_current_wal_insert_lsn pg_current_wal_lsn pg_last_wal_receive_lsn pg_last_wal_replay_lsn
--2、查询当前的lsn值select pg_current_wal_lsn();
--3、查询当前lsn对应的日志文件select pg_walfile_name('2/81872298');
--4、查询当前lsn在日志文件中的偏移量SELECT * FROM pg_walfile_name_offset(pg_current_wal_lsn());
标签:pgsql,13,wal,备份,PGSQL,pg,var,archive From: https://www.cnblogs.com/ataoxz/p/18426265