rmybackup 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Readme.md +2 -3
- data/bin/rmybackup +14 -4
- data/lib/rmybackup/config.rb +26 -0
- data/lib/rmybackup/config_file.txt +13 -0
- data/lib/rmybackup.rb +51 -100
- metadata +9 -4
data/Readme.md
CHANGED
@@ -9,9 +9,9 @@ RMyBackup was created to solve a simple problem I had, and is hopefully useful t
|
|
9
9
|
|
10
10
|
The gem will install an rmybackup binary. RMyBackup will read its configuration from /etc/rmybackup.conf (a sample configuration file is shown below). RMyBackup will backup the specified databases in the databases: [] list in the configuration file.
|
11
11
|
|
12
|
-
To generate a sample config file, use the --
|
12
|
+
To generate a sample config file, use the --instal_config option. Default location is /etc/rmybackup.conf, or you can specify your own location.
|
13
13
|
|
14
|
-
# rmybackup --
|
14
|
+
# rmybackup --install_config /etc/rmybackup.conf
|
15
15
|
|
16
16
|
Right now the script relies on your [mysqldump] configuration in either /etc/my.cnf or the user's ~/.my.cnf. I'm hoping to change this soon, allowing you to specify the host, user, password, and socket in the configuration file.
|
17
17
|
|
@@ -39,7 +39,6 @@ The default location for the configuration file is /etc/rmybackup.conf, formatte
|
|
39
39
|
#Databases to back up
|
40
40
|
databases: [
|
41
41
|
bercilak,
|
42
|
-
etrack,
|
43
42
|
bbpress
|
44
43
|
]
|
45
44
|
|
data/bin/rmybackup
CHANGED
@@ -10,9 +10,16 @@ end
|
|
10
10
|
|
11
11
|
require 'optparse'
|
12
12
|
|
13
|
-
#
|
14
|
-
|
15
|
-
|
13
|
+
#GLOBALS
|
14
|
+
|
15
|
+
#Default to local config file if it exists
|
16
|
+
if File.exists?(File.expand_path("~/.rmybackup.conf"))
|
17
|
+
OPTIONS = { :config_file => File.expand_path("~/.rmybackup.conf") }
|
18
|
+
else
|
19
|
+
OPTIONS = { :config_file => "/etc/rmybackup.conf" }
|
20
|
+
end
|
21
|
+
|
22
|
+
GEM_VERSION = "0.1.0"
|
16
23
|
|
17
24
|
#Process the command line arguments
|
18
25
|
ARGV.options do |opts|
|
@@ -20,6 +27,8 @@ ARGV.options do |opts|
|
|
20
27
|
opts.on("-f /etc/rmybackup.conf","--config_file /etc/rmybackup.conf","Set Config File",String) do |o|
|
21
28
|
OPTIONS[:config_file] = o
|
22
29
|
end
|
30
|
+
|
31
|
+
#Version
|
23
32
|
opts.on("-v","--version","Outputs version") { puts "Version - #{GEM_VERSION}"; exit }
|
24
33
|
|
25
34
|
#Allow the user to write the sample config file to either the default (no file specified) or to the file they specify
|
@@ -28,6 +37,7 @@ ARGV.options do |opts|
|
|
28
37
|
RMyBackup.install_config(ic)
|
29
38
|
exit 0
|
30
39
|
end
|
40
|
+
|
31
41
|
opts.parse!
|
32
42
|
end
|
33
43
|
|
@@ -38,4 +48,4 @@ if not File.exists? OPTIONS[:config_file]
|
|
38
48
|
end
|
39
49
|
|
40
50
|
#Run
|
41
|
-
RMyBackup.new(OPTIONS[:config_file])
|
51
|
+
RMyBackup::Base.new(OPTIONS[:config_file])
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module RMyBackup
|
2
|
+
#Install a baseline config file from the template
|
3
|
+
def self.install_config(file=false)
|
4
|
+
#Default the file location
|
5
|
+
file = "/etc/rmybackup.conf" if not file
|
6
|
+
file = File.expand_path(file)
|
7
|
+
|
8
|
+
if File.exists? file
|
9
|
+
puts "The file already exists, do you want to overwrite it? (Y/Yes):"
|
10
|
+
STDOUT.flush
|
11
|
+
answer = gets.chomp
|
12
|
+
exit 1 unless answer.upcase == "Y" or answer.upcase == "YES"
|
13
|
+
end
|
14
|
+
|
15
|
+
#Read Sample Config File
|
16
|
+
config_file = File.read(File.expand_path("../../rmybackup/config_file.txt",__FILE__))
|
17
|
+
|
18
|
+
begin
|
19
|
+
File.open(file,'w') {|f| f.write(config_file) }
|
20
|
+
puts "Installing #{file}"
|
21
|
+
rescue Errno::EACCES
|
22
|
+
puts "Can't write to - #{file}"
|
23
|
+
end
|
24
|
+
exit 0
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#Backup Directory
|
2
|
+
backup_dir: /Users/bshelton/mysql_tmp/
|
3
|
+
|
4
|
+
#Databases to back up
|
5
|
+
#databases: [ test, test2 ]
|
6
|
+
|
7
|
+
databases: []
|
8
|
+
|
9
|
+
#Command Locations
|
10
|
+
#You can override where to find the needed system commands, default locations are prefixed with /usr/bin/
|
11
|
+
|
12
|
+
#mysqldump_command: /usr/local/mysql/bin/mysqldump
|
13
|
+
#gzip_command: /usr/bin/gzip
|
data/lib/rmybackup.rb
CHANGED
@@ -1,119 +1,70 @@
|
|
1
1
|
require 'yaml'
|
2
2
|
require 'time'
|
3
|
+
require 'rmybackup/config'
|
3
4
|
|
4
|
-
|
5
|
-
class
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
run_backups if parse_config
|
11
|
-
end
|
12
|
-
|
13
|
-
#Install a baseline config file from the template
|
14
|
-
def self.install_config(file=false)
|
15
|
-
#Default the file location
|
16
|
-
file = "/etc/rmybackup.conf" if not file
|
17
|
-
file = File.expand_path(file)
|
18
|
-
|
19
|
-
if File.exists? file
|
20
|
-
puts "The file already exists, do you want to overwrite it? (Y/Yes):"
|
21
|
-
STDOUT.flush
|
22
|
-
answer = gets.chomp
|
23
|
-
exit 1 unless answer.upcase == "Y" or answer.upcase == "YES"
|
5
|
+
module RMyBackup
|
6
|
+
class Base
|
7
|
+
def initialize(config_file)
|
8
|
+
@config_file = config_file
|
9
|
+
#if the config file passes, run the backups
|
10
|
+
run_backups if parse_config
|
24
11
|
end
|
25
|
-
|
26
|
-
config_file = <<CONFIG_FILE
|
27
|
-
#Configuration File in YAML format
|
28
|
-
|
29
|
-
#Backup Directory
|
30
|
-
backup_dir: /Users/bshelton/mysql_tmp/
|
31
|
-
|
32
|
-
#Databases to backup
|
33
|
-
#This is an array of databases that are to be backed ups
|
34
|
-
# i.e.
|
35
|
-
# databases: [
|
36
|
-
# test,
|
37
|
-
# mysql,
|
38
|
-
# important_db
|
39
|
-
# ]
|
40
|
-
|
41
|
-
#Databases to back up
|
42
|
-
databases: [
|
43
|
-
test
|
44
|
-
]
|
45
|
-
|
46
|
-
#Command Locations
|
47
|
-
#You can override where to find the needed system commands, default locations are prefixed with /usr/bin/
|
48
|
-
|
49
|
-
#mysqldump_command: /usr/local/mysql/bin/mysqldump
|
50
|
-
#gzip_command: /usr/bin/gzip
|
51
|
-
#find_command: /usr/bin/find
|
52
|
-
CONFIG_FILE
|
53
|
-
|
54
|
-
begin
|
55
|
-
File.open(file,'w') {|f| f.write(config_file) }
|
56
|
-
puts "Installing #{file}"
|
57
|
-
rescue Errno::EACCES
|
58
|
-
puts "Can't write to - #{file}"
|
59
|
-
end
|
60
|
-
exit 0
|
61
|
-
end
|
62
12
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
13
|
+
private
|
14
|
+
#Run the backups, we should have proper validation at this point
|
15
|
+
def run_backups
|
16
|
+
#Grab some config variables
|
17
|
+
mysql_dump = @config['mysqldump_command']
|
18
|
+
backup_dir = @config['backup_dir']
|
19
|
+
gzip = @config['gzip_command']
|
20
|
+
date_string = Time.now.strftime "%m_%d_%Y_%H_%M"
|
71
21
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
22
|
+
#Cycle through databases to backup
|
23
|
+
@config['databases'].each do |db|
|
24
|
+
puts "Backing up #{db}\n"
|
25
|
+
system "#{mysql_dump} #{db} |#{gzip} > #{backup_dir}/#{db}_#{date_string}.sql.gz"
|
26
|
+
end
|
76
27
|
end
|
77
|
-
end
|
78
28
|
|
79
|
-
|
80
|
-
|
81
|
-
|
29
|
+
#Parse the config YAML file
|
30
|
+
def parse_config
|
31
|
+
@config = YAML::load(File.open(@config_file))
|
82
32
|
|
83
|
-
|
84
|
-
|
33
|
+
#Initialize error array
|
34
|
+
@error = Array.new
|
85
35
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
36
|
+
#Defaults
|
37
|
+
@config['gzip_command'] = "/usr/bin/gzip" if @config['gzip_command'].nil?
|
38
|
+
@config['mysqldump_command'] = "/usr/bin/mysqldump" if @config['mysqldump_command'].nil?
|
39
|
+
@config['find_command'] = "/usr/bin/find" if @config['find_command'].nil?
|
90
40
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
41
|
+
#Backup dir validation
|
42
|
+
if not File.directory? @config['backup_dir']
|
43
|
+
@error << "No Such Backup Directory #{@config['backup_dir']}"
|
44
|
+
else
|
45
|
+
@config['backup_dir'] = File.expand_path @config['backup_dir']
|
46
|
+
if not File.writable? @config['backup_dir']
|
47
|
+
@error << "Can't write to the backup directory - #{@config['backup_dir']}"
|
48
|
+
end
|
98
49
|
end
|
99
|
-
end
|
100
50
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
51
|
+
#Check Commands
|
52
|
+
@error << "Can't locate find command: #{@config['find_command']}" unless File.exists? @config['find_command']
|
53
|
+
@error << "Can't locate gzip command: #{@config['gzip_command']}" unless File.exists? @config['gzip_command']
|
54
|
+
@error << "Can't locate mysqldump command: #{@config['mysqldump_command']}" unless File.exists? @config['mysqldump_command']
|
105
55
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
56
|
+
#See if we've created any errors, if so, display them and exit
|
57
|
+
if @error.empty?
|
58
|
+
return true
|
59
|
+
else
|
60
|
+
@error.each {|e| puts "#{e}\n" }
|
61
|
+
exit 1
|
62
|
+
end
|
63
|
+
#Rescue anything by displaying errors if we have them and exiting 1
|
64
|
+
rescue
|
110
65
|
@error.each {|e| puts "#{e}\n" }
|
111
66
|
exit 1
|
67
|
+
return false
|
112
68
|
end
|
113
|
-
#Rescue anything by displaying errors if we have them and exiting 1
|
114
|
-
rescue
|
115
|
-
@error.each {|e| puts "#{e}\n" }
|
116
|
-
exit 1
|
117
|
-
return false
|
118
69
|
end
|
119
70
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.5
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Bryan Shelton
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-05-11 00:00:00 -06:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -29,6 +29,9 @@ extra_rdoc_files: []
|
|
29
29
|
files:
|
30
30
|
- lib/rmybackup.rb
|
31
31
|
- Readme.md
|
32
|
+
- lib/rmybackup/config_file.txt
|
33
|
+
- lib/rmybackup/config.rb
|
34
|
+
- bin/rmybackup
|
32
35
|
has_rdoc: true
|
33
36
|
homepage: http://github.com/bshelton229/rmybackup/
|
34
37
|
licenses: []
|
@@ -39,6 +42,7 @@ rdoc_options: []
|
|
39
42
|
require_paths:
|
40
43
|
- lib
|
41
44
|
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
42
46
|
requirements:
|
43
47
|
- - ">="
|
44
48
|
- !ruby/object:Gem::Version
|
@@ -46,6 +50,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
46
50
|
- 0
|
47
51
|
version: "0"
|
48
52
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
49
54
|
requirements:
|
50
55
|
- - ">="
|
51
56
|
- !ruby/object:Gem::Version
|
@@ -55,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
60
|
requirements: []
|
56
61
|
|
57
62
|
rubyforge_project:
|
58
|
-
rubygems_version: 1.3.
|
63
|
+
rubygems_version: 1.3.7
|
59
64
|
signing_key:
|
60
65
|
specification_version: 3
|
61
66
|
summary: Ruby Mysql Backup Script
|