Class: Admin::BackupsController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/admin/backups_controller.rb

Overview

TODO: Remove inheritence to Admin::AdminController

Move to background task
Settings prefrences (ie. mysql and zip commands)

Instance Method Summary collapse

Instance Method Details

#databaseObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/admin/backups_controller.rb', line 8

def database    
  connection = YAML.load_file(File.join(RAILS_ROOT, 'config', 'database.yml'))[params[:env] || RAILS_ENV]    
  render :text => 'Database must be MySQL' and return unless connection['adapter'] == 'mysql'
  
  target_file = RAILS_ROOT + '/tmp/backup.sql'
  File.delete(target_file) if File.exists? target_file

  cmd = build_command(connection, target_file)

  if system("#{cmd}")
    send_file target_file, :filename => "#{connection['database']}_#{Time.now.strftime('%d-%m-%y')}.sql"
  else
    render :text => "#{cmd} <br>Backup failed!" and return
  end
end

#filesObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/admin/backups_controller.rb', line 24

def files    
  if request.post?     
    paths = []
    errors = []
    
    params[:paths].each do |path|
      next if path.blank?
      path = File.join(RAILS_ROOT, 'public', path)
      if File.exists? path
        paths << path
      else
        errors << "Path does not exist: #{path}"
      end
  
      errors << 'No paths given' if paths.empty?
  
      unless errors.empty?
        render :text => errors.inspect and return
      end    
    end
  
    # archive
    target_file = RAILS_ROOT + '/tmp/backup.zip'
    File.delete(target_file) if File.exists? target_file
    
    paths.each do |p|

      cmd = "zip -r9 #{target_file} #{p}"
      unless system("#{cmd}")
        errors << "Failed to add: #{p}"
      end
    end

    filename = params[:filename] || 'files'

    if errors.empty?
      send_file target_file, :filename => "#{filename}_#{Time.now.strftime('%d-%m-%y')}.zip"
    else
      render :text => "#{errors.inspect} <br>Backup failed!" and return
    end
  end
end

#indexObject



5
6
# File 'app/controllers/admin/backups_controller.rb', line 5

def index
end