Class: Pvectl::Models::Backup

Inherits:
Base
  • Object
show all
Defined in:
lib/pvectl/models/backup.rb

Overview

Represents a VM/container backup in Proxmox.

A backup is a full copy of a VM or container stored on a storage backend. Backups can be created via vzdump and restored to create new VMs/containers.

Examples:

Creating a backup model

backup = Backup.new(
  volid: "local:backup/vzdump-qemu-100-2024_01_15-10_30_00.vma.zst",
  vmid: 100,
  size: 1610612736,
  ctime: 1705315800,
  format: "vma"
)
backup.human_size   # => "1.5 GiB"
backup.vm?          # => true

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Backup

Creates a new Backup instance.

Parameters:

  • attrs (Hash) (defaults to: {})

    backup attributes

Options Hash (attrs):

  • :volid (String)

    volume identifier

  • :vmid (Integer)

    VM/container ID

  • :node (String)

    node name

  • :storage (String)

    storage name (auto-detected from volid if not provided)

  • :resource_type (Symbol)

    :qemu or :lxc (auto-detected from volid if not provided)

  • :format (String)

    backup format

  • :size (Integer)

    size in bytes

  • :ctime (Integer)

    Unix timestamp of creation

  • :notes (String)

    description

  • :protected (Boolean)

    protection status



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pvectl/models/backup.rb', line 67

def initialize(attrs = {})
  super
  @volid = attributes[:volid]
  @vmid = attributes[:vmid]
  @node = attributes[:node]
  @storage = attributes[:storage] || extract_storage
  @resource_type = attributes[:resource_type] || detect_resource_type
  @format = attributes[:format]
  @size = attributes[:size]
  @ctime = attributes[:ctime]
  @notes = attributes[:notes]
  @protected = attributes[:protected] || false
end

Instance Attribute Details

#ctimeInteger? (readonly)

Returns Unix timestamp when backup was created.

Returns:

  • (Integer, nil)

    Unix timestamp when backup was created



46
47
48
# File 'lib/pvectl/models/backup.rb', line 46

def ctime
  @ctime
end

#formatString? (readonly)

Returns backup format (“vma”, “tar”).

Returns:

  • (String, nil)

    backup format (“vma”, “tar”)



40
41
42
# File 'lib/pvectl/models/backup.rb', line 40

def format
  @format
end

#nodeString? (readonly)

Returns node name where backup is stored.

Returns:

  • (String, nil)

    node name where backup is stored



31
32
33
# File 'lib/pvectl/models/backup.rb', line 31

def node
  @node
end

#notesString? (readonly)

Returns optional notes/description.

Returns:

  • (String, nil)

    optional notes/description



49
50
51
# File 'lib/pvectl/models/backup.rb', line 49

def notes
  @notes
end

#protectedBoolean (readonly)

Returns whether backup is protected from deletion.

Returns:

  • (Boolean)

    whether backup is protected from deletion



52
53
54
# File 'lib/pvectl/models/backup.rb', line 52

def protected
  @protected
end

#resource_typeSymbol? (readonly)

Returns resource type (:qemu or :lxc).

Returns:

  • (Symbol, nil)

    resource type (:qemu or :lxc)



37
38
39
# File 'lib/pvectl/models/backup.rb', line 37

def resource_type
  @resource_type
end

#sizeInteger? (readonly)

Returns backup size in bytes.

Returns:

  • (Integer, nil)

    backup size in bytes



43
44
45
# File 'lib/pvectl/models/backup.rb', line 43

def size
  @size
end

#storageString? (readonly)

Returns storage name (extracted from volid).

Returns:

  • (String, nil)

    storage name (extracted from volid)



34
35
36
# File 'lib/pvectl/models/backup.rb', line 34

def storage
  @storage
end

#vmidInteger (readonly)

Returns VM/container ID.

Returns:

  • (Integer)

    VM/container ID



28
29
30
# File 'lib/pvectl/models/backup.rb', line 28

def vmid
  @vmid
end

#volidString (readonly)

Returns full volume identifier (e.g., “local:backup/vzdump-qemu-100-xxx.vma.zst”).

Returns:

  • (String)

    full volume identifier (e.g., “local:backup/vzdump-qemu-100-xxx.vma.zst”)



25
26
27
# File 'lib/pvectl/models/backup.rb', line 25

def volid
  @volid
end

Instance Method Details

#container?Boolean

Checks if the backup is for a container (LXC).

Returns:

  • (Boolean)

    true if resource_type is :lxc



100
101
102
# File 'lib/pvectl/models/backup.rb', line 100

def container?
  resource_type == :lxc
end

#created_atTime?

Returns the backup creation time as a Time object.

Returns:

  • (Time, nil)

    creation time or nil if ctime is not set



84
85
86
87
88
# File 'lib/pvectl/models/backup.rb', line 84

def created_at
  return nil if ctime.nil?

  Time.at(ctime)
end

#filenameString?

Extracts the filename from the volume identifier.

Returns:

  • (String, nil)

    filename or nil if volid is not set



107
108
109
110
111
# File 'lib/pvectl/models/backup.rb', line 107

def filename
  return nil if volid.nil?

  volid.split("/").last
end

#human_sizeString?

Returns human-readable size.

Returns:

  • (String, nil)

    formatted size (e.g., “1.5 GiB”) or nil if size is not set



116
117
118
119
120
# File 'lib/pvectl/models/backup.rb', line 116

def human_size
  return nil if size.nil?

  format_bytes(size)
end

#protected?Boolean

Checks if the backup is protected from deletion.

Returns:

  • (Boolean)

    true if protected



125
126
127
# File 'lib/pvectl/models/backup.rb', line 125

def protected?
  @protected == true
end

#vm?Boolean

Checks if the backup is for a VM (QEMU).

Returns:

  • (Boolean)

    true if resource_type is :qemu



93
94
95
# File 'lib/pvectl/models/backup.rb', line 93

def vm?
  resource_type == :qemu
end