rmybackup 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -1,17 +1,17 @@
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. 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.
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.
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 /etc/rmybackup.conf (a sample configuration file is shown below). 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 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.
11
11
 
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.
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
- # rmybackup --install_config /etc/rmybackup.conf
14
+ # rmybackup --install-config [/config/location]
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
 
@@ -29,22 +29,17 @@ Once everything is set up correctly in the config file, and mysqldump is able to
29
29
 
30
30
  ## Sample Configuration File
31
31
 
32
- The default location for the configuration file is /etc/rmybackup.conf, formatted in YAML. You can specify a different config file on the command line using the --config_file (-f) switch.
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
- #Configuration File in YAML format
35
-
36
- #Backup Directory
37
- backup_dir: /Users/bshelton/mysql_tmp
34
+ backup_dir: /Users/username/mysql_backups/
35
+ #Number of days to keep backups
36
+ remove_after: 7
38
37
 
39
38
  #Databases to back up
40
- databases: [
41
- bercilak,
42
- bbpress
43
- ]
39
+ databases: [ test2, test3 ]
44
40
 
45
41
  #Command Locations
46
42
  mysqldump_command: /usr/local/mysql/bin/mysqldump
47
43
  gzip_command: /usr/bin/gzip
48
- find_command: /usr/bin/find
49
44
 
50
- If mysqldump_command, gzip_command, or find_command are left out, they will default to finding the applications in /usr/bin
45
+ If mysqldump_command, or gzip_command are left out, they will default to finding the applications in /usr/bin
data/bin/rmybackup CHANGED
@@ -1,17 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #Load our libraries, if we can't load them from the gem, assume we're running from source
4
- begin
5
- require 'rubygems'
6
- require 'rmybackup'
7
- rescue LoadError
8
- require '../lib/rmybackup'
9
- end
10
-
4
+ require 'rubygems'
5
+ require 'rmybackup'
11
6
  require 'optparse'
12
7
 
13
8
  #GLOBALS
14
-
15
9
  #Default to local config file if it exists
16
10
  if File.exists?(File.expand_path("~/.rmybackup.conf"))
17
11
  OPTIONS = { :config_file => File.expand_path("~/.rmybackup.conf") }
@@ -19,12 +13,12 @@ else
19
13
  OPTIONS = { :config_file => "/etc/rmybackup.conf" }
20
14
  end
21
15
 
22
- GEM_VERSION = "0.1.0"
16
+ GEM_VERSION = "0.1.2"
23
17
 
24
18
  #Process the command line arguments
25
19
  ARGV.options do |opts|
26
20
  #Set the config file
27
- opts.on("-f /etc/rmybackup.conf","--config_file /etc/rmybackup.conf","Set Config File",String) do |o|
21
+ opts.on("-f /etc/rmybackup.conf","--config-file /etc/rmybackup.conf","Set Config File",String) do |o|
28
22
  OPTIONS[:config_file] = o
29
23
  end
30
24
 
@@ -33,7 +27,7 @@ ARGV.options do |opts|
33
27
 
34
28
  #Allow the user to write the sample config file to either the default (no file specified) or to the file they specify
35
29
  #on the command line
36
- opts.on("-i [config_file location]","--install_config [config_file location]","Generates a sample config file",String) do |ic|
30
+ opts.on("-i [config_file location]","--install-config [config_file location]","Generates a sample config file",String) do |ic|
37
31
  RMyBackup.install_config(ic)
38
32
  exit 0
39
33
  end
@@ -48,4 +42,4 @@ if not File.exists? OPTIONS[:config_file]
48
42
  end
49
43
 
50
44
  #Run
51
- RMyBackup::Base.new(OPTIONS[:config_file])
45
+ RMyBackup::Base.new(OPTIONS[:config_file])
@@ -1,5 +1,5 @@
1
- #Backup Directory
2
- backup_dir: /Users/bshelton/mysql_tmp/
1
+ backup_dir: /Users/username/mysql_backups/
2
+ remove_after: 7
3
3
 
