Skip to content

Commit

Permalink
made filesystem operations on local work in Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
akloboucnik authored and everzet committed Apr 16, 2011
1 parent 6ea7d14 commit 09270ba
Showing 1 changed file with 43 additions and 17 deletions.
60 changes: 43 additions & 17 deletions lib/symfony1.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def load_database_config(data, env)
namespace :dump do namespace :dump do
desc "Dump remote database" desc "Dump remote database"
task :remote do task :remote do
filename = "#{application}.remote_dump.#{Time.now.to_i}.sql.bz2" filename = "#{application}.remote_dump.#{Time.now.to_i}.sql.gz"
file = "/tmp/#{filename}" file = "/tmp/#{filename}"
sqlfile = "#{application}_dump.sql" sqlfile = "#{application}_dump.sql"
config = "" config = ""
Expand All @@ -430,70 +430,96 @@ def load_database_config(data, env)


case config['type'] case config['type']
when 'mysql' when 'mysql'
run "mysqldump -u#{config['user']} --password='#{config['pass']}' #{config['db']} | bzip2 -c > #{file}" do |ch, stream, data| run "mysqldump -u#{config['user']} --password='#{config['pass']}' #{config['db']} | gzip -c > #{file}" do |ch, stream, data|
puts data puts data
end end
when 'pgsql' when 'pgsql'
run "pg_dump -U #{config['user']} --password='#{config['pass']}' #{config['db']} | bzip2 -c > #{file}" do |ch, stream, data| run "pg_dump -U #{config['user']} --password='#{config['pass']}' #{config['db']} | gzip -c > #{file}" do |ch, stream, data|
puts data puts data
end end
end end


`mkdir -p backups` require "FileUtils"
FileUtils.mkdir_p("backups")
get file, "backups/#{filename}" get file, "backups/#{filename}"
`cd backups && ln -nfs #{filename} #{application}.remote_dump.latest.sql.bz2` begin
FileUtils.ln_sf(filename, "backups/#{application}.remote_dump.latest.sql.gz")
rescue NotImplementedError # hack for windows which doesnt support symlinks
FileUtils.cp_r("backups/#{filename}", "backups/#{application}.remote_dump.latest.sql.gz")
end
run "rm #{file}" run "rm #{file}"
end end


desc "Dump local database" desc "Dump local database"
task :local do task :local do
filename = "#{application}.local_dump.#{Time.now.to_i}.sql.bz2" filename = "#{application}.local_dump.#{Time.now.to_i}.sql.gz"
tmpfile = "backups/#{application}_dump_tmp.sql"
file = "backups/#{filename}" file = "backups/#{filename}"
config = load_database_config IO.read('config/databases.yml'), symfony_env_local config = load_database_config IO.read('config/databases.yml'), symfony_env_local
sqlfile = "#{application}_dump.sql" sqlfile = "#{application}_dump.sql"


`mkdir -p backups` require "FileUtils"
FileUtils::mkdir_p("backups")
case config['type'] case config['type']
when 'mysql' when 'mysql'
`mysqldump -u#{config['user']} --password='#{config['pass']}' #{config['db']} | bzip2 -c > #{file}` `mysqldump -u#{config['user']} --password=\"#{config['pass']}\" #{config['db']} > #{tmpfile}`
when 'pgsql' when 'pgsql'
`pg_dump -U #{config['user']} --password='#{config['pass']}' #{config['db']} | bzip2 -c > #{file}` `pg_dump -U #{config['user']} --password=\"#{config['pass']}\" #{config['db']} > #{tmpfile}`
end
File.open(tmpfile, "r+") do |f|
gz = Zlib::GzipWriter.open(file)
while (line = f.gets)
gz << line
end
gz.flush
gz.close
end end


`cd backups && ln -nfs #{filename} #{application}.local_dump.latest.sql.bz2` begin
FileUtils.ln_sf(filename, "backups/#{application}.local_dump.latest.sql.gz")
rescue NotImplementedError # hack for windows which doesnt support symlinks
FileUtils.cp_r("backups/#{filename}", "backups/#{application}.local_dump.latest.sql.gz")
end
FileUtils.rm(tmpfile)
end end
end end


namespace :move do namespace :move do
desc "Dump remote database, download it to local & populate here" desc "Dump remote database, download it to local & populate here"
task :to_local do task :to_local do
filename = "#{application}.remote_dump.latest.sql.bz2" filename = "#{application}.remote_dump.latest.sql.gz"
config = load_database_config IO.read('config/databases.yml'), symfony_env_local config = load_database_config IO.read('config/databases.yml'), symfony_env_local
sqlfile = "#{application}_dump.sql" sqlfile = "#{application}_dump.sql"


database.dump.remote database.dump.remote


`bunzip2 -kc backups/#{filename} > backups/#{sqlfile}` require "FileUtils"
f = File.new("backups/#{sqlfile}", "a+")
require "zlib"
gz = Zlib::GzipReader.new(File.open("backups/#{filename}", "r"))
f << gz.read
f.close

case config['type'] case config['type']
when 'mysql' when 'mysql'
`mysql -u#{config['user']} --password='#{config['pass']}' #{config['db']} < backups/#{sqlfile}` `mysql -u#{config['user']} --password=\"#{config['pass']}\" #{config['db']} < backups/#{sqlfile}`
when 'pgsql' when 'pgsql'
`psql -U #{config['user']} --password='#{config['pass']}' #{config['db']} < backups/#{sqlfile}` `psql -U #{config['user']} --password=\"#{config['pass']}\" #{config['db']} < backups/#{sqlfile}`
end end
`rm backups/#{sqlfile}` FileUtils.rm("backups/#{sqlfile}")
end end


desc "Dump local database, load it to remote & populate there" desc "Dump local database, load it to remote & populate there"
task :to_remote do task :to_remote do
filename = "#{application}.local_dump.latest.sql.bz2" filename = "#{application}.local_dump.latest.sql.gz"
file = "backups/#{filename}" file = "backups/#{filename}"
sqlfile = "#{application}_dump.sql" sqlfile = "#{application}_dump.sql"
config = "" config = ""


database.dump.local database.dump.local


upload(file, "/tmp/#{filename}", :via => :scp) upload(file, "/tmp/#{filename}", :via => :scp)
run "bunzip2 -kc /tmp/#{filename} > /tmp/#{sqlfile}" run "gunzip -c /tmp/#{filename} > /tmp/#{sqlfile}"


run "cat #{shared_path}/config/databases.yml" do |ch, st, data| run "cat #{shared_path}/config/databases.yml" do |ch, st, data|
config = load_database_config data, symfony_env_prod config = load_database_config data, symfony_env_prod
Expand Down

0 comments on commit 09270ba

Please sign in to comment.