简体   繁体   English

是否有更快的方式从Heroku而不是水龙头中提取生产数据?

[英]Is There A Faster Way To Pull Production Data From Heroku Than Taps?

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. 即使有一个简单的数据库大小heroku db:拉(点击)需要5分钟以上,似乎很有可能失败。 Is there an alternative method to pull the database? 是否有另一种方法来提取数据库?

Libraries for alternative processes / articles would also be appreciated. 还将赞赏替代流程/文章的图书馆。

Check out pgbackups . 查看pgbackups It has replaced the Heroku bundle command and will give you a the postgres equivalent of mysqldump. 它已经取代了Heroku bundle命令,并且会给你一个相当于mysqldump的postgres。 This is far more civilized than Taps for large datasets. 对于大型数据集,这比Taps更加文明。

heroku pgbackups:capture

Will create a dumpfile and store it. 将创建一个转储文件并存储它。 To download the dumpfile you need the url which you get with 要下载转储文件,您需要使用的URL

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. 这将返回一个url,您可以从中下载转储。 You can paste it into Firefox if you want or use curl/wget like they suggest. 如果你愿意,可以将它粘贴到Firefox中,或者像他们建议的那样使用curl / wget。 The use pg_restore to load the dump file into your database as they say in the docs: 使用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: connecting to database for restore pg_restore:连接到数据库以进行还原

I created a shell script that automates this process (based on Mike Williamson's answer). 我创建了一个自动执行此过程的shell脚本(基于Mike Williamson的回答)。

https://gist.github.com/921535 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

Mike's correct - PGBackups is the way to do this. 迈克是正确的 - PGBackups就是这样做的方法。 When you create a backup with PGBackups, you get access to a standard pg_dump file. 使用PGBackups创建备份时,可以访问标准的pg_dump文件。 Here's the relevant section of the Dev Center PGBackups article. 这是 Dev Center PGBackups文章的相关部分。

This post is quite old right now. 这篇文章现在很老了。

The newest and easiest method right now is using Heroku's pg:pull/pg:push 现在最新,最简单的方法是使用Heroku的pg:pull / pg:push

An update to Jack's script, with Heroku's recommendation as of Jan 2015. Jack的脚本更新, Heroku的建议截至2015年1月。

The first part is due to running on different computers, hence my Postgres dbs has different names. 第一部分是由于在不同的计算机上运行,​​因此我的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! :) =="

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: 这是我编写的一个脚本,它使用pg:pull ,如Lomefin所提到的,从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;"

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). 由于pg:pull克隆拉到新数据库,因此您的工作不会被中断(只有在重命名数据库时才会中断,这需要几分之一秒)。 The script can, of course, be easily customized to your liking. 当然,脚本可以根据自己的喜好轻松定制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM