备份和恢复数据库的Rake任务[英] Rake task to backup and restore database

本文是小编为大家收集整理的关于备份和恢复数据库的Rake任务的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在从事一个铁路项目,有时我在家中,有时在工作中进行编程.在我的开发过程中,我将数据添加到数据库中,并且我确实需要一种在家中数据库同步的方法.

我正在考虑一项启动任务以备份/还原铁轨应用程序.
无论如何都有吗?

推荐答案

写一个耙子任务:

namespace :db do
  task :backup do
    system "mysqldump --opt --user=root --password rose userdetails> xyz.sql"
  end

  task :restore do
    system "mysqldump --user=root --password  < xyz.sql"
  end
end

rake db:backup您将获得可以投入git/svn的SQL,一旦您在家工作以还原并运行rake db:restore

其他推荐答案

我使用将数据库转储到特定位置的脚本,第二个可以获取转储并使用它来还原指定的数据库.我每天使用GEM来安排每日备份(通过调用第一个脚本),将其放在附表.rb文件中:

  every :day, :at => "05:00" do
    command "/var/www/current/script/db_backup.sh -n #{@db_name}"
  end

脚本的确切内容取决于您正在使用的数据库.当我使用postgresql时,备份脚本,在找出转储的正确位置后,运行pg_dump:

pg_dump -F t -U username -f file_location<timestamp>.dat database_name

和" Restore"脚本,我用来将生产备份复制到本地数据库进行测试,使用PG_RESTORE:

pg_restore -U username -O -x -d database_name_new path/to/file

如果您使用的是其他一些数据库,这些工具显然会有所不同,但是大多数数据库都以某种形式支持备份和恢复.

本文地址:https://www.itbaoku.cn/post/597674.html

问题描述

I am working on a Rails project, and sometimes I program at home and sometimes at work. In my development process, I add data to the database, and I really need a way to synchronize the databases at home and work.

I am thinking about a Rake task to backup/restore the whole database in a Rails app.
Is there anyway to do that?

推荐答案

write a rake task:

namespace :db do
  task :backup do
    system "mysqldump --opt --user=root --password rose userdetails> xyz.sql"
  end

  task :restore do
    system "mysqldump --user=root --password  < xyz.sql"
  end
end

By the rake db:backup you will get the sql that you can commit to your git/svn and once you work from home to restore pull it and run rake db:restore

其他推荐答案

I use a script that dumps the the database to a particular location, and a second that fetches the dump and uses it to restore a specified database. I use the Whenever gem to schedule daily backups (by calling the first script), by putting this in the schedule.rb file:

  every :day, :at => "05:00" do
    command "/var/www/current/script/db_backup.sh -n #{@db_name}"
  end

The exact contents of the script depends on what database you're using. As I'm using postgreSQL, the backup script, after figuring the proper location for the dump, runs pg_dump:

pg_dump -F t -U username -f file_location<timestamp>.dat database_name

And the 'restore' script, which I use to copy the production backup to a local database for testing, uses pg_restore:

pg_restore -U username -O -x -d database_name_new path/to/file

If you're using some other database, these tools would obviously be different, but most databases support backup and restoration in some form.