Regular backup of postgresql database on Linux

Regular backup of postgresql database on Linux

In order to avoid the risk of operating the database, we plan to back up the postgresql database daily

1. Server address:
backup server address 10.10.10.101 (fictional)
the server address where the postgresql database is located 10.10.10.100 (fictional)

Because I have a separate backup database that does not operate on the same server. First of all, I must ensure that the networks of these two servers are connected.
I use shell scripts and the timing tasks that come with Linux. The steps are as follows:

**

One to create a backup shell script

**
1. Create a backup script storage path:
mkdir -p /u01/db-bak/2/bak-file/all

mkdir -p /u01/db-bak/2/bak-file/all

I store the shell script in /u01/db-bak/2 and put the backed up pgsql.backup in the file /u01/db-bak/2/bak-file/all
2. Prepare the shell script and
enter /u01/db-bak /2

cd  /u01/db-bak/2

Create a backup shell script

vim  pgsql-all-backup.sh

shell script

#!/bin/bash
echo "开始执行 PostgreSql 数据库hrmw的备份!"
echo "backup ing -------------------"
nowtime=$(date +%F+%T)
export PGPASSWORD=123
echo "时间:" $nowtime
set timeout 500
/usr/pgsql-11/bin/pg_dump --file "/u01/db-bak/2/bak-file/all/pgsql-all-"$nowtime".backup" --host "10.10.10.100" --port "5432" --username "postgres" --dbname "hrmw" --verbose --role "postgres" --format=c --blobs --encoding "UTF8"
echo "数据库 hrmw 备份结束!"
exit;

nowtime=$(date +%F+%T) --current time
PGPASSWORD='database password'

Usage of pg_dump

~                                                                                                                                                                                                                                                
用法:                                                                                                                                                                                                                                            
  pg_dump [选项]... [数据库名字]                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                
一般选项:                                                                                                                                                                                                                                        
  -f, --file=FILENAME          输出文件或目录名                                                                                                                                                                                                  
  -F, --format=c|d|t|p         输出文件格式 (定制, 目录, tar                                                                                                                                                                                     
                               明文 (默认值))                                                                                                                                                                                                    
  -j, --jobs=NUM               执行多个并行任务进行备份转储工作                                                                                                                                                                                  
  -v, --verbose                详细模式                                                                                                                                                                                                          
  -V, --version                输出版本信息,然后退出                                                                                                                                                                                            
  -Z, --compress=0-9           被压缩格式的压缩级别                                                                                                                                                                                              
  --lock-wait-timeout=TIMEOUT  在等待表锁超时后操作失败                                                                                                                                                                                          
  --no-sync                    不用等待变化安全写入磁盘                                                                                                                                                                                          
  -?, --help                   显示此帮助, 然后退出                                                                                                                                                                                              
~                                                                                                                                                                                                                                                
控制输出内容选项:                                                                                                                                                                                                                                
  -a, --data-only              只转储数据,不包括模式                                                                                                                                                                                             
  -b, --blobs                  在转储中包括大对象                                                                                                                                                                                                
  -B, --no-blobs               排除转储中的大型对象                                                                                                                                                                                              
  -c, --clean                  在重新创建之前,先清除(删除)数据库对象                                                                                                                                                                          
  -C, --create                 在转储中包括命令,以便创建数据库                                                                                                                                                                                   
  -E, --encoding=ENCODING      转储以ENCODING形式编码的数据                                                                                                                                                                                      
  -n, --schema=SCHEMA          只转储指定名称的模式                                                                                                                                                                                              
  -N, --exclude-schema=SCHEMA  不转储已命名的模式                                                                                                                                                                                                
  -o, --oids                   在转储中包括 OID                                                                                                                                                                                                  
  -O, --no-owner               在明文格式中, 忽略恢复对象所属者
  -s, --schema-only            只转储模式, 不包括数据
  -S, --superuser=NAME         在明文格式中使用指定的超级用户名
  -t, --table=TABLE            只转储指定名称的表
  -T, --exclude-table=TABLE    不转储指定名称的表
  -x, --no-privileges          不要转储权限 (grant/revoke)
  --binary-upgrade             只能由升级工具使用
  --column-inserts             以带有列名的INSERT命令形式转储数据
  --disable-dollar-quoting     取消美元 (符号) 引号, 使用 SQL 标准引号
  --disable-triggers           在只恢复数据的过程中禁用触发器
  --enable-row-security        启用行安全性(只转储用户能够访问的内容)
  --exclude-table-data=TABLE   不转储指定名称的表中的数据
  --if-exists                  当删除对象时使用IF EXISTS
  --inserts                    以INSERT命令,而不是COPY命令的形式转储数据
  --load-via-partition-root    通过根表加载分区
  --no-comments                不转储注释
  --no-publications            不转储发布
  --no-security-labels         不转储安全标签的分配
  --no-subscriptions           不转储订阅
  --no-synchronized-snapshots  在并行工作集中不使用同步快照
  --no-tablespaces             不转储表空间分配信息
  --no-unlogged-table-data     不转储没有日志的表数据
  --quote-all-identifiers      所有标识符加引号,即使不是关键字
  --section=SECTION            备份命名的节 (数据前, 数据, 及 数据后)
  --serializable-deferrable    等到备份可以无异常运行
  --snapshot=SNAPSHOT          为转储使用给定的快照
  --strict-names               要求每个表和()schema包括模式以匹配至少一个实体
  --use-set-session-authorization
                               使用 SESSION AUTHORIZATION 命令代替
                               ALTER OWNER 命令来设置所有权

