rmybackup 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -1,28 +1,28 @@
1
1
  # RMyBackup
2
2
 
3
- RMyBackup was created to solve a simple problem I had, and is hopefully useful to somebody else out there. RMyBackup 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.
3
+ RMyBackup was created to solve a simple problem I had, and is hopefully useful to somebody else out there. RMyBackup is a quick way to backup up your 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
6
  # gem install rmybackup
7
7
 
8
8
  ## Usage
9
9
 
10
- The gem will install an rmybackup binary. RMyBackup will read its configuration from ~/.rmybackup.conf or /etc/rmybackup.conf (a sample configuration file is shown below). The .rmybackup.conf file in your home folder will be used if present, next it rmybackup will look for /etc/rmybackup. You may also specify an alternate config file on the command line using rmybackup --config-file /alternate/config/file. RMyBackup will backup the specified databases in the databases: [] list in the configuration file.
10
+ The gem will install an rmybackup binary. RMyBackup will read its configuration from ~/.rmybackup.conf or /etc/rmybackup.conf (a sample configuration file is shown below). The .rmybackup.conf file in your home folder will be used if present, next rmybackup will look for /etc/rmybackup. You may also specify an alternate config file on the command line using rmybackup --config-file /alternate/config/file. RMyBackup will backup all databases excluding the databases listed in the skip_databases: [] list in the configuration file.
11
11
 
12
12
  To generate a sample config file, use the --instal-config option. Default location is /etc/rmybackup.conf, or if we can't write to /etc, ~/.rmybacukp.conf.
13
13
 
14
14
  # rmybackup --install-config [/config/location]
15
15
 
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.
16
+ If use_mycnf_credentials is set to true in the config file, mysqldump will not be passed --user, --password, or --host based on the values in the config file. The script will rely on your [mysqldump] configuration in either /etc/my.cnf or the user's ~/.my.cnf. This is more secure if running on a shared server.
17
17
 
18
- # example my.cnf
18
+ # example my.cnf or ~/.my.cnf
19
19
 
20
20
  [mysqldump]
21
21
  user = root
22
22
  password = roots_password
23
23
 
24
24
 
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.
25
+ Once everything is set up, simply run the rmybackup command. It will connect to the mysql server using the values in your config file and back up your databases.
26
26
 
27
27
  # rmybackup
28
28
 
@@ -31,15 +31,26 @@ Once everything is set up correctly in the config file, and mysqldump is able to
31
31
 
32
32
  The default location for the configuration file is /etc/rmybackup.conf then ~/.rmybacukp.conf, it's formatted in YAML. You can specify a different config file on the command line using the --config-file (-f) option.
33
33
 
34
+ ---
34
35
  backup_dir: /Users/username/mysql_backups/
35
- #Number of days to keep backups
36
36
  remove_after: 7
37
37
 
38
+ #Database
39
+ username: root
40
+ password: password
41
+ host: localhost
42
+
43
+ #If this is set to true, no --user --password or --host switches will be passed to
44
+ #mysqldump. You will need to have credentials within /etc/my.cnf or ~/.my.cnf
45
+ use_mycnf_credentials: false
46
+
38
47
  #Databases to back up
39
- databases: [ test2, test3 ]
48
+ skip_databases: [ mysql, test, information_schema ]
40
49
 
41
50
  #Command Locations
42
- mysqldump_command: /usr/local/mysql/bin/mysqldump
43
- gzip_command: /usr/bin/gzip
51
+ #You can override where to find the needed system commands, default locations are prefixed with /usr/bin/
52
+
53
+ #mysqldump_command: /usr/local/mysql/bin/mysqldump
54
+ #gzip_command: /usr/bin/gzip
44
55
 
45
56
  If mysqldump_command, or gzip_command are left out, they will default to finding the applications in /usr/bin
data/bin/rmybackup CHANGED
@@ -8,22 +8,22 @@ require 'optparse'
8
8
  #GLOBALS
9
9
  #Default to local config file if it exists
10
10
  if File.exists?(File.expand_path("~/.rmybackup.conf"))
11
- OPTIONS = { :config_file => File.expand_path("~/.rmybackup.conf") }
11
+ options = { :config_file => File.expand_path("~/.rmybackup.conf") }
12
12
  else
13
- OPTIONS = { :config_file => "/etc/rmybackup.conf" }
13
+ options = { :config_file => "/etc/rmybackup.conf" }
14
14
  end
15
15
 
16
- GEM_VERSION = "0.1.2"
16
+ gem_version = RMyBackup::GEM_VERSION
17
17
 
18
18
  #Process the command line arguments
19
19
  ARGV.options do |opts|
20
20
  #Set the config file
21
21
  opts.on("-f /etc/rmybackup.conf","--config-file /etc/rmybackup.conf","Set Config File",String) do |o|
22
- OPTIONS[:config_file] = o
22
+ options[:config_file] = o
23
23
  end
24
24
 
25
25
  #Version
26
- opts.on("-v","--version","Outputs version") { puts "Version - #{GEM_VERSION}"; exit }
26
+ opts.on("-v","--version","Outputs version") { puts "Version - #{gem_version}"; exit }
27
27
 
28
28
  #Allow the user to write the sample config file to either the default (no file specified) or to the file they specify
