Skip to content

Instantly share code, notes, and snippets.

@r3bo0t
Last active December 19, 2015 21:39
Show Gist options
  • Save r3bo0t/6021931 to your computer and use it in GitHub Desktop.
Save r3bo0t/6021931 to your computer and use it in GitHub Desktop.
Customizing s3 database backup to keep only last 5 db entries
require 'mysql_s3_backup'
class MysqlS3Dumper
attr_accessor :config
class MysqlS3Backup::Backup
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|
puts file
@mysql.dump(file)
@bucket.store(dump_file_name(name), file)
@bucket.copy(dump_file_name(name), dump_file_name("latest"))
@bucket.keep_last_five
end
end
end
end
class MysqlS3Backup::Bucket
def keep_last_five
puts @name
puts "coming to bucket"
all_backups = AWS::S3::Bucket.objects(@name)
while all_backups.count > 6
AWS::S3::Bucket.objects(@name).first.delete
all_backups = AWS::S3::Bucket.objects(@name)
end
end
end
def initialize
@config = MysqlS3Backup::Config.from_yaml_file(File.dirname(__FILE__) + "/../config/s3_mysql.yml")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment