Class: Aidp::Setup::Devcontainer::BackupManager
- Inherits:
-
Object
- Object
- Aidp::Setup::Devcontainer::BackupManager
- Defined in:
- lib/aidp/setup/devcontainer/backup_manager.rb
Overview
Manages backups of devcontainer.json before modifications
Defined Under Namespace
Classes: BackupError
Instance Method Summary collapse
-
#cleanup_old_backups(keep_count = 10) ⇒ Integer
Delete old backups, keeping only the N most recent.
-
#create_backup(source_path, metadata = {}) ⇒ String
Create a backup of the devcontainer file.
-
#initialize(project_dir, clock: Time) ⇒ BackupManager
constructor
A new instance of BackupManager.
-
#latest_backup ⇒ Hash?
Get the most recent backup.
-
#list_backups ⇒ Array<Hash>
List all available backups.
-
#restore_backup(backup_path, target_path, create_backup: true) ⇒ Boolean
Restore a backup to the specified location.
-
#total_backup_size ⇒ Integer
Calculate total size of all backups.
Constructor Details
#initialize(project_dir, clock: Time) ⇒ BackupManager
Returns a new instance of BackupManager.
13 14 15 16 17 |
# File 'lib/aidp/setup/devcontainer/backup_manager.rb', line 13 def initialize(project_dir, clock: Time) @project_dir = project_dir @backup_dir = File.join(project_dir, ".aidp", "backups", "devcontainer") @clock = clock end |
Instance Method Details
#cleanup_old_backups(keep_count = 10) ⇒ Integer
Delete old backups, keeping only the N most recent
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/aidp/setup/devcontainer/backup_manager.rb', line 96 def cleanup_old_backups(keep_count = 10) backups = list_backups return 0 if backups.size <= keep_count to_delete = backups[keep_count..] deleted_count = 0 to_delete.each do |backup| File.delete(backup[:path]) if File.exist?(backup[:path]) = "#{backup[:path]}.meta" File.delete() if File.exist?() deleted_count += 1 end Aidp.log_info("backup_manager", "cleaned up old backups", deleted: deleted_count, kept: keep_count) deleted_count end |
#create_backup(source_path, metadata = {}) ⇒ String
Create a backup of the devcontainer file
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 |
# File 'lib/aidp/setup/devcontainer/backup_manager.rb', line 23 def create_backup(source_path, = {}) unless File.exist?(source_path) raise BackupError, "Source file does not exist: #{source_path}" end ensure_backup_directory_exists = current_time.utc.strftime("%Y%m%d_%H%M%S") backup_filename = "devcontainer-#{timestamp}.json" backup_path = File.join(@backup_dir, backup_filename) FileUtils.cp(source_path, backup_path) # Create metadata file if .any? = "#{backup_path}.meta" File.write(, JSON.pretty_generate()) end Aidp.log_info("backup_manager", "created backup", source: source_path, backup: backup_path) backup_path rescue => e raise BackupError, "Failed to create backup: #{e.message}" end |
#latest_backup ⇒ Hash?
Get the most recent backup
121 122 123 |
# File 'lib/aidp/setup/devcontainer/backup_manager.rb', line 121 def latest_backup list_backups.first end |
#list_backups ⇒ Array<Hash>
List all available backups
53 54 55 56 57 58 59 60 61 |
# File 'lib/aidp/setup/devcontainer/backup_manager.rb', line 53 def list_backups return [] unless File.directory?(@backup_dir) Dir.glob(File.join(@backup_dir, "devcontainer-*.json")) .reject { |f| f.end_with?(".meta") } .map { |path| backup_info(path) } .sort_by { |info| info[:timestamp] } .reverse end |
#restore_backup(backup_path, target_path, create_backup: true) ⇒ Boolean
Restore a backup to the specified location
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/aidp/setup/devcontainer/backup_manager.rb', line 68 def restore_backup(backup_path, target_path, create_backup: true) unless File.exist?(backup_path) raise BackupError, "Backup file does not exist: #{backup_path}" end # Backup current file before restoring if create_backup && File.exist?(target_path) create_backup(target_path, { reason: "pre_restore", restoring_from: backup_path }) end FileUtils.mkdir_p(File.dirname(target_path)) FileUtils.cp(backup_path, target_path) Aidp.log_info("backup_manager", "restored backup", backup: backup_path, target: target_path) true rescue => e raise BackupError, "Failed to restore backup: #{e.message}" end |
#total_backup_size ⇒ Integer
Calculate total size of all backups
127 128 129 130 131 132 |
# File 'lib/aidp/setup/devcontainer/backup_manager.rb', line 127 def total_backup_size return 0 unless File.directory?(@backup_dir) Dir.glob(File.join(@backup_dir, "devcontainer-*.{json,meta}")) .sum { |f| File.size(f) } end |