Class: BackupNexus::BackupService

Inherits:
Object
  • Object
show all
Defined in:
app/services/backup_nexus/backup_service.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ BackupService

Returns a new instance of BackupService.



16
17
18
19
20
21
# File 'app/services/backup_nexus/backup_service.rb', line 16

def initialize(config)
  @config = config
  @record = nil
  @errors = []
  @temp_files = []
end

Class Method Details

.run(config) ⇒ Object

Execute a backup for a given config



12
13
14
# File 'app/services/backup_nexus/backup_service.rb', line 12

def self.run(config)
  new(config).run
end

Instance Method Details

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/services/backup_nexus/backup_service.rb', line 23

def run
  @record = BackupNexus::Backup.start!(
    config_name: @config.name,
    triggered_by: "service"
  )

  begin
    @stage = "preparing storage"
    validate_identifier!(@config.name, "backup name")
    FileUtils.mkdir_p(validated_path(@config.storage_path_expanded, "storage path"))

    # Step 1: Dump database
    @stage = "database dump"
    dump_path = dump_database
    raise "Database dump failed" unless dump_path

    # Step 2: Create archive if enabled (tar additional files/dirs)
    @stage = "archive creation"
    if @config.archive_enabled? && @config.archive_paths_list.any?
      dump_path = create_archive(dump_path)
    end

    # Step 3: Split into chunks if enabled
    @stage = "file splitting"
    if @config.split_chunks? && File.size(dump_path) > 50_000_000 # 50MB
      dump_path = split_file(dump_path)
    end

    # Step 4: Compress
    @stage = "compression"
    if @config.compress? && !@config.split_chunks?
      dump_path = compress(dump_path)
    elsif @config.bzip2_compress? && !@config.split_chunks?
      dump_path = compress_bzip2(dump_path)
    end

    # Step 5: Encrypt
    @stage = "encryption"
    if @config.encrypted?
      dump_path = encrypt_openssl(dump_path)
    elsif @config.gpg_enabled?
      dump_path = encrypt_gpg(dump_path)
    end

    # Step 6: Sync to S3
    @stage = "S3 sync"
    sync_s3(dump_path) if @config.s3_enabled?

    # Step 7: Sync to remote
    @stage = "remote sync"
    sync_remote(dump_path) if @config.rsync_enabled?

    # Step 8: Cleanup old backups
    @stage = "retention cleanup"
    cleanup_old_backups

    # Step 9: Record success
    @stage = "recording success"
    file_size = calculate_size(dump_path)
    @record.succeed!(file_path: dump_path, file_size: file_size)

    # Step 10: Notify
    @stage = "success notification"
    notify_success

    { success: true, record: @record, file_path: dump_path }
  rescue StandardError => e
    full_error = failure_message(e)
    @record&.fail!(error_message: full_error)
    notify_failure(full_error)
    { success: false, error: full_error, record: @record }
  ensure
    cleanup_temp_files
  end
end