联接选项:
  -d, --dbname=DBNAME      对数据库 DBNAME备份
  -h, --host=主机名        数据库服务器的主机名或套接字目录
  -p, --port=端口号        数据库服务器的端口号
  -U, --username=名字      以指定的数据库用户联接
  -w, --no-password        永远不提示输入口令
  -W, --password           强制口令提示 (自动)
  --role=ROLENAME          在转储前运行SET ROLE

如果没有提供数据库名字, 那么使用 PGDATABASE 环境变量
的数值.

**

Two create a shell script to clear the backup regularly

**
Daily database backup will take up a lot of memory if it is not cleaned up, and manual cleanup is too troublesome. Let him perform it automatically.

The path is consistent with the backup shell, which is convenient for management. There is no need to create
one here. 1. Prepare the shell script and
enter /u01/db-bak/2

cd  /u01/db-bak/2

Create delete shell script

vim  delete-pgbak.sh

Delete shell script

#!/bin/bash
echo "删除 8 天前的 数据库备份文件!"
find /u01/db-bak/2/bak-file/all/ -name "pgsql-all*" -mtime +7 -exec rm -rf {
    
    } \;
set timeout 1000
echo " 8 天前的数据库备份文件删除完毕!"

**

Three configuration timing tasks

**
1. Add timed tasks

[root@host-10-10-10-101 db-bak]# crontab -e                   

The database backup script is performed at 23:00 every day. The delete script is run at 23:30 every day.

30 23 * * * /u01/db-bak/2/delete-pgbak.sh
	
0 23 * * * /u01/db-bak/2/pgsql-all-backup.sh                   

2. View timed tasks

[root@host-10-10-10-101 db-bak]# crontab -l                   
	30 23 * * * /u01/db-bak/2/delete-pgbak.sh
	
	0 23 * * * /u01/db-bak/2/pgsql-all-backup.sh

**

Four grant permissions

**
Configuration permissions chmod

chmod +x /u01/db-bak/2/*

**

Five test shell script

**

[root@host-10-10-10-101 2]# bash pgsql-all-backup.sh
开始执行 PostgreSql 数据库hrmw的备份!
backup ing -------------------
时间: 2020-12-17+14:47:53
pg_dump: 最后的内置 OID16383
pg_dump: 读扩展
pg_dump: 识别扩展成员
pg_dump: 读取模式
pg_dump: 读取用户定义表
pg_dump: 读取用户定义函数
pg_dump: 读取用户定义类型
pg_dump: 读取过程语言
pg_dump: 读取用户定义聚集函数
pg_dump: 读取用户定义操作符
pg_dump: 读取用户定义的访问方法
pg_dump: 读取用户定义操作符集
pg_dump: 读取用户定义操作符
pg_dump: 读取用户定义的文本搜索解析器
pg_dump: 读取用户定义的文本搜索模板
pg_dump: 读取用户定义的文本搜索字典
pg_dump: 读取用户定义的文本搜索配置
pg_dump: 读取用户定义外部数据封装器
pg_dump: 读取用户定义的外部服务器
......
数据库 hrmw2 备份结束!
[root@host-10-10-10-101 2]# bash delete-pgbak.sh
删除 8 天前的 数据库备份文件!
 8 天前的数据库备份文件删除完毕!

Guess you like

Origin blog.csdn.net/yang_z_1/article/details/111313731