Class: Bup::Tar

Inherits:
Object
  • Object
show all
Defined in:
lib/bup/tar.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Tar

Returns a new instance of Tar.



9
10
11
# File 'lib/bup/tar.rb', line 9

def initialize(config)
  @config = config
end

Instance Method Details

#build_exclude_file(patterns) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/bup/tar.rb', line 23

def build_exclude_file(patterns)
  tf = Tempfile.new("bup")
  patterns.each do |p|
    p = expand_string(p)
    tf.write(p)
    tf.write("\n")
  end
  tf.close
  tf
end

#call(profile_name = nil) ⇒ Object

Run tar.



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
# File 'lib/bup/tar.rb', line 90

def call(profile_name = nil)
  profile_name ||= @config.runtime["profile"]
  profile = @config.config["profiles"][profile_name]

  raise "Missing profile #{profile_name}." unless profile

  args = profile["tarcmd"].dup || ["tar", "cJvf", "${BUP_FILENAME}.tar.xz"]

  prepare_destination(profile_name, profile)

  if @config.runtime["type"] == "incremental"
    lastrun = @config.lastrun(profile_name)
    args += ["--newer", lastrun.strftime("%Y-%m-%d %H:%M:%S %z")] if lastrun
  end

  run_pre_cmds profile

  @config.update_lastrun(profile_name)

  tf = build_exclude_file(profile["exclude"] || [])

  # Build and run tar command.
  begin
    args += ["--exclude-from", tf.path]
    args += (profile["include"] || ["."])
    args = args.map do |str|
      expand_string(str)
    end

    # Run tar.
    run_cmd(*args)
  ensure
    tf.unlink
  end

  run_post_cmds profile
end

#clear_history(profile_name, destination) ⇒ Object



61
62
63
64
65
# File 'lib/bup/tar.rb', line 61

def clear_history(profile_name, destination)
  Dir["#{destination}/#{profile_name}*"].each do |backup|
    File.delete(backup)
  end
end

#expand_string(str) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/bup/tar.rb', line 13

def expand_string(str)
  while str =~ /\${?([a-zA-Z0-9_]+)}?/
    all = $~[0]
    nm = $~[1]
    str = str.sub(all, ENV[nm] || "")
  end

  str
end

#prepare_destination(profile_name, profile) ⇒ Object

Prepare the destination directory.



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
# File 'lib/bup/tar.rb', line 35

def prepare_destination(profile_name, profile)
  destination = expand_string(profile["destination"] || ".")

  type = @config.runtime["type"] || "full"

  FileUtils.mkdir_p(destination) unless File.exist?(destination)

  filename_base = "#{profile_name}-#{type}"
  filename = File.join(destination,
                       "#{filename_base}-#{DateTime.now.new_offset(0).strftime("%Y%m%d%H%M%S")}")

  history = @config.profile(profile_name)["history"] || 0

  if type == "full" 
    if history.positive?
      prune_history(profile_name, destination, filename_base, history)
    elsif history == 0
      clear_history(profile_name, destination)
    end
  end

  ENV["BUP_DESTINATION"] = destination
  ENV["BUP_FILENAME"] = filename
  filename
end

#prune_history(profile_name, destination, filename_base, history) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bup/tar.rb', line 67

def prune_history(profile_name, destination, filename_base, history)
  oldest_kept_file = nil

  # Keep some full backups and remove the others.
  # We capture the oldest full backup and get rid of preceeding incrementals.
  Dir["#{destination}/#{filename_base}*"].sort.reverse.each_with_index do |fullbackup, idx|
    if idx < history
      oldest_kept_file = fullbackup
    else
      File.delete(fullbackup)
    end
  end

  # Remove all incremental files that are older than the oldest kept full backup.
  if oldest_kept_file
    remove_before = File.stat(oldest_kept_file).ctime
    Dir["#{destination}/#{profile_name}*"].each do |backupfile|
      File.delete(backupfile) if File.stat(backupfile).ctime < remove_before
    end
  end
end

#run_cmd(*args) ⇒ Object

Exec and run a command in a standard way.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/bup/tar.rb', line 142

def run_cmd(*args)
  Open3.popen3(*args) do |stdin, stdout, stderr, wait_thr|
    stdin.close
    x = nil
    while wait_thr.status
      r, _w, _e = IO.select([stdout, stderr])
      r.each do |stream|
        print x if (x = stream.read_nonblock(1024, exception: false))
      end
    end

    wait_thr.join
    print x if (x = stdout.read_nonblock(1024, exception: false))

    print x if (x = stderr.read_nonblock(1024, exception: false))

    $stdout.flush
  end
end

#run_post_cmds(profile) ⇒ Object



135
136
137
138
139
# File 'lib/bup/tar.rb', line 135

def run_post_cmds(profile)
  (profile["post_cmds"] || []).each do |args|
    run_cmd(*args.map { |c| expand_string(c) })
  end
end

#run_pre_cmds(profile) ⇒ Object



128
129
130
131
132
133
# File 'lib/bup/tar.rb', line 128

def run_pre_cmds(profile)
  # Before we update the last_run, execute any pre_cmds.
  (profile["pre_cmds"] || []).each do |args|
    run_cmd(*args.map { |c| expand_string(c) })
  end
end