使用capistrano部署rails应用程序的文件备份

时间:2016-06-03 12:00:57

标签: ruby-on-rails-4 backup capistrano

我正在将带有capistrano的rails 4应用程序部署到Ubuntu 14.04服务器上。这是我目前使用的deploy.rb

server 'ip', port: port, roles: [:web, :app, :db], primary: true

set :repo_url,        'git git'
set :application,     'appname'
set :user,            'user'
set :puma_threads,    [4, 16]
set :puma_workers,    0

set :pty,             true
set :use_sudo,        false
set :stage,           :production
set :deploy_via,      :remote_cache
set :deploy_to,       "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind,       "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state,      "#{shared_path}/tmp/pids/puma.state"
set :puma_pid,        "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log,  "#{release_path}/log/puma.access.log"
set :ssh_options,     { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true  # Change to false when not using ActiveRecord

## Defaults:
# set :scm,           :git
# set :branch,        :master
# set :format,        :pretty
# set :log_level,     :debug
# set :keep_releases, 5

## Linked Files & Directories (Default None):
set :linked_dirs,  %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/uploads}

namespace :puma do
  desc 'Create Directories for Puma Pids and Socket'
  task :make_dirs do
    on roles(:app) do
      execute "mkdir #{shared_path}/tmp/sockets -p"
      execute "mkdir #{shared_path}/tmp/pids -p"
    end
  end

  before :start, :make_dirs
end

namespace :deploy do
  desc "Make sure local git is in sync with remote."
  task :check_revision do
    on roles(:app) do
      unless `git rev-parse HEAD` == `git rev-parse origin/master`
        puts "WARNING: HEAD is not the same as origin/master"
        puts "Run `git push` to sync changes."
        exit
      end
    end
  end

  desc 'Initial Deploy'
  task :initial do
    on roles(:app) do
      before 'deploy:restart', 'puma:start'
      invoke 'deploy'
    end
  end

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      invoke 'puma:restart'
    end
  end

  before :starting,     :check_revision
  after  :finishing,    :compile_assets
  after  :finishing,    :cleanup
  after  :finishing,    :restart
end


namespace :sake do
  desc "Run a task on a remote server."
  # run like: cap staging sake:invoke task="a_certain_task"
  task :invoke do
    on roles(:all) do |h|
      execute "cd #{fetch(:deploy_to)}/current && bundle exec rake #{ENV['task']} RAILS_ENV=#{fetch(:rails_env)}"
    end
  end
end

# ps aux | grep puma    # Get puma pid
# kill -s SIGUSR2 pid   # Restart puma
# kill -s SIGTERM pid   # Stop puma

我在此文件中遇到问题,导致我的shared/public文件夹被覆盖,我丢失了大量上传的图片。由于这种损失,我现在正在考虑在服务器上的应用程序目录之外创建一些备份文件的方法。

是否有可能创建某种自动例程,在capistrano部署新版本之前将当前共享文件夹复制到服务器上的保存空间?

问候。

1 个答案:

答案 0 :(得分:0)

是的,您可能希望在部署新版本之前挂钩Capistrano的deploy:started步骤来执行备份。

定义执行备份的任务:

task :backup_shared_public do
  on roles(:all) do
    # Very basic backup technique. YMMV!
    execute "mkdir -p ~/public_backups" 
    execute "cp -Rp #{shared_path.join('public')} ~/public_backups/#{Time.now.to_i}"
  end
end

然后告诉Capistrano在每次部署之前执行它:

after "deploy:started", "backup_shared_public"