2

rails dump/restore数据

 2 years ago
source link: http://blog.ilibrary.me/2021/04/15/rails-dump-restore%E6%95%B0%E6%8D%AE
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
rails dump/restore数据
欢迎转载,请支持原创,保留原文链接:blog.ilibrary.me

可以通过gem yaml_db直接dump/restore数据库,省得去记各种数据的dump,restore命令。

确定就是速度非常慢。rails就是慢。 数据库太大就不合适了。

有点就是能跨数据库迁移数据。比如sqlite3到postgres,到mysql的迁移。因为yaml_db用了yaml来保存数据,不是sql脚本,屏蔽了数据库之间脚本的差异。

  1. 添加gem 'yaml_db'Gemfile
  2. bundle install
  3. rails db:data:dump # Dump contents of Rails database to db/data.yml
  4. rails db:data:load # Load contents of db/data.yml into the database
  5. 在服务器上dump的时候没有任何压力
  6. 在服务器上load的时候内存爆了。dump文件4.7G, 服务器内存8G。后来转开发机load, 开发机32G内存. 在开发机上,最高消耗14.1G-
  7. 在开发机上load到psql里面去,任何再用psql到处备份文件,再恢复到服务器的psql上去。
  8. yaml_db不支持外键, 遇到下面的问题:
    ActiveRecord::StatementInvalid: PG::FeatureNotSupported: ERROR:  cannot truncate a table referenced in a foreign key constraint
     DETAIL:  Table "user_comments" references "rainbows".
     HINT:  Truncate table "user_comments" at the same time, or use TRUNCATE ... CASCADE.
    
    1. https://stackoverflow.com/questions/37363637/exporting-database-from-development-to-production

最后还是sqlite dump sql scripts, 手动改sql scripts, 再倒入到psql里面去

  1. 导出sqlite数据: ` sqlite3 rainbow_production.sqlite3 .dump > dump.sql`
  2. 导出schema: sqlite3 rainbow_production.sqlite3 .schema > schema.sql, 这个schema根据自己的实际情况选择用还是不用.
  3. put \set ON_ERROR_STOP on into ~/.psqlrc, 有了这个配置以后psql导入数据出错后就会里面停下来,看到错误信息。
  4. 连接psql: psql -U postgres -h db -d rainbow
  5. 清除ar_internal_metadata里的内容delete from ar_internal_metadata
  6. sql scripts清理:
    1. 清空rainbows: delete from rainbows
    2. 删除第一行: sed -i '' -e '1d' data.sql.bak
    3. 删除data.sql.bak里面末尾sqlite_sequence相关的数据: sed -i '' '/sqlite_sequence/d' data.sql.bak
  7. 导入数据到psql: psql -d rainbowfart_dev -U postgres < data.sql.bak
扫描二维码分享到微信朋友圈Loading...Please wait
qrcode.php?url=%2F2021%2F04%2F15%2Frails-dump-restore%25E6%2595%25B0%25E6%258D%25AE

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK