daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jun 8th, 2017 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Directly copied from eycap-0.5.2 (thanks!)
  2. #
  3. # With these tasks you can:
  4. #   - dump your production database and save it in shared_path/db_backups
  5. #   - dump your production into your local database (clone_to_local)
  6. #
  7. # Tested and fixed by fjguzman
  8. # migrated to capistrano 3 by bjelline
  9.  
  10. namespace :db do
  11.   task :backup_name do
  12.     on roles(:db), primary: true do
  13.       execute "mkdir -p #{shared_path}/db_backups"
  14.       backup_time = Time.now.strftime('%Y-%m-%d--%H-%M-%S')
  15.       set :backup_file, "#{shared_path}/db_backups/#{backup_time}-dump.sql"
  16.     end
  17.   end
  18.  
  19.   desc 'Backup your MySQL or PostgreSQL database to shared_path+/db_backups'
  20.   task :dump do
  21.     on roles(:db), primary: true do
  22.       invoke 'db:backup_name'
  23.       rails_env = fetch(:stage).to_s
  24.  
  25.       data = capture("cat #{shared_path}/config/database.yml")
  26.       environment_info = YAML.safe_load(data)[rails_env]
  27.  
  28.       dbuser = environment_info['username']
  29.       dbpass = environment_info['password']
  30.       environment_database = environment_info['database']
  31.       adapter = environment_info['adapter']
  32.       dbhost = environment_info['host']
  33.  
  34.       pipe = "| bzip2 -c > #{fetch(:backup_file)}.bz2"
  35.       if adapter == 'mysql2'
  36.         cmd = "mysqldump --add-drop-table -u #{dbuser} -h #{dbhost} -p #{environment_database} #{pipe}"
  37.         execute(cmd, interaction_handler: { /Enter password/ => "#{dbpass}\n" })
  38.       elsif adapter == 'pg'
  39.         cmd = "pg_dump -W -c -U #{dbuser} -h #{dbhost} #{environment_database} #{pipe}"
  40.         execute(cmd, interaction_handler: { /Password/ => "#{dbpass}\n" })
  41.       else
  42.         execute "echo \"I don't know how to dump your database adapter=#{adapter}\""
  43.       end
  44.     end
  45.   end
  46.  
  47.   desc 'Sync your production database to your local workstation'
  48.   task :clone_to_local do
  49.     on roles(:db), primary: true do
  50.       invoke 'db:dump'
  51.       remote_file = fetch(:backup_file)
  52.       filename = Pathname.new(remote_file).basename
  53.  
  54.       execute "echo \"Will download #{remote_file}.bz2 to #{filename}.bz2\""
  55.  
  56.       download! "#{remote_file}.bz2", "/tmp/#{filename}.bz2"
  57.       info = YAML.load_file('config/database.yml')['development']
  58.  
  59.       pipe = "bzcat /tmp/#{filename}.bz2 | "
  60.       if info['adapter'] == 'mysql2'
  61.         cmd = "#{pipe} mysql -u #{info['username']} -h #{info['host']} #{info['database']}"
  62.         run_locally do
  63.           execute(cmd, interaction_handler: { /Enter password/ => "#{info['password']}\n" })
  64.         end
  65.       elsif adapter == 'pg'
  66.         cmd = "#{pipe} psql -U #{info['username']} -h #{info['host']} #{info['database']}"
  67.         run_locally do
  68.           execute(cmd, interaction_handler: { /Password/ => "#{info['password']}\n" })
  69.         end
  70.       else
  71.         execute "echo \"I don't know how to load dump for database adapter=#{adapter}\""
  72.       end
  73.     end
  74.   end
  75.  
  76. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top