Module: RMyBackup

Defined in:
lib/rmybackup.rb,
lib/rmybackup/purge_files.rb,
lib/rmybackup/install_config.rb

Defined Under Namespace

Classes: Base

Constant Summary collapse

GEM_VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.install_config(file = false) ⇒ Object

Install a baseline config file from the template



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rmybackup/install_config.rb', line 3

def self.install_config(file=false)
  #Default the file location
  if not file
    if File.writable_real?("/etc/rmybackup.conf")
      file = "/etc/rmybackup.conf"
    else
      file = "~/.rmybackup.conf"
    end
  end
  
  #Expand the path
  file = File.expand_path(file)
  
  if File.exists? file
    puts "#{file} already exists, do you want to overwrite it? (Y/n):"
    STDOUT.flush
    answer = gets.chomp
    exit 1 unless answer.upcase == "Y" or answer.upcase == "YES"
  end

  #Read Sample Config File
  config_file = File.read(File.expand_path("../../rmybackup/config_file.txt",__FILE__))

  begin
    File.open(file,'w') {|f| f.write(config_file) }
    puts "Installing #{file}"
  rescue Errno::EACCES
    puts "Can't write to - #{file}"
  end
  exit 0
end

.purge_days(path, days = false) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rmybackup/purge_files.rb', line 5

def self.purge_days(path,days=false)
  return true unless days
  Dir["#{path}/*.sql.gz"].each do |file|
    mtime = File.mtime(File.expand_path(file))
    mdate = Date.parse("#{mtime.year}-#{mtime.month}-#{mtime.day}")
    date = Date.today - days
    if mdate < date
      puts "Cleaning up - #{file}"
      File.delete(file)
    end
  end
end

.purge_number(path, number = false) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/rmybackup/purge_files.rb', line 18

def self.purge_number(path,number=false)
  return true unless number
  all_files = Dir[File.join(path,"*.gz")].sort_by {|f| File.mtime(f)}.reverse
  keep = all_files[0..number - 1]
  remove = all_files - keep
  remove.each do |f|
    File.delete f
  end
end