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, timeout) ⇒ Backup

Returns a new instance of Backup.



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

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

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



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

def bucket
  @bucket
end

#mysqlObject (readonly)

Returns the value of attribute mysql.



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

def mysql
  @mysql
end

Instance Method Details

#full(name = make_new_name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mysql_s3_backup/backup.rb', line 16

def full(name=make_new_name)
  lock do
    timeout 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
end

#incrementalObject Also known as: inc



33
34
35
36
37
38
39
40
41
# File 'lib/mysql_s3_backup/backup.rb', line 33

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

#restore(name = "latest") ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mysql_s3_backup/backup.rb', line 44

def restore(name="latest")
  lock do
    timeout 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
end