Module: CmdTools::Command::Backup

Extended by:
RubyPatch::AutoLoad
Defined in:
lib/cmd_tools/command/backup.rb

Constant Summary collapse

BAK_DIR_HEAD =
"bak_since_"
BAK_DIR_REG =
/(\A|\/)#{BAK_DIR_HEAD}\d{12}\z/
BAK_PREFIX =
'bak.'

Class Method Summary collapse

Class Method Details

.run(*files) ⇒ Object

Back up files and directories to bak_dir.

Parameters:

  • files (Array<String>)

    Files and directories to be backed up.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cmd_tools/command/backup.rb', line 13

def self.run(*files)
  time_stamp = Time.now.ymdhms
  bak_dir = get_bak_dir(time_stamp)
  nested_bak_dir_reg = /\A#{bak_dir}\/.*#{BAK_DIR_HEAD}\d{12}\z/
  FileUtils.mkdir_p(bak_dir)
  (files.flatten.map{|f| f.chomp('/')} - ['.', '..', bak_dir])\
    .select{|f| File.exist?(f)}\
    .each{|f|
    dest = get_dest(bak_dir, f, time_stamp)
    begin
      FileUtils.cp_r(f, dest)
    rescue
      warn "WARN: Failed to back up #{f}"
    end

    delete_nested_bak_dir(dest, nested_bak_dir_reg) if(File.directory?(dest))
  }
end