best way to run tasks locally?

10 views
Skip to first unread message

Rob Jonson

unread,
Dec 6, 2017, 6:35:30 AM12/6/17
to Capistrano
I have some database tasks which work fantastically for my standard stages (beta, deploy).
They're doing things like triggering a backup, downloading it and restoring.

that's great for getting the production db and copying it up to beta.

I'd like to be able to restore the backup on my local development machine too. Is there a sensible way I can do this?

something like?
cap development db:restore

how would I set up my development stage to say 'connect on localhost, don't worry about ssh'

the restore task is...



  desc
'restore backup'
  task
:restore do
    on primary
:web do


     
raise "Cant restore to production without deleting this line\n" if (fetch(:stage) == :production)


      backup_file
= "tmp/rails_database.tar"
      backup_destination
= "#{shared_path}/tmp/rails_database.tar"


      upload
! backup_file, backup_destination
      execute
"tar -xvf #{backup_destination} -C #{shared_path}/tmp --strip-components=2"
      execute
:rm, backup_destination


      sql_file
= "#{shared_path}/tmp/MySQL.sql.gz"
      secrets
= current_secrets(fetch(:stage).to_s)


      mysql_command
= mysql(secrets)
      execute
"gzip -d -c #{sql_file} | #{mysql_command}"




   
end
 
end

thank you!


Versions:
  • Ruby 2.4.2
  • Capistrano 3.9.1
  • Rails 5.1
Platform:
  • Mac OS

Lee Hambley

unread,
Dec 6, 2017, 6:57:12 AM12/6/17
to Capistrano
For local tasks, simply use Rake. Because Capistrano sub-classes Rake, and your Capfile is a Rakefile with a different name, you can do the following:

namespace :db do
   task :something_local do
      sh "anything you like"
   end
end

#sh comes from Rake, and is documented here:


You lose a little of the logging niceness from Airbrussh/SSHKit, but there's no SSH in the game, so I think it's acceptable that hostnames/IPs, etc are not logged, as it's implicit.

--
You received this message because you are subscribed to the Google Groups "Capistrano" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capistrano+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/capistrano/c5d48803-c211-48e1-aef0-9b701392febb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages