Class: Rubber::Commands::BackupDb

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/rubber/commands/util.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descriptionObject



149
150
151
152
153
154
155
# File 'lib/rubber/commands/util.rb', line 149

def self.description
  Rubber::Util.clean_indent( <<-EOS
    Performs a cyclical backup of the database by storing the results of COMMAND
    to the backup directory (and the cloud)
  EOS
  )
end

.subcommand_descriptionObject



145
146
147
# File 'lib/rubber/commands/util.rb', line 145

def self.subcommand_description
  "Performs a cyclical database backup"
end

.subcommand_nameObject



141
142
143
# File 'lib/rubber/commands/util.rb', line 141

def self.subcommand_name
  "util:backup_db"
end

Instance Method Details

#executeObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/rubber/commands/util.rb', line 181

def execute
  signal_usage_error "DIRECTORY, DBUSER, DBHOST, DBNAME are required" unless directory && dbuser && dbhost && dbname
  
  time_stamp = Time.now.strftime("%Y-%m-%d_%H-%M")
  if filename
    backup_file = "#{directory}/#{filename}"
  else
    backup_file = "#{directory}/#{Rubber.env}_dump_#{time_stamp}.sql.gz"
  end
  FileUtils.mkdir_p(File.dirname(backup_file))
  
  # extra variables for command interpolation
  dir = directory
  user = dbuser
  pass = dbpass
  pass = nil if pass && pass.strip.size == 0
  host = dbhost
  name = dbname

  raise "No db_backup_cmd defined in rubber.yml, cannot backup!" unless Rubber.config.db_backup_cmd
  db_backup_cmd = Rubber.config.db_backup_cmd.gsub(/%([^%]+)%/, '#{\1}')
  db_backup_cmd = eval('%Q{' + db_backup_cmd + '}')

  puts "Backing up database with command: '#{db_backup_cmd}'"
  system db_backup_cmd || fail("Command failed: '#{db_backup_cmd.inspect}'")
  puts "Created backup: #{backup_file}"

  cloud_prefix = "db/"
  backup_bucket = Rubber.cloud.env.backup_bucket
  if backup_bucket
    dest = "#{cloud_prefix}#{File.basename(backup_file)}"
    puts "Saving db backup to cloud: #{backup_bucket}:#{dest}"
    Rubber.cloud.storage(backup_bucket).store(dest, open(backup_file))
  end

  tdate = Date.today - age
  threshold = Time.local(tdate.year, tdate.month, tdate.day)
  puts "Cleaning backups older than #{age} days"
  Dir["#{directory}/*"].each do |file|
    if file =~ /#{Rubber.env}_dump_/ && File.mtime(file) < threshold
      puts "Deleting #{file}"
      FileUtils.rm_f(file)
    end
  end

  if backup_bucket
    puts "Cleaning cloud backups older than #{age} days from: #{backup_bucket}:#{cloud_prefix}"
    Rubber.cloud.storage(backup_bucket).walk_tree(cloud_prefix) do |f|
      if f.key =~ /#{Rubber.env}_dump_/ && f.last_modified < threshold
        puts "Deleting #{f.key}"
        f.destroy
      end
    end
  end
  
end