heroku db:pull
如果您熟悉它,我正在尝试使用类似于功能的 Capistrano 任务来做一些事情。
我有一个远程服务器。/path/db_backups/
在该服务器上,我在文件夹中有一堆备份。在那个文件夹中每天都有数据库的备份。
我想做的就是
- 在客户端机器上下载最新的备份。
- 解压它。
- 将其导入本地 mysql 数据库。
有人知道处理这个问题的好方法吗?有没有我不知道的宝石?有手头的脚本吗?
heroku db:pull
如果您熟悉它,我正在尝试使用类似于功能的 Capistrano 任务来做一些事情。
我有一个远程服务器。/path/db_backups/
在该服务器上,我在文件夹中有一堆备份。在那个文件夹中每天都有数据库的备份。
我想做的就是
有人知道处理这个问题的好方法吗?有没有我不知道的宝石?有手头的脚本吗?
我不确定是否有宝石。我通常在 capistrano (config/deploy.rb) 上复制/粘贴此任务,以从服务器中提取压缩数据库并将其存储在我的开发环境中
namespace :utils do
desc 'pull the DB from the server'
task :pull_db, :roles => :db, :only => { :primary => true } do
website = "http://www.my_website.com"
filename = "#{application}.dump.#{Time.now.to_f}.sql"
filename_bz2 = "#{filename}.bz2"
remote_file = "#{current_path}/public/#{filename_bz2}"
text = capture "cat #{deploy_to}/current/config/database.yml"
yaml = YAML::load(text)
on_rollback { run "rm #{remote_file}" }
run "mysqldump -h#{yaml[rails_env]['host']} -u #{yaml[rails_env]['username']} -p #{yaml[rails_env]['database']} | bzip2 -c > #{remote_file}" do |ch, stream, out|
ch.send_data "#{yaml[rails_env]['password']}\n" if out =~ /^Enter password:/
end
local_text = run_locally("cat config/database.yml")
local_yaml = YAML::load(local_text)
run_locally("wget #{website}/#{filename_bz2}")
run_locally("bzip2 -d #{filename_bz2}")
run_locally("bundle exec rake db:drop")
run_locally("bundle exec rake db:create")
if local_yaml['development']['password'] && !local_yaml['development']['password'].blank?
run_locally("mysql -h#{local_yaml['development']['host']} -u#{local_yaml['development']['username']} -p#{local_yaml['development']['password']} #{local_yaml['development']['database']} < #{filename}")
else
run_locally("mysql -h#{local_yaml['development']['host']} -u#{local_yaml['development']['username']} #{local_yaml['development']['database']} < #{filename}")
end
run_locally("rm #{filename}")
run "rm #{remote_file}"
end
end
以下脚本应该实现这一点:
# Find out which file to copy and save its name in a local text file:
# ssh allows you to specify a command that should be executed on the remote
# machine instead of opening a terminal session on it. I use this to get
# a sorted (ls -t sorts by modification date) list of all backups. I then
# truncate this list to one entry using head -1 and save the file name in a
# local file (filename.txt).
# (12.34.56.78 is a placeholder for the ip/hostname of your server)
ssh 12.34.56.78 ls -t /path/to/backups/ | head -1 > filename.txt
# Copy the backup specified in filename.txt to the tmp dir on your local machine.
scp 12.34.56.78:/path/to/backups/`cat filename.txt` /tmp/db_backup.sql.tar
# Untar the backup archive.
cd /tmp && tar -xf db_backup.sql.tar
# Import into database of choice.
mysql -u your_username -p database_to_import_to < /tmp/db_backup.sql
(这假设您在 UNIX 系统上并且安装了 scp 和 tar...)