Class: S3Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/s3-backup/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket_name) ⇒ S3Backup

Initialize with the name of S3 bucket to push to



22
23
24
25
26
27
28
29
30
31
# File 'lib/s3-backup/base.rb', line 22

def initialize(bucket_name)
  @files = []
  @files_to_cleanup = []
  @tar_excludes = []
  @copies_to_keep = 5
  @bucket_name = bucket_name
  @backup_name = 'backup'
  
  @s3 =  RightAws::S3.new(AWSCredentials.access_key, AWSCredentials.secret_access_key, :logger => Logger.new(nil))
end

Instance Attribute Details

#backup_nameObject

Prefix for tarball, defaults to “backup”.



16
17
18
# File 'lib/s3-backup/base.rb', line 16

def backup_name
  @backup_name
end

#bucket_nameObject (readonly)

Name of bucket to push to, set on initialize.



19
20
21
# File 'lib/s3-backup/base.rb', line 19

def bucket_name
  @bucket_name
end

#copies_to_keepObject

Number of backups to keep on S3. Defaults to 5.



13
14
15
# File 'lib/s3-backup/base.rb', line 13

def copies_to_keep
  @copies_to_keep
end

#filesObject

Array of files or paths to back up



4
5
6
# File 'lib/s3-backup/base.rb', line 4

def files
  @files
end

#files_to_cleanupObject

Array of files that will be deleted post-backup, regardless of success



7
8
9
# File 'lib/s3-backup/base.rb', line 7

def files_to_cleanup
  @files_to_cleanup
end

#tar_excludesObject

Array of exclude patterns, these are passed to tar as --exclude flags



10
11
12
# File 'lib/s3-backup/base.rb', line 10

def tar_excludes
  @tar_excludes
end

Instance Method Details

#after_backup(&block) ⇒ Object

Called after backup runs. Useful for restarting services.



41
42
43
# File 'lib/s3-backup/base.rb', line 41

def after_backup &block
  @after_backup = block
end

#before_backup(&block) ⇒ Object

Called before backup runs. Useful for dumping a database, or creating files prior to tarball create. As you create tmpfiles, you can push onto files_to_cleanup to ensure post-backup cleanup.



36
37
38
# File 'lib/s3-backup/base.rb', line 36

def before_backup &block
  @before_backup = block
end

#runObject

Runs the backup: creates tarball, pushes to s3, and rotates old backups.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/s3-backup/base.rb', line 46

def run
  begin    
    @before_backup.call unless @before_backup.nil?
  
    create_tarball
    push_to_s3
    rotate_remote_backups
  
    @after_backup.call unless @after_backup.nil?
  ensure
    cleanup_files
  end
end