有没有比 Taps 更快的方法从 Heroku 提取生产数据?

发布于 2024-11-01 01:19:34 字数 135 浏览 0 评论 0原文

我经常需要克隆生产数据来调查错误。即使数据库大小很小,heroku db:pull (taps) 也需要 5 分钟以上,并且失败的可能性很高。 是否有替代方法来提取数据库?

替代流程/文章的库也将受到赞赏。

I often need to clone production data to investigate bugs. Even with a trivial database size heroku db:pull (taps) takes 5+ minutes and seems to have a high chance of failing. Is there an alternative method to pull the database?

Libraries for alternative processes / articles would also be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

叶落知秋 2024-11-08 01:19:34

查看 pgbackups。它取代了 Heroku 的捆绑命令,并且会给你一个相当于 mysqldump 的 postgres 命令。这比大型数据集的 Taps 文明得多。

heroku pgbackups:capture

将创建一个转储文件并存储它。要下载转储文件,您需要使用获得的 url

heroku pgbackups:url b001 (or whatever the id number of the backup is)

,这将返回一个可以从中下载转储的 url。如果您愿意,您可以将其粘贴到 Firefox 中,或者像他们建议的那样使用curl/wget。使用 pg_restore 将转储文件加载到数据库中,如文档中所述:

pg_restore --verbose --clean --no-acl --no-owner -h localhost -U test_user -d myapp_development /home/mike/Downloads/b001.dump

pg_restore:连接到数据库进行恢复

Check out pgbackups. It has replaced the Heroku bundle command and will give you a the postgres equivalent of mysqldump. This is far more civilized than Taps for large datasets.

heroku pgbackups:capture

Will create a dumpfile and store it. To download the dumpfile you need the url which you get with

heroku pgbackups:url b001 (or whatever the id number of the backup is)

That will return an url from which you can download your dump. You can paste it into Firefox if you want or use curl/wget like they suggest. The use pg_restore to load the dump file into your database as they say in the docs:

pg_restore --verbose --clean --no-acl --no-owner -h localhost -U test_user -d myapp_development /home/mike/Downloads/b001.dump

pg_restore: connecting to database for restore

心碎无痕… 2024-11-08 01:19:34

我创建了一个 shell 脚本来自动执行此过程(基于 Mike Williamson 的回答)。

https://gist.github.com/921535

#!/bin/bash

# Best use case is to create a file "update_local_db.sh" in your project folder and then     
# call the command with bash update_local_db

# Follow me: @jackkinsella

function LastBackupName () { 
  heroku pgbackups | tail -n 1 | cut -d"|" -f 1
}

# This part assumes you have a low limit on no. of backups allowed
old_backup=$(LastBackupName)
heroku pgbackups:destroy $old_backup 

heroku pgbackups:capture 
new_backup=$(LastBackupName)
curl $(heroku pgbackups:url $new_backup) > temporary_backup.dump
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U REPLACE_WITH_YOUR_USER -d REPLACE_WITH_YOUR_DB_NAME temporary_backup.dump 
rm -f temporary_backup.dump

I created a shell script that automates this process (based on Mike Williamson's answer).

https://gist.github.com/921535

#!/bin/bash

# Best use case is to create a file "update_local_db.sh" in your project folder and then     
# call the command with bash update_local_db

# Follow me: @jackkinsella

function LastBackupName () { 
  heroku pgbackups | tail -n 1 | cut -d"|" -f 1
}

# This part assumes you have a low limit on no. of backups allowed
old_backup=$(LastBackupName)
heroku pgbackups:destroy $old_backup 

heroku pgbackups:capture 
new_backup=$(LastBackupName)
curl $(heroku pgbackups:url $new_backup) > temporary_backup.dump
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U REPLACE_WITH_YOUR_USER -d REPLACE_WITH_YOUR_DB_NAME temporary_backup.dump 
rm -f temporary_backup.dump
玻璃人 2024-11-08 01:19:34

Mike 是正确的 - PGBackups 就是执行此操作的方法。当您使用 PGBackups 创建备份时,您可以访问标准 pg_dump 文件。 这里是开发中心 PGBackups 文章的相关部分。

Mike's correct - PGBackups is the way to do this. When you create a backup with PGBackups, you get access to a standard pg_dump file. Here's the relevant section of the Dev Center PGBackups article.

稳稳的幸福 2024-11-08 01:19:34

这篇文章现在已经很老了。

目前最新、最简单的方法是使用 Heroku 的 pg:pull /pg:推

This post is quite old right now.

The newest and easiest method right now is using Heroku's pg:pull/pg:push

╰◇生如夏花灿烂 2024-11-08 01:19:34

Jack 的 脚本的更新,其中 Heroku 的建议 截至 2015 年 1 月。

第一部分是由于在不同的计算机上运行,​​因此我的 Postgres dbs 有不同的名称。

#!/bin/bash

# Run the following command: bash update_local_db.sh

# Getting computer name, which is the same as username in Postgres db
echo "Please enter name of Computer"
read input_variable
echo "You entered: $input_variable"

# Make a backup on Heroku
heroku pgbackups:capture --app APP_NAME
echo "== Created a new backup =="

# Download the backup and name it latest.dump
curl -o latest.dump `heroku pgbackups:url --app APP_NAME`
echo "== Downloaded the backup =="

# Restore local db with latest.dump
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U $input_variable -d my_db_name latest.dump
echo "== Replaced db with downloaded =="

# Delete downloaded db latest.dump
rm -f latest.dump
echo "== Deleted downloaded db =="
echo "== Done! :) =="

An update to Jack's script, with Heroku's recommendation as of Jan 2015.

The first part is due to running on different computers, hence my Postgres dbs has different names.

#!/bin/bash

# Run the following command: bash update_local_db.sh

# Getting computer name, which is the same as username in Postgres db
echo "Please enter name of Computer"
read input_variable
echo "You entered: $input_variable"

# Make a backup on Heroku
heroku pgbackups:capture --app APP_NAME
echo "== Created a new backup =="

# Download the backup and name it latest.dump
curl -o latest.dump `heroku pgbackups:url --app APP_NAME`
echo "== Downloaded the backup =="

# Restore local db with latest.dump
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U $input_variable -d my_db_name latest.dump
echo "== Replaced db with downloaded =="

# Delete downloaded db latest.dump
rm -f latest.dump
echo "== Deleted downloaded db =="
echo "== Done! :) =="
鹊巢 2024-11-08 01:19:34

这是我编写的一个脚本,它利用 Lomefin 提到的 pg:pull 来从 Heroku 中拉取数据库并用它替换本地数据库

#!/bin/bash

export MAIN_DB=NAME_OF_LOCAL_DB
export TMP_DB=NAME_OF_TEMPORARY_DB

function delete_db () {
    psql -d ${1} -c "SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '$1'
  AND pid <> pg_backend_pid();" || true

    dropdb ${1} || true
}

delete_db ${TMP_DB}

heroku pg:pull DATABASE_URL ${TMP_DB} || exit 1

delete_db ${MAIN_DB}

psql -c "ALTER DATABASE $TMP_DB RENAME TO $MAIN_DB;"

:克隆到新数据库时,您的工作不会被中断(仅重命名数据库一次,这需要几分之一秒的时间)。当然,可以根据您的喜好轻松定制脚本。

Here is a script I wrote that utilizes pg:pull, as mentioned by Lomefin, to pull down a db from Heroku and replace a local one with it:

#!/bin/bash

export MAIN_DB=NAME_OF_LOCAL_DB
export TMP_DB=NAME_OF_TEMPORARY_DB

function delete_db () {
    psql -d ${1} -c "SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '$1'
  AND pid <> pg_backend_pid();" || true

    dropdb ${1} || true
}

delete_db ${TMP_DB}

heroku pg:pull DATABASE_URL ${TMP_DB} || exit 1

delete_db ${MAIN_DB}

psql -c "ALTER DATABASE $TMP_DB RENAME TO $MAIN_DB;"

Since pg:pull clones to a new database, your work will not be interrupted (only once it renames the db, which takes a fraction of a second). The script can, of course, be easily customized to your liking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文