Class: VpsAdmin::CLI::Commands::BackupDataset

Inherits:
BaseDownload
  • Object
show all
Defined in:
lib/vpsadmin/cli/commands/backup_dataset.rb

Direct Known Subclasses

BackupVps

Defined Under Namespace

Classes: LocalSnapshot

Instance Method Summary collapse

Methods inherited from BaseDownload

#initialize

Constructor Details

This class inherits a constructor from VpsAdmin::CLI::Commands::BaseDownload

Instance Method Details

#exec(args) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/vpsadmin/cli/commands/backup_dataset.rb', line 90

def exec(args)
  if args.size == 1 && /^\d+$/ !~ args[0]
    fs = args[0]

    ds_id = read_dataset_id(fs)

    ds = if ds_id
           @api.dataset.show(ds_id)
         else
           dataset_chooser
         end

  elsif args.size != 2
    warn 'Provide DATASET_ID and FILESYSTEM arguments'
    exit(false)

  else
    ds = @api.dataset.show(args[0].to_i)
    fs = args[1]
  end

  check_dataset_id!(ds, fs)
  snapshots = ds.snapshot.list

  local_state = parse_tree(fs)

  # - Find out current history ID
  # - If there are snapshots with this ID that are not present locally,
  #   download them
  #   - If the dataset for this history ID does not exist, create it
  #   - If it exists, check what snapshots are there and make an incremental
  #     download

  remote_state = {}

  snapshots.each do |s|
    remote_state[s.history_id] ||= []
    remote_state[s.history_id] << s
  end

  if remote_state[ds.current_history_id].nil? \
     || remote_state[ds.current_history_id].empty?
    exit_msg(
      "Nothing to transfer: no snapshots with history id #{ds.current_history_id}",
      error: @opts[:no_snapshots_error]
    )
  end

  for_transfer = []

  latest_local_snapshot = local_state[ds.current_history_id] \
                          && local_state[ds.current_history_id].last
  found_latest = false

  # This is the first run within this history id, no local snapshots are
  # present
  if !latest_local_snapshot && @opts[:init_snapshots]
    remote_state[ds.current_history_id] =
      remote_state[ds.current_history_id].last(@opts[:init_snapshots])
  end

  remote_state[ds.current_history_id].each do |snap|
    found = false

    local_state.each_value do |local_snapshots|
      found = local_snapshots.detect { |s| s.name == snap.name }
      break if found
    end

    if !found_latest && latest_local_snapshot \
       && latest_local_snapshot.name == snap.name
      found_latest = true

    elsif latest_local_snapshot
      next unless found_latest
    end

    for_transfer << snap unless found
  end

  if for_transfer.empty?
    if found_latest
      exit_msg(
        'Nothing to transfer: all snapshots with history id ' \
        "#{ds.current_history_id} are already present locally",
        error: @opts[:no_snapshots_error]
      )

    else
      exit_msg(<<~END
        Unable to transfer: the common snapshot has not been found

        This can happen when the latest local snapshot was deleted from the server,
        i.e. you have not backed up this dataset for quite some time.

        You can either rename or destroy the whole current history id:

          zfs rename #{fs}/#{ds.current_history_id} #{fs}/#{ds.current_history_id}.old

        or

          zfs list -r -t all #{fs}/#{ds.current_history_id}
          zfs destroy -r #{fs}/#{ds.current_history_id}

        which will destroy all snapshots with this history id.

        You can also destroy the local backup completely or backup to another dataset
        and start anew.
      END
              )
    end
  end

  unless @opts[:quiet]
    puts "Will download #{for_transfer.size} snapshots:"
    for_transfer.each { |s| puts "  @#{s.name}" }
    puts
  end

  if @opts[:pretend]
    pretend_state = local_state.clone
    pretend_state[ds.current_history_id] ||= []
    pretend_state[ds.current_history_id].concat(for_transfer.map do |s|
      LocalSnapshot.new(s.name, ds.current_history_id, Time.iso8601(s.created_at).to_i)
    end)

    rotate(fs, pretend: pretend_state) if @opts[:rotate]

  else
    # Find the common snapshot between server and localhost, so that the transfer
    # can be incremental.
    shared_name = local_state[ds.current_history_id] \
                  && !local_state[ds.current_history_id].empty? \
                  && local_state[ds.current_history_id].last.name
    shared = nil

    if shared_name
      shared = remote_state[ds.current_history_id].detect { |s| s.name == shared_name }

      for_transfer.insert(0, shared) if shared && !for_transfer.detect { |s| s.id == shared.id }
    end

    write_dataset_id!(ds, fs) unless written_dataset_id?
    transfer(local_state, for_transfer, ds.current_history_id, fs)
    rotate(fs) if @opts[:rotate]
  end
end

#options(opts) ⇒ Object



15
16
17
18
19
20
21
22
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
# File 'lib/vpsadmin/cli/commands/backup_dataset.rb', line 15

def options(opts)
  @opts = {
    rotate: true,
    min_snapshots: 30,
    max_snapshots: 45,
    max_age: 30,
    attempts: 10,
    checksum: true,
    delete_after: true,
    sudo: true
  }

  opts.on('-p', '--pretend', 'Print what would the program do') do
    @opts[:pretend] = true
  end

  opts.on('-r', '--[no-]rotate', 'Delete old snapshots (enabled)') do |r|
    @opts[:rotate] = r
  end

  opts.on('-m', '--min-snapshots N', Integer, 'Keep at least N snapshots (30)') do |m|
    exit_msg('--min-snapshots must be greater than zero') if m <= 0
    @opts[:min_snapshots] = m
  end

  opts.on('-M', '--max-snapshots N', Integer, 'Keep at most N snapshots (45)') do |m|
    exit_msg('--max-snapshots must be greater than zero') if m <= 0
    @opts[:max_snapshots] = m
  end

  opts.on('-a', '--max-age N', Integer, 'Delete snapshots older then N days (30)') do |m|
    exit_msg('--max-age must be greater than zero') if m <= 0
    @opts[:max_age] = m
  end

  opts.on('-x', '--max-rate N', Integer, 'Maximum download speed in kB/s') do |r|
    exit_msg('--max-rate must be greater than zero') if r <= 0
    @opts[:max_rate] = r
  end

  opts.on('-q', '--quiet', 'Print only errors') do |q|
    @opts[:quiet] = q
  end

  opts.on('-s', '--safe-download', 'Download to a temp file (needs 2x disk space)') do |s|
    @opts[:safe] = s
  end

  opts.on('--retry-attemps N', Integer, 'Retry N times to recover from download error (10)') do |n|
    exit_msg('--retry-attempts must be greater than zero') if n <= 0
    @opts[:attempts] = n
  end

  opts.on('-i', '--init-snapshots N', Integer, 'Download max N snapshots initially') do |s|
    exit_msg('--init-snapshots must be greater than zero') if s <= 0
    @opts[:init_snapshots] = s
  end

  opts.on('--[no-]checksum', 'Verify checksum of the downloaded data (enabled)') do |c|
    @opts[:checksum] = c
  end

  opts.on('-d', '--[no-]delete-after', 'Delete the file from the server after successful download (enabled)') do |d|
    @opts[:delete_after] = d
  end

  opts.on('--no-snapshots-as-error', 'Consider no snapshots to download as an error') do
    @opts[:no_snapshots_error] = true
  end

  opts.on('--[no-]sudo', 'Use sudo to run zfs if not run as root (enabled)') do |s|
    @opts[:sudo] = s
  end
end