29
29
  #on the command line
@@ -36,10 +36,10 @@ ARGV.options do |opts|
36
36
  end
37
37
 
38
38
  #If the config file doesn't exist, warn and exit
39
- if not File.exists? OPTIONS[:config_file]
40
- puts "Unable to read the configuration file - #{OPTIONS[:config_file]}"
39
+ if not File.exists? options[:config_file]
40
+ puts "Unable to read the configuration file - #{options[:config_file]}"
41
41
  exit
42
42
  end
43
43
 
44
44
  #Run
45
- RMyBackup::Base.new(OPTIONS[:config_file])
45
+ RMyBackup::Base.new(options[:config_file])
@@ -1,10 +1,16 @@
1
+ ---
1
2
  backup_dir: /Users/username/mysql_backups/
2
3
  remove_after: 7
3
4
 
4
- #Databases to back up
5
- #databases: [ test, test2 ]
5
+ #Database
6
+ username: root
7
+ password: password
8
+ host: localhost
9
+
10
+ use_mycnf_credentials: false
6
11
 
7
- databases: []
12
+ #Databases to back up
13
+ skip_databases: [ mysql, test, information_schema ]
8
14
 
9
15
  #Command Locations
10
16
  #You can override where to find the needed system commands, default locations are prefixed with /usr/bin/
data/lib/rmybackup.rb CHANGED
@@ -1,9 +1,14 @@
1
1
  require 'yaml'
2
2
  require 'time'
3
+ require 'mysql'
4
+
3
5
  require File.expand_path('../rmybackup/install_config',__FILE__)
4
6
  require File.expand_path('../rmybackup/purge_files',__FILE__)
5
7
 
6
8
  module RMyBackup
9
+
10
+ GEM_VERSION = "0.2.0"
11
+
7
12
  class Base
8
13
  def initialize(config_file)
9
14
  @config_file = config_file
@@ -17,18 +22,43 @@ module RMyBackup
17
22
 
18
23
  #Grab some config variables
19
24
  mysql_dump = @config['mysqldump_command']
20
- backup_dir = @config['backup_dir']
25
+ backup_root = @config['backup_dir']
21
26
  gzip = @config['gzip_command']
22
27
  date_string = Time.now.strftime "%m_%d_%Y_%H_%M"
23
28
 
29
+
24
30
  #Cycle through databases to backup
25
- @config['databases'].each do |db|
31
+ get_databases.each do |db|
32
+ backup_dir = File.expand_path("#{backup_root}/#{db}")
33
+ Dir.mkdir(backup_dir) if not File.exists?(backup_dir)
34
+
35
+ #Decide if we use my.cnf or creds on cli
36
+ if @config['use_mycnf_credentials']
37
+ cred_string = " --user=#{@config['username']} --password=#{@config['password']} --host=#{@config['host']}"
38
+ else
39
+ cred_string = ''
40
+ end
41
+
26
42
  puts "Backing up #{db}\n"
27
- system "#{mysql_dump} #{db} |#{gzip} > #{backup_dir}/#{db}_#{date_string}.sql.gz"
43
+ system "#{mysql_dump}#{cred_string} #{db} |#{gzip} > #{backup_dir}/#{db}_#{date_string}.sql.gz"
44
+
45
+ #Purge after x days
46
+ RMyBackup.purge_days(backup_dir,@config['remove_after'])
28
47
  end
29
-
30
- #Purges after x days
31
- RMyBackup.purge_days(@config['backup_dir'],@config['remove_after'])
48
+ end
49
+
50
+ #Get Databases from MySQL
51
+ def get_databases
52
+ dbc = Mysql.real_connect(@config['host'],@config['username'],@config['password'])
53
+ res = dbc.query('SHOW DATABASES;')
54
+ databases = []
55
+ res.each_hash do |db|
56
+ databases << db['Database']
57
+ end
58
+ return databases - @config['skip_databases']
59
+ rescue
60
+ puts "There was a problem connecting to the mysql server"
61
+ exit 0
32
62
  end
33
63
 
34
64
  #Parse the config YAML file
@@ -44,6 +74,13 @@ module RMyBackup
44
74
  @config['find_command'] = "/usr/bin/find" if @config['find_command'].nil?
45
75
  @config['remove_after'] = @config['remove_after'] || false
46
76
 
77
+ @config['use_mycnf_credentials'] = @config['use_mycnf_credentials'] ? true : false
78
+
79
+ #Database Config
80
+ @config['username'] = @config['username'] || false
81
+ @config['password'] = @config['password'] || false
82
+ @config['host'] = @config['host'] || false
83
+
47
84
  #Backup dir validation
48
85
  if not File.directory? @config['backup_dir']
49
86
  @error << "No Such Backup Directory #{@config['backup_dir']}"
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
7
  - 2
9
- version: 0.1.2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Bryan Shelton
@@ -16,8 +16,20 @@ cert_chain: []
16
16
 
17
17
  date: 2010-05-13 00:00:00 -06:00
18
18
  default_executable:
19
- dependencies: []
20
-
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: mysql
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
21
33
  description: Ruby mysql backup script, the script uses mysqldump from the system
22
34
  email: bryan@sheltonopensolutions.com
23
35
  executables: