天天看點

阿裡雲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