summaryrefslogtreecommitdiff
path: root/byteback-setup-client
blob: afcbee4aa8cf9eb323a78e0acec48fa340290ab2 (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
#!/usr/bin/ruby
#
# Run on a client machine to set up backups for the first time

$: << '/usr/lib/byteback'
require 'fileutils'
require 'trollop'
require 'byteback/util'
require 'byteback/log'
include Byteback::Util
include Byteback::Log

def error(message)
	STDERR.print "*** #{message}\n"
	exit 1
end

def verbose(message)
	print "#{message}\n"
end

opts = Trollop::options do

	opt :hostname, "Set host name for backups",
	  :type => :string

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

end

@destination = opts[:destination]
@hostname = opts[:hostname]

_dummy, @destination_user, @destination_host, colon, @destination_path = 
  /^(.*)?(?:@)([^:]+)(:)(.*)?$/.match(@destination).to_a

error("Must be a remote path") unless colon
if !@hostname
	@hostname = `hostname -f`.chomp
	print "No hostname set, using #{@hostname}\n"
end

FileUtils.mkdir_p("/etc/byteback")

if File.readable?("/etc/byteback/key")
	warn "Skipping key generation, delete /etc/byteback/key if that's wrong"
else

	error "Couldn't generate SSH key" unless 
		system <<-KEYGEN
			ssh-keygen -q -t rsa -C "byteback client key" \
			-N "" -f /etc/byteback/key
		KEYGEN

end

key_pub = File.read("/etc/byteback/key.pub").chomp

error "Remote setup didn't work" unless
  system("ssh -i /etc/byteback/key -l #{@destination_user} #{@destination_host} byteback-setup-client-receive #{@hostname} #{key_pub}")

File.open("/etc/byteback/host", "w") { |f| f.print @destination }

print "Setup worked!  To take your first backup run: byteback-backup --verbose\n"