Class: S3Rotate::BackupUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_rotate/core/backup_uploader.rb

Overview

BackupUploader Class Handles backup uploads with the right format

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s3_client) ⇒ Object

Initialize a new BackupUploader instance.

Parameters:

  • s3_client

    S3Client instance



22
23
24
# File 'lib/s3_rotate/core/backup_uploader.rb', line 22

def initialize(s3_client)
  @s3_client = s3_client
end

Instance Attribute Details

#s3_clientObject

attributes



13
14
15
# File 'lib/s3_rotate/core/backup_uploader.rb', line 13

def s3_client
  @s3_client
end

Instance Method Details

#upload(backup_name, local_backups_path, date_regex = /\d{4}-\d{2}-\d{2}/, date_format = "%Y-%m-%d") ⇒ Object

Upload local backup files to AWS S3 Only uploads new backups Only uploads backups as daily backups: use ‘rotate` to generate the weekly & monthly files

Parameters:

  • backup_name

    String containing the name of the backup to upload

  • local_backups_path

    String containing the path to the directory containing the backups

  • date_regex (defaults to: /\d{4}-\d{2}-\d{2}/)

    Regex returning the date contained in the filename of each backup

  • date_format (defaults to: "%Y-%m-%d")

    Format to be used by DateTime.strptime to parse the extracted date

Returns:

  • nothing



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

def upload(backup_name, local_backups_path, date_regex=/\d{4}-\d{2}-\d{2}/, date_format="%Y-%m-%d")
  # get backup files
  local_backups = FileUtils::files_in_directory(local_backups_path).reverse

  # upload local backups until we find one backup already uploaded
  local_backups.each do |local_backup|
    # parse the date & extension
    backup_date      = FileUtils::date_from_filename(local_backup, date_regex, date_format)
    backup_extension = FileUtils::extension_from_filename(local_backup)

    # skip invalid files
    next if not backup_date

    # stop uploading once we reach a file already uploaded
    break if @s3_client.exists?(backup_name, backup_date, "daily", extension=backup_extension)

    # upload file
    @s3_client.upload(backup_name, backup_date, "daily", backup_extension, File.open("#{local_backups_path}/#{local_backup}"))
  end
end