ubsafe 0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +9 -0
- data/bin/ubsafe +2 -0
- data/bin/ubsafe_cmd_with_password.expect +11 -0
- data/bin/ubsafe_file_exists +9 -0
- data/bin/ubsafe_file_mtime +10 -0
- data/bin/ubsafe_scp_cmd.expect +12 -0
- data/bin/ubsafe_ssh_cmd.expect +11 -0
- data/bin/ubsafer +22 -0
- data/lib/ubsafe.rb +21 -0
- data/lib/ubsafe/extensions/ubsafe_extensions.rb +5 -0
- data/lib/ubsafe/extensions/ubsafe_file_extensions.rb +34 -0
- data/lib/ubsafe/extensions/ubsafe_hash_extensions.rb +15 -0
- data/lib/ubsafe/extensions/ubsafe_integer_extensions.rb +45 -0
- data/lib/ubsafe/extensions/ubsafe_kernel_extensions.rb +35 -0
- data/lib/ubsafe/extensions/ubsafe_logging_extensions.rb +136 -0
- data/lib/ubsafe/ubsafe_commands/ubsafe_command_backup.rb +647 -0
- data/lib/ubsafe/ubsafe_commands/ubsafe_command_backup_mysql.rb +54 -0
- data/lib/ubsafe/ubsafe_commands/ubsafe_command_backup_postgres.rb +54 -0
- data/lib/ubsafe/ubsafe_commands/ubsafe_command_backup_svn.rb +53 -0
- data/lib/ubsafe/ubsafe_commands/ubsafe_commands.rb +12 -0
- data/lib/ubsafe/ubsafe_config.rb +166 -0
- metadata +110 -0
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
module UBSafe
|
4
|
+
|
5
|
+
module Commands
|
6
|
+
|
7
|
+
class MySqlBackup < UBSafe::Commands::Backup
|
8
|
+
|
9
|
+
##
|
10
|
+
# Hook to allow customization before creating source backup
|
11
|
+
#
|
12
|
+
# @return [Symbol] :success or :failure
|
13
|
+
#
|
14
|
+
def before_source_backup
|
15
|
+
tmp_dir = File.expand_path(@backup_options[:temporary_directory])
|
16
|
+
mysql_tmp_dir = File.join(tmp_dir,'mysql')
|
17
|
+
FileUtils.mkdir_p(mysql_tmp_dir)
|
18
|
+
cmd = " mysqldump -u#{@backup_options[:mysql_username]} -p#{@backup_options[:mysql_password]} -h#{@backup_options[:mysql_host]} #{@backup_options[:mysql_database]} >#{mysql_tmp_dir}/#{@backup_options[:mysql_database]}.sql"
|
19
|
+
@log.info("Backup #{@backup_name} \"mysqldump -u#{@backup_options[:mysql_username]} -p[PASSWORD] -h#{@backup_options[:mysql_host]} #{@backup_options[:mysql_database]} >#{mysql_tmp_dir}/#{@backup_options[:mysql_database]}.sql\"")
|
20
|
+
cmd_output = `#{cmd}`
|
21
|
+
cmd_status = $?
|
22
|
+
cmd_status = cmd_status == 0 ? :success : :failure
|
23
|
+
if cmd_status == :failure
|
24
|
+
# cleanup
|
25
|
+
cmd_output = `rm -rf #{mysql_tmp_dir}`
|
26
|
+
@log.error("Backup #{@backup_name} before_source_backup failed during mysqldump. Output #{cmd_output}")
|
27
|
+
end
|
28
|
+
# Point source to directory with dump file in it so rest of the world works
|
29
|
+
@backup_options[:source_tree] = mysql_tmp_dir
|
30
|
+
return cmd_status
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# Hook to allow customization after cleaning source
|
35
|
+
#
|
36
|
+
# @return [Symbol] :success or :failure
|
37
|
+
#
|
38
|
+
def after_clean_source
|
39
|
+
tmp_dir = File.expand_path(@backup_options[:temporary_directory])
|
40
|
+
mysql_tmp_dir = File.join(tmp_dir,'mysql')
|
41
|
+
cmd_output = `rm -rf #{mysql_tmp_dir}`
|
42
|
+
cmd_status = $?
|
43
|
+
cmd_status == 0 ? :success : :failure
|
44
|
+
if cmd_status == :failure
|
45
|
+
@log.error("Backup #{@backup_name} after_clean_source failed. Output #{cmd_output}")
|
46
|
+
end
|
47
|
+
return cmd_status
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
module UBSafe
|
4
|
+
|
5
|
+
module Commands
|
6
|
+
|
7
|
+
class PostgresBackup < UBSafe::Commands::Backup
|
8
|
+
|
9
|
+
##
|
10
|
+
# Hook to allow customization before creating source backup
|
11
|
+
#
|
12
|
+
# @return [Symbol] :success or :failure
|
13
|
+
#
|
14
|
+
def before_source_backup
|
15
|
+
tmp_dir = File.expand_path(@backup_options[:temporary_directory])
|
16
|
+
postgres_tmp_dir = File.join(tmp_dir,'postgres')
|
17
|
+
FileUtils.mkdir_p(postgres_tmp_dir)
|
18
|
+
cmd = "#{@backup_options[:postgres_bin_dir]}/pg_dump -U#{@backup_options[:postgres_username]} -h#{@backup_options[:postgres_host]} -p#{@backup_options[:postgres_port]} #{@backup_options[:postgres_database]} >#{postgres_tmp_dir}/#{@backup_options[:postgres_database]}.sql"
|
19
|
+
@log.info("Backup #{@backup_name} \"pg_dump -U#{@backup_options[:postgres_username]} -h#{@backup_options[:postgres_host]} -p#{@backup_options[:postgres_port]} #{@backup_options[:postgres_database]} >#{postgres_tmp_dir}/#{@backup_options[:postgres_database]}.sql\"")
|
20
|
+
cmd_output = `#{cmd}`
|
21
|
+
cmd_status = $?
|
22
|
+
cmd_status = cmd_status == 0 ? :success : :failure
|
23
|
+
if cmd_status == :failure
|
24
|
+
# cleanup
|
25
|
+
cmd_output = `rm -rf #{postgres_tmp_dir}`
|
26
|
+
@log.error("Backup #{@backup_name} before_source_backup failed during pg_dump. Output #{cmd_output}")
|
27
|
+
end
|
28
|
+
# Point source to directory with dump file in it so rest of the world works
|
29
|
+
@backup_options[:source_tree] = postgres_tmp_dir
|
30
|
+
return cmd_status
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# Hook to allow customization after cleaning source
|
35
|
+
#
|
36
|
+
# @return [Symbol] :success or :failure
|
37
|
+
#
|
38
|
+
def after_clean_source
|
39
|
+
tmp_dir = File.expand_path(@backup_options[:temporary_directory])
|
40
|
+
postgres_tmp_dir = File.join(tmp_dir,'postgres')
|
41
|
+
cmd_output = `rm -rf #{postgres_tmp_dir}`
|
42
|
+
cmd_status = $?
|
43
|
+
cmd_status == 0 ? :success : :failure
|
44
|
+
if cmd_status == :failure
|
45
|
+
@log.error("Backup #{@backup_name} after_clean_source failed. Output #{cmd_output}")
|
46
|
+
end
|
47
|
+
return cmd_status
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
module UBSafe
|
4
|
+
|
5
|
+
module Commands
|
6
|
+
|
7
|
+
class SVNBackup < UBSafe::Commands::Backup
|
8
|
+
|
9
|
+
##
|
10
|
+
# Hook to allow customization before creating source backup
|
11
|
+
#
|
12
|
+
# @return [Symbol] :success or :failure
|
13
|
+
#
|
14
|
+
def before_source_backup
|
15
|
+
source_tree = File.expand_path(@backup_options[:source_tree])
|
16
|
+
tmp_dir = File.expand_path(@backup_options[:temporary_directory])
|
17
|
+
svn_tmp_dir = File.join(tmp_dir,'svn')
|
18
|
+
FileUtils.mkdir_p(svn_tmp_dir)
|
19
|
+
cmd = "svnadmin hotcopy #{source_tree} #{svn_tmp_dir}"
|
20
|
+
cmd_output = `#{cmd}`
|
21
|
+
cmd_status = $?
|
22
|
+
cmd_status = cmd_status == 0 ? :success : :failure
|
23
|
+
if cmd_status == :failure
|
24
|
+
# cleanup
|
25
|
+
cmd_output = `rm -rf #{svn_tmp_dir}`
|
26
|
+
@log.error("Backup #{@backup_name} before_source_backup failed during hotcopy. Output #{cmd_output}")
|
27
|
+
end
|
28
|
+
# Point source to hotcopy so rest of the world works
|
29
|
+
@backup_options[:source_tree] = svn_tmp_dir
|
30
|
+
return cmd_status
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# Hook to allow customization after cleaning source
|
35
|
+
#
|
36
|
+
# @return [Symbol] :success or :failure
|
37
|
+
#
|
38
|
+
def after_clean_source
|
39
|
+
tmp_dir = File.expand_path(@backup_options[:temporary_directory])
|
40
|
+
svn_tmp_dir = File.join(tmp_dir,'svn')
|
41
|
+
cmd_output = `rm -rf #{svn_tmp_dir}`
|
42
|
+
cmd_status = $?
|
43
|
+
cmd_status == 0 ? :success : :failure
|
44
|
+
if cmd_status == :failure
|
45
|
+
@log.error("Backup #{@backup_name} after_clean_source failed. Output #{cmd_output}")
|
46
|
+
end
|
47
|
+
return cmd_status
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module UBSafe
|
2
|
+
module Commands
|
3
|
+
end
|
4
|
+
end
|
5
|
+
|
6
|
+
require File.join_from_here('ubsafe_command_backup')
|
7
|
+
require File.join_from_here('ubsafe_command_backup_svn')
|
8
|
+
require File.join_from_here('ubsafe_command_backup_mysql')
|
9
|
+
require File.join_from_here('ubsafe_command_backup_postgres')
|
10
|
+
|
11
|
+
|
12
|
+
|
@@ -0,0 +1,166 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'yaml'
|
3
|
+
require 'erb'
|
4
|
+
require 'fileutils'
|
5
|
+
|
6
|
+
module UBSafe
|
7
|
+
|
8
|
+
class Config
|
9
|
+
|
10
|
+
attr_reader :options
|
11
|
+
|
12
|
+
class << self
|
13
|
+
|
14
|
+
##
|
15
|
+
# Get configuration settings
|
16
|
+
#
|
17
|
+
# @return [UBSafe::Config] Singleton instance of UBSafe::Config
|
18
|
+
#
|
19
|
+
def config
|
20
|
+
if (not defined?(@@config_instance)) or @@config_instance.nil?
|
21
|
+
@@config_instance = UBSafe::Config.new
|
22
|
+
end
|
23
|
+
return @@config_instance
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Reset the configuration settings
|
28
|
+
#
|
29
|
+
def reset
|
30
|
+
@@config_instance = nil
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
##
|
38
|
+
# Create a new Config instance
|
39
|
+
#
|
40
|
+
# @return [UBSafe::Config] Instance of UBSafe::Config
|
41
|
+
#
|
42
|
+
def initialize
|
43
|
+
@options = {}
|
44
|
+
end
|
45
|
+
|
46
|
+
public
|
47
|
+
|
48
|
+
##
|
49
|
+
# Load a Config instance
|
50
|
+
#
|
51
|
+
# @param [Array] args Command-line arguments
|
52
|
+
#
|
53
|
+
def load(args = nil)
|
54
|
+
@options[:config_file] = ENV['UBSAFE_CONFIG_FILE'] unless ENV['UBSAFE_CONFIG_FILE'].nil?
|
55
|
+
#puts "UBConfig.load After env check @options #{@options.inspect}"
|
56
|
+
@options.merge!(parse_args(args))
|
57
|
+
#puts "UBConfig.load After merge @options #{@options.inspect}"
|
58
|
+
if @options[:config_file]
|
59
|
+
@options.merge!(load_config_file(@options[:config_file]))
|
60
|
+
end
|
61
|
+
configure_logging
|
62
|
+
end
|
63
|
+
|
64
|
+
##
|
65
|
+
# Get the full set of configuration options for the specified backup
|
66
|
+
#
|
67
|
+
# @param [Symbol] backup_name
|
68
|
+
# @return [Hash] (Flattened?) hash with all the options for this backup. Nil if this backup is not present or enabled.
|
69
|
+
#
|
70
|
+
def full_options(backup_name)
|
71
|
+
backup_options = {}
|
72
|
+
# Get backup defaults
|
73
|
+
backup_options.merge!(@options[:backup_defaults].dup_contents_1_level)
|
74
|
+
# Get the specific backup definition
|
75
|
+
unless @options[:backups].has_key?(backup_name.to_sym)
|
76
|
+
@logger.fatal("The backup name specified '#{backup_name}' has no configuration defined in #{@options[:config_file]}")
|
77
|
+
raise Exception.new("Non-existent backup specified '#{backup_name}'")
|
78
|
+
end
|
79
|
+
backup_options.merge!(@options[:backups][backup_name.to_sym].dup_contents_1_level)
|
80
|
+
return nil unless backup_options[:enabled]
|
81
|
+
backup_options[:backup_name] = backup_name.to_s unless backup_options[:backup_name]
|
82
|
+
# Expand the backup host reference
|
83
|
+
selected_host = backup_options[:backup_host]
|
84
|
+
backup_options.merge!(@options[:backup_hosts][selected_host].dup_contents_1_level)
|
85
|
+
# Expand the backup type reference
|
86
|
+
selected_backup_type = backup_options[:backup_type]
|
87
|
+
backup_options.merge!(@options[:backup_types][selected_backup_type].dup_contents_1_level)
|
88
|
+
return backup_options
|
89
|
+
end
|
90
|
+
|
91
|
+
## Get logger
|
92
|
+
#
|
93
|
+
# @return [Logging::Logger] Common logger instance
|
94
|
+
#
|
95
|
+
def log
|
96
|
+
return defined?(@logger) ? @logger : nil
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
##
|
103
|
+
# Parse command-line arguments
|
104
|
+
#
|
105
|
+
# @param [Array] args Command-line arguments
|
106
|
+
# @return [Hash] Parsed options. :remainder contains any options not part
|
107
|
+
#
|
108
|
+
def parse_args(args = nil)
|
109
|
+
parsed_options = {:remainder => []}
|
110
|
+
if args and args.kind_of?(Array)
|
111
|
+
options = OptionParser.new
|
112
|
+
options.on("-c","--config CONFIG_FILE", String, "Name of configuration file") {|val| parsed_options[:config_file] = val }
|
113
|
+
options.on("-n","--name BACKUP_NAME", String, 'Backup Name') {|val| parsed_options[:backup_name] = val.downcase.to_sym}
|
114
|
+
parsed_options[:remainder] = options.parse(*args)
|
115
|
+
parsed_options[:options_help_text] = options.to_s
|
116
|
+
end
|
117
|
+
return parsed_options
|
118
|
+
end
|
119
|
+
|
120
|
+
##
|
121
|
+
# Load config file
|
122
|
+
#
|
123
|
+
# @param [String] config_file_name Configuration file name
|
124
|
+
# @return [Object] all the settings in the associated configuration file
|
125
|
+
#
|
126
|
+
def load_config_file(config_file_name)
|
127
|
+
contents = nil
|
128
|
+
begin
|
129
|
+
template = ERB.new(File.open(config_file_name).read)
|
130
|
+
contents = YAML.load(template.result)
|
131
|
+
rescue Exception => ex
|
132
|
+
puts "FATAL - Unable to find or load configuration file '#{config_file_name}'"
|
133
|
+
raise ex
|
134
|
+
#exit 1
|
135
|
+
end
|
136
|
+
return contents
|
137
|
+
end
|
138
|
+
|
139
|
+
##
|
140
|
+
# Initialize logging
|
141
|
+
#
|
142
|
+
def configure_logging
|
143
|
+
@logger_configuration = @options[:logging]
|
144
|
+
@logger = Logging::Logger[@logger_configuration[:log_identifier]]
|
145
|
+
logger_layout = Logging::Layouts::UBSafeLoggerLayout.new(@logger_configuration)
|
146
|
+
FileUtils.mkdir_p(File.expand_path(@logger_configuration[:log_directory]))
|
147
|
+
env = ENV['UBSAFE_ENV'] ? "_#{ENV['UBSAFE_ENV'].downcase}" : ''
|
148
|
+
log_file_name = @logger_configuration[:log_filename_pattern].gsub(/\%env\%/,env)
|
149
|
+
qualified_logger_file_name = File.expand_path(File.join(@logger_configuration[:log_directory],log_file_name))
|
150
|
+
@logger.add_appenders(
|
151
|
+
Logging::Appenders::RollingFile.new(@logger_configuration[:log_identifier],
|
152
|
+
{ :filename => qualified_logger_file_name,
|
153
|
+
:age => @logger_configuration[:log_rolling_frequency],
|
154
|
+
:keep => @logger_configuration[:logs_to_retain],
|
155
|
+
:safe => true,
|
156
|
+
:layout => logger_layout
|
157
|
+
}
|
158
|
+
|
159
|
+
)
|
160
|
+
)
|
161
|
+
@logger.level = @logger_configuration[:log_level]
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ubsafe
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.5"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tracy Flynn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-24 00:00:00 -04:00
|
13
|
+
default_executable: ubsafe
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - "="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: fastthread
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.7
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: logging
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.9.4
|
44
|
+
version:
|
45
|
+
description: "ubsafe was developed by: Tracy Flynn"
|
46
|
+
email: gems@olioinfo.net
|
47
|
+
executables:
|
48
|
+
- ubsafe
|
49
|
+
- ubsafe_file_exists
|
50
|
+
- ubsafe_file_mtime
|
51
|
+
- ubsafe_scp_cmd.expect
|
52
|
+
- ubsafe_ssh_cmd.expect
|
53
|
+
- ubsafer
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files: []
|
57
|
+
|
58
|
+
files:
|
59
|
+
- bin/ubsafe
|
60
|
+
- bin/ubsafe_cmd_with_password.expect
|
61
|
+
- bin/ubsafe_file_exists
|
62
|
+
- bin/ubsafe_file_mtime
|
63
|
+
- bin/ubsafe_scp_cmd.expect
|
64
|
+
- bin/ubsafe_ssh_cmd.expect
|
65
|
+
- bin/ubsafer
|
66
|
+
- lib/ubsafe/extensions/ubsafe_extensions.rb
|
67
|
+
- lib/ubsafe/extensions/ubsafe_file_extensions.rb
|
68
|
+
- lib/ubsafe/extensions/ubsafe_hash_extensions.rb
|
69
|
+
- lib/ubsafe/extensions/ubsafe_integer_extensions.rb
|
70
|
+
- lib/ubsafe/extensions/ubsafe_kernel_extensions.rb
|
71
|
+
- lib/ubsafe/extensions/ubsafe_logging_extensions.rb
|
72
|
+
- lib/ubsafe/ubsafe_commands/ubsafe_command_backup.rb
|
73
|
+
- lib/ubsafe/ubsafe_commands/ubsafe_command_backup_mysql.rb
|
74
|
+
- lib/ubsafe/ubsafe_commands/ubsafe_command_backup_postgres.rb
|
75
|
+
- lib/ubsafe/ubsafe_commands/ubsafe_command_backup_svn.rb
|
76
|
+
- lib/ubsafe/ubsafe_commands/ubsafe_commands.rb
|
77
|
+
- lib/ubsafe/ubsafe_config.rb
|
78
|
+
- lib/ubsafe.rb
|
79
|
+
- README
|
80
|
+
has_rdoc: true
|
81
|
+
homepage: http://www.olioinfo.net/projects
|
82
|
+
licenses: []
|
83
|
+
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: "0"
|
95
|
+
version:
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: "0"
|
101
|
+
version:
|
102
|
+
requirements: []
|
103
|
+
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 1.3.5
|
106
|
+
signing_key:
|
107
|
+
specification_version: 3
|
108
|
+
summary: ubsafe simplify and automate backup tasks
|
109
|
+
test_files: []
|
110
|
+
|