Class: MysqlS3Backup::Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_s3_backup/backup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mysql, bucket) ⇒ Backup

Returns a new instance of Backup.



8
9
10
11
12
# File 'lib/mysql_s3_backup/backup.rb', line 8

def initialize(mysql, bucket)
  @mysql = mysql
  @bucket = bucket
  @bin_log_prefix = "#{@mysql.database}/bin_logs"
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



6
7
8
# File 'lib/mysql_s3_backup/backup.rb', line 6

def bucket
  @bucket
end

#mysqlObject (readonly)

Returns the value of attribute mysql.



6
7
8
# File 'lib/mysql_s3_backup/backup.rb', line 6

def mysql
  @mysql
end

Instance Method Details

#full(name = make_new_name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mysql_s3_backup/backup.rb', line 14

def full(name=make_new_name)
  lock do
    # When the full backup runs it delete any binary log files that might already exist
    # in the bucket. Otherwise the restore will try to restore them even though they’re
    # older than the full backup.
    @bucket.delete_all @bin_log_prefix
  
    with_temp_file do |file|
      @mysql.dump(file)
      @bucket.store(dump_file_name(name), file)
      @bucket.copy(dump_file_name(name), dump_file_name("latest"))
    end
  end
end

#incrementalObject Also known as: inc



29
30
31
32
33
34
35
# File 'lib/mysql_s3_backup/backup.rb', line 29

def incremental
  lock do
    @mysql.each_bin_log do |log|
      @bucket.store "#{@bin_log_prefix}/#{File.basename(log)}", log
    end
  end
end

#restore(name = "latest") ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mysql_s3_backup/backup.rb', line 38

def restore(name="latest")
  lock do
    # restore from the dump file
    with_temp_file do |file|
      @bucket.fetch(dump_file_name(name), file)
      @mysql.restore(file)
    end
  
    if name == "latest"
      # Restoring binary log files
      @bucket.find("#{@bin_log_prefix}/").sort.each do |log|
        with_temp_file do |file|
          @bucket.fetch log, file
          @mysql.apply_bin_log file
        end
      end
    end
  end
end