Module: BackupPlugins::Remove

Included in:
Keepitsafe
Defined in:
lib/plugins/remove.rb

Instance Method Summary collapse

Instance Method Details

#keep_all(backups, time_back = 1.day.ago) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/plugins/remove.rb', line 97

def keep_all backups,time_back = 1.day.ago
  hours = []
   backups.sort.each do |time|
     if time > 1.days.ago
       hours << time
     end
   end
   
   hours
end

#keep_first_each_day(backups, time_back = 7.days.ago) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/plugins/remove.rb', line 84

def keep_first_each_day backups,time_back = 7.days.ago
  
  days = {}
  backups.sort.each do |time|
    if time > time_back
      string = time.strftime('%Y%m%d')
      days[string] = time unless days[string]
    end
  end
  
  days.values
end

#keep_first_each_month(backups, time_back = 1.year.ago) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/plugins/remove.rb', line 61

def keep_first_each_month backups,time_back = 1.year.ago
  months = {}
  backups.sort.each do |time|
    if time > time_back
      months[time.strftime('%Y%m')] = time unless months[time.strftime('%Y%m')]
    end
  end
  
  months.values
end

#keep_first_each_week(backups, time_back = 2.months.ago) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/plugins/remove.rb', line 72

def keep_first_each_week backups,time_back = 2.months.ago
  weeks = {}
  backups.sort.each do |time|
    if time > time_back
      string = time.strftime('%Y%m') + time.to_date.cweek.to_s
      weeks[string] = time unless weeks[string]
    end
  end
  
  weeks.values
end

#remove(paths, test_run = false) ⇒ Object



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
34
35
36
37
38
39
40
# File 'lib/plugins/remove.rb', line 8

def remove paths,test_run = false
   
   puts "\n\tbackup.remove(#{paths},#{test_run})\n\n"
  
  paths = [paths] unless paths.is_a? Array
  
  raise "We are missing a block here!" unless block_given?
  
  paths.each do |path|
    
    backups = run_cmd("ls #{path}").strip.split("\n").map do |file_name|
      time_data = DateTime.strptime(file_name,'%Y%m%d_%H%M.%S').to_time.utc
      Time.utc(time_data.year,time_data.month,time_data.day,time_data.hour,time_data.min,time_data.sec,time_data.usec)
    end
    
    to_keep = (yield(backups) || []).uniq
    
    to_remove = backups.delete_if {|v| to_keep.include?(v) }
    
    to_remove.each do |backup_to_remove|
      
      cmd = "rm -rf #{path}/#{backup_to_remove.strftime("%Y%m%d_%H%M.%S")}*"
      
      if test_run
        puts cmd
      else
        run_cmd(cmd)
      end
      
    end

  end
end

#remove_old_backupsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/plugins/remove.rb', line 42

def remove_old_backups
  remove(run_cmd('ls -m ~/backups/').strip.split(/,\s/).map{|path| "~/backups/#{path}" }) do |backups|

    # First backup of each month 1 years back
    months = keep_first_each_month(backups,1.years.ago)

    # First backup of each week 2 months back
    weeks = keep_first_each_week(backups,1.months.ago)

    # First backup of each day 1 week back
    days = keep_first_each_day(backups,7.days.ago)

    # All backups during the last 24 hours
    all = keep_all(backups,1.day.ago)

    all + days + weeks + months
  end 
end