简体   繁体   English

Rails rake任务将实时(MySQL)数据库加载到本地开发数据库

[英]Rails rake task for loading live (MySQL) database to local development database

For years I've used a ssh pipe from mysqldump on the live server to mysql on my development machine for getting a copy of the current data. 多年来我一直使用来自live server上的mysqldump的ssh管道到我的开发机器上的mysql来获取当前数据的副本。

ssh -C <server> mysqldump --opt <live_database_name> |mysql <local_dev_database_name>

Where -C enables ssh compression and --opt enables quickness and completeness. 其中-C启用ssh压缩,而--opt启用快速和完整性。

Does anyone have a rails-ish equivalent rake task for this? 有人有一个rails-ish等效的rake任务吗? Ideally it'd take the database names from config/database.yml 理想情况下,它需要从config / database.yml获取数据库名称

https://gist.github.com/750129 https://gist.github.com/750129

This is not an elegant solution. 这不是一个优雅的解决方案。 It's basically a wrapper for your old method, so it's not even compatible with other database drivers. 它基本上是旧方法的包装器,因此它甚至不兼容其他数据库驱动程序。

But it is something you can put in your SCM under lib/tasks to share it with other developers on your team. 但是,您可以在lib / tasks下将其放入SCM中,以便与团队中的其他开发人员共享。 It also uses config data from your existing config/database.yml file. 它还使用现有config / database.yml文件中的配置数据。 You define the live db by simply adding another branch to that file and it uses the same key names that Rails do. 您可以通过简单地向该文件添加另一个分支来定义实时数据库,它使用与Rails相同的密钥名称。

Maybe it would even make sense to reuse the production database configuration. 也许重用生产数据库配置甚至是有意义的。

Here's one I use for a Postgres database: https://gist.github.com/748222 . 这是我用于Postgres数据库的一个: https//gist.github.com/748222

There are three tasks: db:download , db:replace and db:restore . 有三个任务: db:downloaddb:replacedb:restore db:restore is just a wrapper around the other two. db:restore只是另外两个的包装器。

I'd say you could do something similar for Mysql pretty quickly as well. 我会说你也可以很快地为Mysql做类似的事情。 I just use the latest backup in this case instead of creating it at runtime. 我只是在这种情况下使用最新的备份,而不是在运行时创建它。

If you are fine with coding in Ruby you might want to look into seed-fu and activerecord-import . 如果您在Ruby中编码很好,您可能需要查看seed-fuactiverecord-import

Oh, I forgot to mention standalone-migrations . 哦,我忘了提到独立迁移 It comes with a Rake task for schema migrations. 它附带了一个用于架构迁移的Rake任务。

Good luck! 祝好运!

我没有使用rake来执行此操作,而是使用capistrano任务,因为您的capistrano任务已经知道生产服务器的位置等等...

Theoretically you should be able to create another "instance" in database.yml (for your live server) and put the correct host there (and leave the rest username/passwords etc). 从理论上讲,你应该能够在database.yml中创建另一个“实例”(对于你的实时服务器)并在那里放置正确的主机(并保留其余的用户名/密码等)。 In your rake task you would load the database yaml file, read the host and go on with your command line slurpiness. 在你的rake任务中,你将加载数据库yaml文件,读取主机并继续你的命令行slurpiness。 It could easily be wrapped up in a rake task. 它可以很容易地包含在rake任务中。

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

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