-
Notifications
You must be signed in to change notification settings - Fork 1
/
btr-backup-clean
executable file
·166 lines (151 loc) · 4.46 KB
/
btr-backup-clean
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/ruby
#
# btr-backup-clean: cleanup old backup directory for btr-backup
#
# Usage:
# btr-backup-clean <backup directories...>
#
# This script was forked from hlbackup-clean;
# http://hlbackup.sourceforge.jp/
#
# Copyright(C) 2003 Taku YASUI <tach@debian.or.jp>,
# Copyright(C) 2010 Tatsuki Sugiura <sugi@nemui.org>, All rights reserved.
# This is free software with ABSOLUTELY NO WARRANTY.
#
# You can redistribute it and/or modify it under the terms of
# the GNU General Public License version 2.
#
# 2010-09-20:
# Modified for btr-backup by Tatsuki Sugiura <sugi@nemui.org>
#
require 'getoptlong'
require 'date'
NAME = 'btr-backup-clean'
VERS = '0.0.1'
DEFAULT = { 'year' => 2,
'month' => 6,
'week' => 6,
'day' => 7 }
def parse_options
options = Hash.new
parser = GetoptLong.new
parser.set_options(['--help', '-h', GetoptLong::NO_ARGUMENT],
['--no-act', '-n', GetoptLong::NO_ARGUMENT],
['--verbose', '-v', GetoptLong::NO_ARGUMENT],
['--version', '-V', GetoptLong::NO_ARGUMENT],
['--keep', '-k', GetoptLong::REQUIRED_ARGUMENT])
parser.each_option {|name, arg|
options[name.sub(/^--/, "")] = arg
}
show_version if options['version']
show_usage if options['help'] or ARGV[0].nil?
return options
end
def parse_keep(keep)
ch_tables = { 'year' => /(\d+)Y/,
'month' => /(\d+)M/,
'week' => /(\d+)W/,
'day' => /(\d+)D/ }
ret = {}
ch_tables.each do |key, regex|
ret[key] = DEFAULT[key]
ret[key] = $1.to_i if ( regex.match(keep) )
ret[key] -= 1
end
return ret
end
def create_keepdirs(keep)
nowdate = Date.new(Time.now.year, Time.now.month, Time.now.day)
ret = {}
# year
keep['year'].downto(0) do |i|
(ret["#{Time.now.year-i}-01-01"] ||= []) << :Yearly
end
# month
keep['month'].downto(0) do |i|
date = nowdate << i
(ret["#{date.year}-#{sprintf("%02d", date.month)}-01"] ||= []) << :Monthly
end
# week
keep['week'].downto(0) do |i|
date = nowdate - nowdate.cwday - 7 * i
(ret["#{date.year}-#{sprintf("%02d", date.month)}-#{sprintf("%02d", date.day)}"] ||= []) << :Weekly
end
# day
keep['day'].downto(0) do |i|
date = nowdate - i
(ret[("#{date.year}-#{sprintf("%02d", date.month)}-#{sprintf("%02d", date.day)}")] ||= []) << :Daily
end
ret
end
def show_usage
print <<_EOT
Usage: #{NAME} [OPTIONS] directories...
OPTIONS:
--help, -h: show this help
--keep KEEPARGS, -k: use KEEPARGS to decide delete directories
--no-act, -n: run command but don't do it
--verbose, -v: verbose output
--version, -V: show software version
KEEPARGS: [digitY][digitM][digitW][digitD]
e.g. --keep 2Y6M6W7D (2years, 6months, 6weeks, 7days, default)
_EOT
exit
end
def show_version
print <<_EOT
#{NAME} version #{VERS}
Copyright (C) 2003 Taku YASUI <tach@debian.or.jp>
Copyright (C) 2010 Tatsuki Sugiura <sugi@nemui.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
_EOT
exit
end
def main
opts = parse_options
keepdirs = create_keepdirs(parse_keep(opts['keep']))
# lower priority
Process.setpriority(Process::PRIO_PROCESS, $$, 19)
begin
system('ionice', '-c3', "-p#{$$}")
rescue
# ignore
end
ARGV.each do |basedir|
keeped = []
Dir.glob("#{basedir}/[0-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]").sort.each do |dir|
next if ( ! File.directory?(dir) )
if File.stat(dir).ino != 256
$stderr.puts "Skip: '#{dir}' seems not snapshot."
next
end
daykey = dir[(basedir.length+1)..(basedir.length+10)]
if keepdirs.has_key? daykey
keeped.push [dir, keepdirs[daykey]]
next
end
puts "Deleting #{dir}" if opts['verbose'] && opts['no-act']
args = ['btrfs', 'subvolume', 'delete', "'"+dir.gsub(/'/, %q{\'})+"'"]
args << "> /dev/null" unless opts['verbose']
begin
system(args.join(' ')) unless opts['no-act']
rescue => e
$stderr.puts "btrfs command execution failed: #{e.inspect}"
next
end
if !opts['no-act'] && ($? >> 8) != 0
$stderr.puts "btrfs has been exited with error=#{$?>>8} on #{dir}"
end
end
if opts['verbose']
puts "Keep dirs on #{basedir}:"
keeped.each do |dir|
puts "#{dir[0]} (#{dir[1].join(', ').to_s})"
end
end
end
end
main if __FILE__ == $0