berty 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/berty +28 -0
- data/lib/berty/backup.rb +12 -0
- data/lib/berty/server.rb +94 -0
- data/lib/berty.rb +31 -0
- metadata +65 -0
data/bin/berty
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
3
|
+
require 'berty'
|
4
|
+
|
5
|
+
servers = Berty.servers
|
6
|
+
|
7
|
+
for server in servers
|
8
|
+
case ARGV.first
|
9
|
+
when 'setup'
|
10
|
+
Berty.logger.info "Started server setup..."
|
11
|
+
server.setup!
|
12
|
+
Berty.logger.info "Finished server setup..."
|
13
|
+
puts "Server '#{server.hostname}' setup..."
|
14
|
+
when 'info'
|
15
|
+
puts " * #{server.username}@#{server.hostname}"
|
16
|
+
for directory in server.directories
|
17
|
+
puts " #{directory[:path]} -> #{directory[:destination]}"
|
18
|
+
end
|
19
|
+
if server.mysql_backup?
|
20
|
+
puts " MySQL backup using password: #{server.mysql_password.gsub(/[a-zA-Z]/, '*')}"
|
21
|
+
end
|
22
|
+
puts unless server == servers.last
|
23
|
+
else
|
24
|
+
Berty.logger.info "Started backup..."
|
25
|
+
server.backup!
|
26
|
+
Berty.logger.info "Finished backup..."
|
27
|
+
end
|
28
|
+
end
|
data/lib/berty/backup.rb
ADDED
data/lib/berty/server.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
module Berty
|
2
|
+
class Server
|
3
|
+
|
4
|
+
attr_reader :hostname
|
5
|
+
|
6
|
+
def initialize(hostname, options = {})
|
7
|
+
@hostname = hostname
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
## Return all directories which need to be backed up
|
12
|
+
def directories
|
13
|
+
(@options['directories'] || []).map do |dir|
|
14
|
+
path, label = dir.split(':')
|
15
|
+
label ||= path.split('/').last
|
16
|
+
{:path=> path, :label => label, :destination => File.join(backup_path, label)}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
## Which username should be used when connecting to this server?
|
21
|
+
def username
|
22
|
+
@options['username']
|
23
|
+
end
|
24
|
+
|
25
|
+
## Return the full path where backups for this server should be stored on the local file system.
|
26
|
+
def backup_path
|
27
|
+
File.join(Berty.config['root'], hostname)
|
28
|
+
end
|
29
|
+
|
30
|
+
## Return the full path to where mysql backups are stored
|
31
|
+
def mysql_backup_path
|
32
|
+
File.join(backup_path, 'mysql')
|
33
|
+
end
|
34
|
+
|
35
|
+
## Execute a backup of this server (backing up all directories to the appropriate locations)
|
36
|
+
def backup!
|
37
|
+
run "mkdir -p #{backup_path}"
|
38
|
+
for dir in directories
|
39
|
+
destination =
|
40
|
+
Berty.logger.info " starting backup of: '#{dir[:path]}' to '#{dir[:destination]}'"
|
41
|
+
run "rsync -a #{username}@#{hostname}:#{dir[:path]}/ #{dir[:destination]}"
|
42
|
+
Berty.logger.info " finished backup of: '#{dir[:path]}' to '#{dir[:destination]}'"
|
43
|
+
end
|
44
|
+
backup_mysql!
|
45
|
+
end
|
46
|
+
|
47
|
+
## Setup the connection to this server by sshing to it and doing nothing. This gives the option to accept
|
48
|
+
## any host keys.
|
49
|
+
def setup!
|
50
|
+
key = File.read("#{ENV['HOME']}/.ssh/id_rsa.pub")
|
51
|
+
ssh "echo '#{key.chomp}' >> ~/.ssh/authorized_keys"
|
52
|
+
end
|
53
|
+
|
54
|
+
## Should this server backup a mysql database?
|
55
|
+
def mysql_backup?
|
56
|
+
@options['mysql_password'].is_a?(String)
|
57
|
+
end
|
58
|
+
|
59
|
+
## What password should be used for accessing the database?
|
60
|
+
def mysql_password
|
61
|
+
@options['mysql_password']
|
62
|
+
end
|
63
|
+
|
64
|
+
## Execute mysql backups?
|
65
|
+
def backup_mysql!
|
66
|
+
return unless mysql_backup?
|
67
|
+
run "mkdir -p #{mysql_backup_path}"
|
68
|
+
Berty.logger.info " starting mysql backup on: #{hostname}"
|
69
|
+
## create a dump on the remote system as '/tmp/bert-mysqldump'
|
70
|
+
ssh "mysqldump --all-databases -u root -p#{mysql_password} > /tmp/berty-mysqldump"
|
71
|
+
## pull the file over to us
|
72
|
+
scp "/tmp/berty-mysqldump", File.join(mysql_backup_path, "#{Time.now.strftime("%Y-%m-%d-%H-%M")}.sql")
|
73
|
+
Berty.logger.info " finishing mysql backup on: #{hostname}"
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def run(command)
|
79
|
+
Berty.logger.info "executing: #{command}"
|
80
|
+
system(command)
|
81
|
+
end
|
82
|
+
|
83
|
+
def ssh(command)
|
84
|
+
run "ssh #{username}@#{hostname} \"#{command}\""
|
85
|
+
end
|
86
|
+
|
87
|
+
def scp(remote, local)
|
88
|
+
run "scp #{username}@#{hostname}:#{remote} #{local}"
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
data/lib/berty.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
require 'berty/backup'
|
5
|
+
require 'berty/server'
|
6
|
+
|
7
|
+
module Berty
|
8
|
+
|
9
|
+
class Error < StandardError; end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def config_path
|
13
|
+
[File.expand_path('../../config/berty.example.conf', __FILE__), '/etc/berty.conf'].each do |file|
|
14
|
+
return file if File.exist?(file)
|
15
|
+
end
|
16
|
+
raise Error, "Configuration file not found. Please ensure it is defined in '/etc/berty.conf'"
|
17
|
+
end
|
18
|
+
|
19
|
+
def config
|
20
|
+
@config ||= YAML.load_file(config_path)
|
21
|
+
end
|
22
|
+
|
23
|
+
def logger
|
24
|
+
@logger ||= Logger.new(config['log_file'] || STDERR)
|
25
|
+
end
|
26
|
+
|
27
|
+
def servers
|
28
|
+
@servers ||= config['servers'].map{|key,value| Berty::Server.new(key, value) }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: berty
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Adam Cooke
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-03-23 00:00:00 +00:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description:
|
22
|
+
email: adam@atechmedia.com
|
23
|
+
executables:
|
24
|
+
- berty
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- bin/berty
|
31
|
+
- lib/berty/backup.rb
|
32
|
+
- lib/berty/server.rb
|
33
|
+
- lib/berty.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://www.atechmedia.com
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
version: "0"
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.3.6
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Berty the backup gem
|
64
|
+
test_files: []
|
65
|
+
|