大网数据广告活动
服务器运维问答

How to call pg_dump from Google Cloud SQL

· 52CE 我爱测

I am looking for a solution to backup my Cloud SQL schemas (and Cloud Storage files). At first I tried using Cloud Scheduler to call the googleapis ( https://cloud.google.com/sql/docs/postgres/import-export/exporting ) in order t…

问题原文

原帖链接 · Jasper · CC BY-SA 4.0

I am looking for a solution to backup my Cloud SQL schemas (and Cloud Storage files).

At first I tried using Cloud Scheduler to call the googleapis (https://cloud.google.com/sql/docs/postgres/import-export/exporting) in order to make an export of the database. The problem is that I need specific backup arguments, and I need to export specific schemas in the PostgreSQL database. Each of my clients has a schema, and i would like to be able to restore a schema without restoring the entire database. Googleapis don't seem to support exporting specific schemas (yet), which is why I would like to call pg_dump directly.

The same problem is with using automatic backups in Cloud SQL. I cannot restore a specific schema in a database, and I need to keep the backups around for atleast a month.

However, I have yet to find a way to call pg_dump using e.g. Cloud Functions. So currently I am thinking about using my own Windows 10 laptop to call pg_dump and to setup a daily task in Windows. I would much rather use Google Cloud, instead of my own laptop.

If anybody has any ideas of how to I should build my backup infrastructure, it would be greatly appreciated.

社区高分解答

原帖链接 · Murat Karyağdı · CC BY-SA 4.0

Connect your cloudsql instance with proxy > https://cloud.google.com/sql/docs/postgres/sql-proxy

then this commands should do the job

pg_dump -U postgres -h localhost -p [PORT] [DBNAME] -n public --format=p  >publicBackup.sql

psql -U postgres -h localhost -p [PORT] [DBNAME]  -c "drop schema public cascade;" 

psql -U postgres -h localhost -p [PORT] [DBNAME] -n public -1 -f  ./publicBackup.sql

You can do this job from any container which have a proxy connection to your database.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-07-31)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。