Top Level Namespace

Defined Under Namespace

Modules: Mina

Constant Summary collapse

COMMAND =
"  method = ARGV[0]\n  dc = JSON.parse(ARGV[2])\n  adapter = dc[\"adapter\"]\n  database = dc[\"database\"]\n  host = dc[\"host\"]\n  username = dc[\"username\"]\n  password = dc[\"password\"]\n  port = dc[\"port\"]\n  arguments = \"\"\n  case adapter.to_s\n  when \"postgresql\"\n    arguments += \"PGPASSWORD=\\\\\"\" + password + \"\\\\\" \" if password\n    arguments += method == \"dump\" ? \"pg_dump\" : \"psql -q\"\n    arguments += \" -d \" + database if database\n    arguments += \" -h \" + host if host\n    arguments += \" -U \" + username if username\n    arguments += \" -p \" + port.to_s if port\n    arguments += method == \"dump\" ? \" -O -c\" : \"\"\n  when \"mysql2\"\n    arguments += method == \"dump\" ? \"mysqldump\" : \"mysql\"\n    arguments += \" \" + database if database\n    arguments += \" -h \" + host if host\n    arguments += \" -u \" + username if username\n    arguments += \" --password=\" + password if password\n    arguments += \" -P \" + port.to_s if port\n  end\n  arguments += method == \"dump\" ? \" > \" : \" < \"\n  arguments += ARGV[1]\n  puts arguments\n"
DATA_SYNC =
"  function data_sync {\n    ruby -rjson -e '\#{COMMAND}' \"$@\"\n  };\n"

Instance Method Summary collapse

Instance Method Details

#dump_restore(rails_root, backup_path, mode: :dump, backend: :local) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/mina/data_sync/helpers.rb', line 39

def dump_restore(rails_root, backup_path, mode: :dump, backend: :local)
  comment %{#{mode == :dump ? 'Dumping' : 'Restoring'} database}
  command %{cd #{rails_root}}
  command DATA_SYNC.to_s
  command %{mkdir -p #{backup_path}}
  command %{CONFIG=$(RAILS_ENV=#{backend == :local ? 'development' : fetch(:rails_env)} bundle exec rails runner "puts ActiveRecord::Base.connection.instance_variable_get(:@config).to_json")}
  comment %{$(data_sync "#{mode}" "#{backup_path}/#{fetch(:backup_file)}" "$CONFIG")}
  command %{eval $(data_sync "#{mode}" "#{backup_path}/#{fetch(:backup_file)}" "$CONFIG")}
end

#rsync_db(mode: :remote_to_local) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mina/data_sync/helpers.rb', line 49

def rsync_db(mode: :remote_to_local)
  if mode == :remote_to_local
    run :local do
      comment %{Copying backup}
      command %{mkdir -p #{fetch(:local_backup_path)}}
      comment %{Backup: #{fetch(:local_backup_path)}/#{fetch(:backup_file)}}
      command %{rsync --progress -e "ssh -p #{fetch(:port, 22)}" #{fetch(:user)}@#{fetch(:domain)}:#{fetch(:current_path)}/#{fetch(:remote_backup_path)}/#{fetch(:backup_file)} #{fetch(:local_backup_path)}/#{fetch(:backup_file)}}
    end
  else
    run :remote do
      command %{mkdir -p #{fetch(:current_path)}/#{fetch(:remote_backup_path)}}
    end
    run :local do
      comment %{Copying backup}
      comment %{Backup: #{fetch(:remote_backup_path)}/#{fetch(:backup_file)}}
      command %{rsync --progress -e "ssh -p #{fetch(:port, 22)}" #{fetch(:local_backup_path)}/#{fetch(:backup_file)} #{fetch(:user)}@#{fetch(:domain)}:#{fetch(:current_path)}/#{fetch(:remote_backup_path)}/#{fetch(:backup_file)}}
    end
  end
end