forked from rsnapshot/rsnapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsnapshot-diff.pl
executable file
·296 lines (220 loc) · 8.57 KB
/
rsnapshot-diff.pl
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!@PERL@ -w
##############################################################################
# rsnapshot-diff
# by David Cantrell <[email protected]>
#
# This program calculates the differences between two directories. It is
# designed to work with two different subdirectories under the rsnapshot
# snapshot_root. For example:
#
# rsnapshot-diff /.snapshots/daily.0/ /.snapshots/daily.1/
#
# http://www.rsnapshot.org/
##############################################################################
# $Id: rsnapshot-diff.pl,v 1.6 2010/08/10 13:00:15 drhyde Exp $
=head1 NAME
rsnapshot-diff - a utility for comparing the disk usage of two snapshots
taken by rsnapshot
=cut
use strict;
use constant DEBUG => 0;
use Getopt::Std;
my $program_name = 'rsnapshot-diff';
my %opts;
my $verbose = 0;
my $ignore = 0;
my $show_size = 0;
my $result = getopts('vVhHis', \%opts);
# help
if ($opts{'h'}) {
print qq{
$program_name [-vVHhi] dir1 dir2
$program_name shows the differences between two 'rsnapshot' backups.
-h show this help
-H also show "human" sizes - MB and GB as well as just bytes
-i ignore symlinks, directories, and special files in verbose output
-s show the size of each changed file
-v be verbose
-V be more verbose (mutter about unchanged files and about symlinks)
dir1 the first directory to look at
dir2 the second directory to look at
if you want to look at directories called '-h' or '-v' pass a
first parameter of '--'.
$program_name always show the changes made starting from the older
of the two directories.
};
exit;
}
=head1 SYNOPSIS
rsnapshot-diff [-h|vVi] dir1 dir2
=head1 DESCRIPTION
rsnapshot-diff is a companion utility for rsnapshot, which traverses two
parallel directory structures and calculates the difference between them.
By default it is silent apart from displaying summary information at the
end, but it can be made more verbose.
In the summary, "added" files may very well include files which at first
glance also appear at the same place in the older directory structure.
However, because the files differ in some respect, they are different files.
They have a different inode number. Consequently if you use -v most of its
output may appear to be pairs of files with the same name being removed
and added.
=head1 OPTIONS
=over 4
=item -h (help)
Displays help information
=item -H (human)
Display more human-friendly numbers - as well as showing the number of
bytes changed, also show MB and GB.
=item -i (ignore)
If verbosity is turned on, -i suppresses information about symlinks,
directories, and special files.
=item -s (show size)
Show the size of each changed file after the + or - sign. To sort the files by
decreasing size, use this option and run the output through "sort -k 2 -rn".
=item -v (verbose)
Be verbose. This will spit out a list of all changes as they are encountered,
apart from symlink, as well as the summary at the end.
=item -V (more verbose)
Be more verbose - as well as listing changed files, unchanged files and
symlinks will be listed too.
=item dir1 and dir2
These are the only compulsory parameters, and should be the names of two
directories to compare. Their order doesn't matter, rsnapshot-diff will
always compare the younger to the older, so files that appear only in the
older will be reported as having been removed, and files that appear only
in the younger will be reported as having been added.
=back
=cut
# verbose
if ($opts{'v'}) { $verbose = 1; }
# extra verbose
if ($opts{'V'}) { $verbose = 2; }
# ignore
if ($opts{'i'}) { $ignore = 1; }
# size
if ($opts{'s'}) { $show_size = 1; }
if(!exists($ARGV[1]) || !-d $ARGV[0] || !-d $ARGV[1]) {
die("$program_name\nUsage: $program_name [-vVhi] dir1 dir2\nType $program_name -h for details\n");
}
my($dirold, $dirnew) = @ARGV;
my($addedfiles, $addedspace, $deletedfiles, $deletedspace) = (0, 0, 0, 0);
my($addedspace_mb, $addedspace_gb, $deletedspace_mb, $deletedspace_gb) = (0, 0, 0, 0);
($dirold, $dirnew) = ($dirnew, $dirold) if(-M $dirold < -M $dirnew);
# remove trailing slahes, if any
$dirold =~ s/\/+$//;
$dirnew =~ s/\/+$//;
print "Comparing $dirold to $dirnew\n";
compare_dirs($dirold, $dirnew);
$addedspace_mb = sprintf("%.2f", $addedspace / (1024 * 1024));
$addedspace_gb = sprintf("%.2f", $addedspace_mb / 1024);
$deletedspace_mb = sprintf("%.2f", $deletedspace / (1024 * 1024));
$deletedspace_gb = sprintf("%.2f", $deletedspace_mb / 1024);
print "Between $dirold and $dirnew:\n";
print " $addedfiles were added, taking $addedspace bytes".
($opts{H} ? " ($addedspace_mb MB, $addedspace_gb GB)" : '').
"\n";
print " $deletedfiles were removed, saving $deletedspace bytes".
($opts{H} ? " ($deletedspace_mb MB, $deletedspace_gb GB)" : '').
"\n";
sub compare_dirs {
my($old, $new) = @_;
opendir(OLD, $old) || die("Can't open dir $old\n");
opendir(NEW, $new) || die("Can't open dir $new\n");
my %old = map {
my $fn = $old.'/'.$_;
($_, (mystat($fn))[1])
} grep { $_ ne '.' && $_ ne '..' } readdir(OLD);
my %new = map {
my $fn = $new.'/'.$_;
($_, (mystat($fn))[1])
} grep { $_ ne '.' && $_ ne '..' } readdir(NEW);
closedir(OLD);
closedir(NEW);
my @added = grep { !exists($old{$_}) } keys %new;
my @deleted = grep { !exists($new{$_}) } keys %old;
my @changed = grep { !-d $new.'/'.$_ && exists($old{$_}) && $old{$_} != $new{$_} } keys %new;
add(map { $new.'/'.$_ } @added, @changed);
remove(map { $old.'/'.$_ } @deleted, @changed);
if($verbose == 2) {
my %changed = map { ($_, 1) } @changed, @added, @deleted;
print "0 $new/$_\n" foreach(grep { !-d "$new/$_" && !exists($changed{$_}) } keys %new);
}
foreach (grep { !-l $new.'/'.$_ && !-l $old.'/'.$_ && -d $new.'/'.$_ && -d $old.'/'.$_ } keys %new) {
print "Comparing subdirs $new/$_ and $old/$_ ...\n" if(DEBUG);
compare_dirs($old.'/'.$_, $new.'/'.$_);
}
}
sub add {
my @added = @_;
print "Adding ".join(', ', @added)."\n" if(DEBUG && @added);
foreach(grep { !-d } @added) {
$addedfiles++;
my $size = (mystat($_))[7];
$addedspace += $size;
# if ignore is on, only print files
unless ($ignore && (-l || !-f)) {
print ''.($show_size ? "+ $size $_" : "+ $_").
(-l $_ ? ' (symlink)' : '').
"\n"
if($verbose == 2 || ($verbose == 1 && !-l $_));
}
}
foreach my $dir (grep { !-l && -d } @added) {
opendir(DIR, $dir) || die("Can't open dir $dir\n");
add(map { $dir.'/'.$_ } grep { $_ ne '.' && $_ ne '..' } readdir(DIR))
}
}
sub remove {
my @removed = @_;
print "Removing ".join(', ', @removed)."\n" if(DEBUG && @removed);
foreach(grep { !-d } @removed) {
$deletedfiles++;
my $size = (mystat($_))[7];
$deletedspace += $size;
# if ignore is on, only print files
unless ($ignore && (-l || !-f)) {
print ''.($show_size ? "- $size $_" : "- $_").
(-l $_ ? ' (symlink)' : '').
"\n"
if($verbose == 2 || ($verbose == 1 && !-l $_));
}
}
foreach my $dir (grep { !-l && -d } @removed) {
opendir(DIR, $dir) || die("Can't open dir $dir\n");
remove(map { $dir.'/'.$_ } grep { $_ ne '.' && $_ ne '..' } readdir(DIR))
}
}
{
my $device;
sub mystat {
local $_ = shift;
my @stat = (-l) ? lstat() : stat();
# on first stat, memorise device
$device = $stat[0] unless(defined($device));
die("Can't compare across devices.\n(looking at $_)\n")
unless($device == $stat[0] || -p $_);
return @stat;
}
}
=head1 SEE ALSO
rsnapshot
=head1 BUGS
Please report bugs (and other comments) to the rsnapshot-discuss mailing list:
L<http://lists.sourceforge.net/lists/listinfo/rsnapshot-discuss>
=head1 AUTHOR
David Cantrell E<lt>[email protected]<gt>
=head1 COPYRIGHT
Copyright 2005-2010 David Cantrell
=head1 LICENCE
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
=cut