lib/backup/environment/unix.rb in backup-2.3.2.pre3 vs lib/backup/environment/unix.rb in backup-2.3.2

- old
+ new

@@ -1,11 +1,10 @@ module Backup module Environment module Unix require 'active_record' - require 'optparse' # Sets BACKUP_PATH BACKUP_PATH = ENV['BACKUP_PATH'] || "/opt/backup" # Sets DB_CONNECTION_SETTINGS @@ -19,13 +18,13 @@ module Commands def setup unless File.directory?(BACKUP_PATH) puts "Installing Backup in #{BACKUP_PATH}.." - %x{ sudo mkdir -p #{File.join(BACKUP_PATH, 'config')} } - %x{ sudo cp #{File.join(File.dirname(__FILE__), '..', '..', '..', 'setup', 'backup.sqlite3')} #{BACKUP_PATH} } - %x{ sudo cp #{File.join(File.dirname(__FILE__), '..', '..', '..', 'setup', 'backup.rb')} #{File.join(BACKUP_PATH, 'config')} } + %x{ #{sudo} mkdir -p #{File.join(BACKUP_PATH, 'config')} } + %x{ #{sudo} cp #{File.join(File.dirname(__FILE__), '..', '..', '..', 'setup', 'backup.sqlite3')} #{BACKUP_PATH} } + %x{ #{sudo} cp #{File.join(File.dirname(__FILE__), '..', '..', '..', 'setup', 'backup.rb')} #{File.join(BACKUP_PATH, 'config')} } puts <<-MESSAGE ============================================================== Backup has been set up! ============================================================== @@ -70,11 +69,11 @@ end end def remove puts "Removing Backup..\n" - %x{ sudo rm -rf #{BACKUP_PATH} } + %x{ #{sudo} rm -rf #{BACKUP_PATH} } end end module Helpers @@ -84,11 +83,27 @@ puts "Did you set up Backup? Do so if you haven't yet:" puts "\nbackup --setup\n " exit end end - + + def sudo + if writable?(BACKUP_PATH) + "" + else + "sudo" + end + end + + private + def writable?(f) + unless File.exists?(f) + writable?(File.dirname(f)) + else + File.writable?(f) + end + end end end end -end \ No newline at end of file +end