天天看点

阿里云postgreSQL数据库逻辑备份

一、创建阿里云存储网关

参考链接:

https://help.aliyun.com/document_detail/108244.html
阿里云postgreSQL数据库逻辑备份

注意购买OSS bucket的区域与数据库实例所在的区域不同。

二、在与存储网关同一区域的ECS机器上面,挂载存储网关:

mount.nfs x.x.x.x:/shares /oss

x.x.x.x:/shares是网关的挂载点,/oss为本地目录

https://help.aliyun.com/document_detail/108284.html

最好将nfs挂载点也写入/etc/fstab文件,重启自动挂载。

三、在ECS机器上安装postgreSQL备份工具

1、

https://www.postgresql.org/ftp/source/

下载相应的数据库版本(与云rds版本相近)

2、解压、安装编译

安装目录为:/usr/local/pgsql/

gunzip postgresql-10.1.tar.gz

tar xf postgresql-10.1.tar

./configure --prefix=/usr/local/pgsql/

make

make install

在pg_dump用户目录下,新建.pgpass文件,权限设为600,或者更小的权限

格式形如: hostname:port:database:username:password

四、编写postgreSQL备份脚本

#!/bin/bash
hostname=xxx.pg.rds.aliyuncs.com
username=xxx
port=xxx
database=xxx
dt=`date +%Y%m%d`
/usr/local/pgsql/bin/pg_dump -h $hostname -U $username -p $port -d $database -o -f /oss/db_$dt.bak

if [ -z "`find /oss -name "*.bak" -mtime 0 -print0`" ]
then
    echo "warning!postgreSQL_backup is failure,please check it!" | mail -s postgreSQL-backup [email protected]
fi           

将脚本添加进任务计划中,即可。

五、还原方法

登录ECS主机,执行命令:

/usr/local/pgsql/bin/pgsql -h xxx.pg.rds.aliyuncs.com -U xxx -d xxx < db_xxx.bak