Method: Backuper#perform_database_backup

Defined in:
lib/backuper.rb

#perform_database_backupObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/backuper.rb', line 48

def perform_database_backup
  if mysql_params == {} || mysql_params['database'] == '' || !(mysql_params['adapter'] =~ /mysql/)
    puts "Skipping MySQL backup as no configurtion given"
    return
  end
  
  timestamp = "#{Time.now.strftime('%Y-%m-%d-%s')}"
  local_current_backup_path = "#{local_backup_base_path}/mysql/#{timestamp}.sql"
  local_latest_backup_path = Dir["#{local_backup_base_path}/mysql/*.sql"].sort.last
  
  unless File.exist?("#{local_backup_base_path}/mysql")
    system "mkdir #{local_backup_base_path}/mysql"
  end
  
  # Local backup
  
  puts "Performing local backup of database '#{mysql_params['database']}' as user '#{mysql_params['username']}'"
  system "nice -n 10 mysqldump #{mysql_params['database']} --user=#{mysql_params['username']} --password=#{mysql_params['password']} > #{local_current_backup_path}"
  
  # Cleanup of old versions
  
  Dir["#{local_backup_base_path}/mysql/*"].sort.reverse.each_with_index do |backup_version, index|
    if index >= max_kept_backups
      system "rm -rf #{backup_version}"
    end
  end
end