-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathprune.rb
47 lines (42 loc) · 949 Bytes
/
prune.rb
1
2
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env ruby
# Deletes/Prunes old backups
# Use at your own risk
#
# Author: Matthias-Christian Ott ([email protected])
# License: GPLv3
require 'optparse'
require 'date'
require 'fileutils'
options = { :limit => 30 }
OptionParser.new do |opts|
opts.on '-l', '--limit limit',
"set archive limit (default: #{options[:limit]})" do |v|
options[:limit] = v.to_i
end
end.parse!
if ARGV.size < 1
warn "no backup directory given"
exit 1
elsif ARGV.size > 1
warn "more than one backup directory given"
exit 1
end
today = DateTime.strptime Time.now.strftime('%Y%m/%d'), '%Y%m/%d'
limit = today - options[:limit]
Dir.chdir ARGV[0]
Dir.glob '*/*' do |entry|
begin
date = DateTime.strptime entry, '%Y%m/%d'
rescue
next
end
if date < limit
FileUtils.rm_r entry
end
topdir = date.strftime '%Y%m'
entries = Dir.entries(topdir) - ['.', '..']
p entries
if entries.empty?
FileUtils.rmdir topdir
end
end