summaryrefslogtreecommitdiff
path: root/bin/byteback-restore
blob: 68d33092d43acb83029c83ff5b46d0b19462004d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
89
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
#!/usr/bin/ruby
# enocoding: UTF-8
#
# Restore a file from the most recent backup, from the remote host.
#

$LOAD_PATH.unshift('/usr/lib/byteback')
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '../lib')) # For development

require 'trollop'
require 'byteback/util'
require 'byteback/log'
require 'byteback/restore'
include Byteback::Util
include Byteback::Log

#
#  Run a remote command.
#
def ssh(*ssh_args)
  args = ['ssh',
          '-o', 'BatchMode=yes',
          '-o', 'ConnectionAttempts=5',
          '-o', 'ConnectTimeout=30',
          '-o', 'ServerAliveInterval=60',
          '-o', 'TCPKeepAlive=yes',
          '-x', '-a',
          '-i', @ssh_key,
          '-l', @destination_user,
          @destination_host
         ] +
         ssh_args.map { |a| a ? a : '' }

  puts args.join(" " ) if @verbose
  system(*args)
end

def list_files(snapshot, list_all, pattern)
  args = ['byteback-receive', '--snapshot', snapshot, '--list'] 
  args << "--list-all" if list_all
  args << @verbose if @verbose
  args += Byteback::Restore.encode_args(pattern)
  ssh(*args)
end

#
#  We cannot use plain 'rsync' here because the receiving-command will
# see that, and rewrite our arguments.
#
#  To cater to this we have to wrap the rsync for the restore and we
# do that by setting "rsync-path" to point to a faux script.
#
#
def restore_files(paths, snapshot)
  #
  # Basic args
  #
  args = %w(rsync --archive --numeric-ids --inplace --relative --compress)

  #
  #  Add on the I/O-timeout
  #
  args += ['--timeout', @io_timeout.to_s ] unless ( @io_timeout.nil? )
  args += ['--rsh', "ssh -o BatchMode=yes -x -a -i #{@ssh_key} -l #{@destination_user}"]
  args << '--verbose' if @verbose
  args += ['--rsync-path', "byteback-restore --fake-super --snapshot #{snapshot}"]

  #
  # To add extra rsync flags, a file can be used.  This can have flags all on one line, or one per line.
  #
  if File.exists?("/etc/byteback/rsync_flags")
    args += File.readlines("/etc/byteback/rsync_flags").map(&:chomp)
  end

  dst = "#{@destination_user}@#{@destination_host}:"

  paths.each do |path|
    path = Byteback::Restore.encode_args(path).first
    args << File.join(dst,path)
    dst = ":"
  end

  args << "."
  puts args.join(" " ) if @verbose
  system(*args)
end

##
##  Entry-point to our code.
##
if __FILE__ == $PROGRAM_NAME

  ME = File.basename($PROGRAM_NAME)

  #
  #  Parse our command-line arguments
  #
  opts = Trollop.options do
    banner "#{ME}: Restore a file to this system from a byteback-enabled server\n "

    opt :restore, "Restore the files"

    opt :snapshot, "The snaphot to use.",
        :type => :string, :default => "*"

    opt :destination, 'Backup destination (i.e. user@host:/path).',
        :type => :string

    opt :verbose, 'Show debugging messages'

    opt :io_timeout, 'Number of seconds to allow I/O timeout for',
          :type => :integer,
          :default => 10800

    opt :ssh_key, 'SSH key filename',
        :type => :string,
        :default => '/etc/byteback/key',
        :short => 'k'

    opt :list_all, 'List all versrions of each file'
    
  end

  @verbose = opts[:verbose] ? '--verbose' : nil
  @io_timeout = opts[:io_timeout] if opts[:io_timeout]

  # Read the default destination
  if File.exist?('/etc/byteback/destination')
    @destination = File.read('/etc/byteback/destination').chomp
  end

  # Set the default SSH key
  if File.exist?('/etc/byteback/key')
    @ssh_key = '/etc/byteback/key'
  end

  #
  # Allow the command-line to override them.
  #
  @ssh_key = opts[:ssh_key] unless  opts[:ssh_key].nil?
  @destination = opts[:destination] unless  opts[:destination].nil?
  
  #
  # Check our destination
  #
  fatal('Must suply --destination or put it into /etc/bytebackup/destination') unless @destination

  #
  # Check our destination is well-formed
  #
  if @destination =~ /^(?:(.+)@)?([^@:]+):(.+)?$/
    @destination_user, @destination_host, @destination_path = [Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)]
  else
    fatal('Destination must be a remote path, e.g. ssh@host.com:/store/backups')
  end

  #
  # Test that we have an SSH-key which we can read.
  #
  fatal("Could not read ssh key #{@ssh_key}") unless File.readable?(@ssh_key)

  #
  #  If the user didn't specify a file then we're not restoring anything,
  # and we should abort.
  #
  if ARGV.empty?
    fatal('You must specify a file to search/restore')
  end

  if opts[:restore]
    #
    #  Restore a file
    #
    restore_files(ARGV.collect{|a| File.expand_path(a)}, opts[:snapshot])
    exit(0)
  end

  list_files(opts[:snapshot], opts[:list_all], ARGV.collect{|a| File.expand_path(a)})
  exit(0)
end