rmybackup 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Readme.md +9 -10
- data/bin/rmybackup +8 -1
- data/lib/rmybackup.rb +61 -10
- metadata +2 -2
data/Readme.md
CHANGED
@@ -3,19 +3,23 @@
|
|
3
3
|
RMyBackup was created to solve a simple problem I had, and is hopefully useful to somebody else out there. It is a quick way to backup up specified mysql databases using mysqldump. It writes a gzipped .sql file using a date/time naming convention to a specified directory.
|
4
4
|
|
5
5
|
### To Install
|
6
|
-
gem install rmybackup
|
6
|
+
# gem install rmybackup
|
7
7
|
|
8
8
|
## Usage
|
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 --config_file option. Default location is /etc/rmybackup.conf, or you can specify your own location.
|
13
|
+
|
14
|
+
# rmybackup --config_file /etc/rmybackup.conf
|
15
|
+
|
12
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.
|
13
17
|
|
14
18
|
# example my.cnf
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
+
|
20
|
+
[mysqldump]
|
21
|
+
user = root
|
22
|
+
password = roots_password
|
19
23
|
|
20
24
|
|
21
25
|
Once everything is set up correctly in the config file, and mysqldump is able to operate using credentials specified in the appropriate my.cnf file, you should simply be able to run the rmybackup command.
|
@@ -32,11 +36,6 @@ The default location for the configuration file is /etc/rmybackup.conf, formatte
|
|
32
36
|
#Backup Directory
|
33
37
|
backup_dir: /Users/bshelton/mysql_tmp
|
34
38
|
|
35
|
-
database_connection:
|
36
|
-
host: localhost
|
37
|
-
user: root
|
38
|
-
password: batman
|
39
|
-
|
40
39
|
#Databases to back up
|
41
40
|
databases: [
|
42
41
|
bercilak,
|
data/bin/rmybackup
CHANGED
@@ -12,12 +12,19 @@ require 'optparse'
|
|
12
12
|
|
13
13
|
#Set some globals
|
14
14
|
OPTIONS = { :config_file => "/etc/rmybackup.conf" }
|
15
|
-
GEM_VERSION = "0.0.
|
15
|
+
GEM_VERSION = "0.0.4"
|
16
16
|
|
17
17
|
#Process the command line arguments
|
18
18
|
ARGV.options do |opts|
|
19
19
|
opts.on("-f /etc/rmybackup.conf","--config_file /etc/rmybackup.conf","Set Config File",String) { |OPTIONS[:config_file]| }
|
20
20
|
opts.on("-v","--version","Outputs version") { puts "Version - #{GEM_VERSION}"; exit }
|
21
|
+
|
22
|
+
#Allow the user to write the sample config file to either the default (no file specified) or to the file they specify
|
23
|
+
#on the command line
|
24
|
+
opts.on("-i [config_file location]","--install_config [config_file location]","Generates a sample config file",String) do |ic|
|
25
|
+
RMyBackup.install_config(ic)
|
26
|
+
exit 0
|
27
|
+
end
|
21
28
|
opts.parse!
|
22
29
|
end
|
23
30
|
|
data/lib/rmybackup.rb
CHANGED
@@ -1,15 +1,65 @@
|
|
1
|
-
#Require Yaml
|
2
1
|
require 'yaml'
|
3
2
|
require 'time'
|
4
3
|
|
4
|
+
#The main RMyBackup library
|
5
5
|
class RMyBackup
|
6
|
-
|
6
|
+
|
7
7
|
def initialize(config_file)
|
8
8
|
@config_file = config_file
|
9
|
-
#if the config
|
9
|
+
#if the config file passes, run the backups
|
10
10
|
run_backups if parse_config
|
11
11
|
end
|
12
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"
|
24
|
+
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
|
+
|
13
63
|
private
|
14
64
|
#Run the backups, we should have proper validation at this point
|
15
65
|
def run_backups
|
@@ -22,13 +72,15 @@ class RMyBackup
|
|
22
72
|
#Cycle through databases to backup
|
23
73
|
@config['databases'].each do |db|
|
24
74
|
puts "Backing up #{db}\n"
|
25
|
-
system "#{mysql_dump} #{db} |#{gzip} > #{backup_dir}
|
75
|
+
system "#{mysql_dump} #{db} |#{gzip} > #{backup_dir}/#{db}_#{date_string}.sql.gz"
|
26
76
|
end
|
27
77
|
end
|
28
78
|
|
79
|
+
#Parse the config YAML file
|
29
80
|
def parse_config
|
30
81
|
@config = YAML::load(File.open(@config_file))
|
31
82
|
|
83
|
+
#Initialize error array
|
32
84
|
@error = Array.new
|
33
85
|
|
34
86
|
#Defaults
|
@@ -36,13 +88,11 @@ class RMyBackup
|
|
36
88
|
@config['mysqldump_command'] = "/usr/bin/mysqldump" if @config['mysqldump_command'].nil?
|
37
89
|
@config['find_command'] = "/usr/bin/find" if @config['find_command'].nil?
|
38
90
|
|
39
|
-
#
|
40
|
-
@config['backup_dir'] += "/" unless @config['backup_dir'][-1,1] == "/"
|
41
|
-
|
42
|
-
#Run Some checks
|
91
|
+
#Backup dir validation
|
43
92
|
if not File.directory? @config['backup_dir']
|
44
93
|
@error << "No Such Backup Directory #{@config['backup_dir']}"
|
45
94
|
else
|
95
|
+
@config['backup_dir'] = File.expand_path @config['backup_dir']
|
46
96
|
if not File.writable? @config['backup_dir']
|
47
97
|
@error << "Can't write to the backup directory - #{@config['backup_dir']}"
|
48
98
|
end
|
@@ -58,11 +108,12 @@ class RMyBackup
|
|
58
108
|
return true
|
59
109
|
else
|
60
110
|
@error.each {|e| puts "#{e}\n" }
|
61
|
-
exit
|
111
|
+
exit 1
|
62
112
|
end
|
113
|
+
#Rescue anything by displaying errors if we have them and exiting 1
|
63
114
|
rescue
|
64
115
|
@error.each {|e| puts "#{e}\n" }
|
65
|
-
exit
|
116
|
+
exit 1
|
66
117
|
return false
|
67
118
|
end
|
68
119
|
end
|