4
4
  #Databases to back up
5
5
  #databases: [ test, test2 ]
@@ -2,11 +2,19 @@ module RMyBackup
2
2
  #Install a baseline config file from the template
3
3
  def self.install_config(file=false)
4
4
  #Default the file location
5
- file = "/etc/rmybackup.conf" if not file
5
+ if not file
6
+ if File.writable_real?("/etc/rmybackup.conf")
7
+ file = "/etc/rmybackup.conf"
8
+ else
9
+ file = "~/.rmybackup.conf"
10
+ end
11
+ end
12
+
13
+ #Expand the path
6
14
  file = File.expand_path(file)
7
-
15
+
8
16
  if File.exists? file
9
- puts "The file already exists, do you want to overwrite it? (Y/Yes):"
17
+ puts "#{file} already exists, do you want to overwrite it? (Y/n):"
10
18
  STDOUT.flush
11
19
  answer = gets.chomp
12
20
  exit 1 unless answer.upcase == "Y" or answer.upcase == "YES"
@@ -0,0 +1,17 @@
1
+ require 'date'
2
+ require 'time'
3
+
4
+ module RMyBackup
5
+ def self.purge_days(path,days=false)
6
+ return true unless days
7
+ Dir["#{path}/*.sql.gz"].each do |file|
8
+ mtime = File.mtime(File.expand_path(file))
9
+ mdate = Date.parse("#{mtime.year}-#{mtime.month}-#{mtime.day}")
10
+ date = Date.today - days
11
+ if mdate < date
12
+ puts "Cleaning up - #{file}"
13
+ File.delete(file)
14
+ end
15
+ end
16
+ end
17
+ end
data/lib/rmybackup.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'yaml'
2
2
  require 'time'
3
- require 'rmybackup/config'
3
+ require File.expand_path('../rmybackup/install_config',__FILE__)
4
+ require File.expand_path('../rmybackup/purge_files',__FILE__)
4
5
 
5
6
  module RMyBackup
6
7
  class Base
@@ -13,6 +14,7 @@ module RMyBackup
13
14
  private
14
15
  #Run the backups, we should have proper validation at this point
15
16
  def run_backups
17
+
16
18
  #Grab some config variables
17
19
  mysql_dump = @config['mysqldump_command']
18
20
  backup_dir = @config['backup_dir']
@@ -24,6 +26,9 @@ module RMyBackup
24
26
  puts "Backing up #{db}\n"
25
27
  system "#{mysql_dump} #{db} |#{gzip} > #{backup_dir}/#{db}_#{date_string}.sql.gz"
26
28
  end
29
+
30
+ #Purges after x days
31
+ RMyBackup.purge_days(@config['backup_dir'],@config['remove_after'])
27
32
  end
28
33
 
29
34
  #Parse the config YAML file
@@ -37,6 +42,7 @@ module RMyBackup
37
42
  @config['gzip_command'] = "/usr/bin/gzip" if @config['gzip_command'].nil?
38
43
  @config['mysqldump_command'] = "/usr/bin/mysqldump" if @config['mysqldump_command'].nil?
39
44
  @config['find_command'] = "/usr/bin/find" if @config['find_command'].nil?
45
+ @config['remove_after'] = @config['remove_after'] || false
40
46
 
41
47
  #Backup dir validation
42
48
  if not File.directory? @config['backup_dir']
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 2
9
+ version: 0.1.2
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-05-11 00:00:00 -06:00
17
+ date: 2010-05-13 00:00:00 -06:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -30,7 +30,8 @@ files:
30
30
  - lib/rmybackup.rb
31
31
  - Readme.md
32
32
  - lib/rmybackup/config_file.txt
33
- - lib/rmybackup/config.rb
33
+ - lib/rmybackup/install_config.rb
34
+ - lib/rmybackup/purge_files.rb
34
35
  - bin/rmybackup
35
36
  has_rdoc: true
36
37
  homepage: http://github.com/bshelton229/rmybackup/